4
votes

I am scanning for a hardware device . When in foreground i can see the device. When i start the scanning in foreground,than move to background, than i turn on the hardware-BLE, the scanning stops , and the app can't detect the hardware .

If i open back the app to foreground, it then continue the scanning and finds the device.

I have set the keys :

  <string>bluetooth-peripheral</string>
        <string>bluetooth-central</string>

and also the background modes:

location updates
external accessory communication
uses BLE accessories
acts as BLE ACCESSORY

But nothing. the system won't let me detect devices on background,only foreground .

What am i missing ?

2

2 Answers

0
votes

Found out that you must set the service id that you look for in background otherwise it won't work. Anyway the scanning in background is very slowly and can take minutes to detect!!

 [_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID]]
0
votes

You can scan BLE in background only for specific serviceUUID, you can scan by passing nil as we do for foreground scan.

Also, ensure that you start the BLE scan in the main thread and set the proper option as below.

 NSArray *services = @[[CBUUID UUIDWithString:UUID1],[CBUUID UUIDWithString:UUID2],[CBUUID UUIDWithString:UUID3]];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.coreBluetoothManager scanForPeripheralsWithServices:services options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil]];
});