简体   繁体   中英

BLE scanning is stopped when go to background?

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 ?

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]]

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]];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM