diff --git a/android/build.gradle b/android/build.gradle index d2567825..55359823 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -105,5 +105,5 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.facebook.react:react-native:0.6+' implementation 'com.intellij:annotations:+@jar' - compile 'org.altbeacon:android-beacon-library:2.16.1' + implementation 'org.altbeacon:android-beacon-library:2.19.4' } diff --git a/ios/RNiBeacon/RNiBeacon/ESSBeaconScanner.m b/ios/RNiBeacon/RNiBeacon/ESSBeaconScanner.m index 57eee79b..6c274dfc 100644 --- a/ios/RNiBeacon/RNiBeacon/ESSBeaconScanner.m +++ b/ios/RNiBeacon/RNiBeacon/ESSBeaconScanner.m @@ -29,7 +29,7 @@ *=-----------------------------------------------------------------------------------------------= */ @interface ESSBeaconScanner () { - CBCentralManager *_centralManager; + CBCentralManager *_internalCentralManager; dispatch_queue_t _beaconOperationsQueue; /** @@ -61,18 +61,26 @@ - (instancetype)init { _onLostTimeout = 5.0; _tlmCache = [NSMutableDictionary dictionary]; _beaconOperationsQueue = dispatch_queue_create(kBeaconsOperationQueueName, NULL); - _centralManager = [[CBCentralManager alloc] initWithDelegate:self - queue:_beaconOperationsQueue]; } return self; } +-(CBCentralManager *)centralManager { + // Calling init() for the first time will ask the user to give the app the permission + // To prevent this happening on app start, we will delay this for the first call + if (_internalCentralManager == nil) { + _internalCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_beaconOperationsQueue options:@{CBCentralManagerOptionShowPowerAlertKey: @NO}]; + [NSThread sleepForTimeInterval: 0.05]; // Calling directly after init() will give us .Unknown. So just sleep for 50ms to prevent this + } + return _internalCentralManager; +} + - (void)startScanning { dispatch_async(_beaconOperationsQueue, ^{ - if (_centralManager.state != CBCentralManagerStatePoweredOn) { + if ([self centralManager].state != CBCentralManagerStatePoweredOn) { NSLog(@"CBCentralManager state is %ld, cannot start or stop scanning", - (long)_centralManager.state); + (long)[self centralManager].state); _shouldBeScanning = YES; } else { NSLog(@"Starting to scan for Eddystones"); @@ -84,14 +92,14 @@ - (void)startScanning { // We do not want multiple discoveries of the same beacon to be coalesced into one. // (Unfortunately this is ignored when we are in the background.) NSDictionary *options = @{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }; - [_centralManager scanForPeripheralsWithServices:services options:options]; + [[self centralManager] scanForPeripheralsWithServices:services options:options]; } }); } - (void)stopScanning { _shouldBeScanning = NO; - [_centralManager stopScan]; + [[self centralManager] stopScan]; [self clearRemainingTimers]; }