My central manager can detect new peripherals in the foreground and background - I know this because I trigger a UNNotification when it finds a new peripheral.
However, when the device is locked it does not seem to keep scanning for new peripherals.
In my capabilities I have enabled background mode for Using LE accessories, as well as remote notifications. Am I missing something with BLE that can allow me to detect new peripherals when the device is locked?
Update w/ more info:
Here's how I instantiate my central manager and begin scanning:
central = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : kRestoreIdKey])
central!.scanForPeripherals(withServices: [kMyService, kMyOtherService], options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
Here's the background mode enabled:
And furthermore, the plist key for the background mode:
Any idea why my local notification will trigger when it scans in the foreground or background, but NOT when the screen is locked?
As a bluetooth-central background app, you should be able to continue scanning when the device is locked, but only if you are scanning for specific service UUIDs.
Apps that have specified the bluetooth-central background mode are allowed to scan while in the background. That said, they must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter. The CBCentralManager scan option is ignored while scanning in the background.
You cannot scan for arbitrary devices in the background. (It's possible that you get away with it in some cases, but that doesn't mean it's supported.)
That said, the scanning interval can be substantially increased when the device is locked (since there are no foreground apps scanning and the system in general is trying to conserve power by turning off the antenna). If you have a device that also has a long advertising interval (and especially if it doesn't have one of the recommended advertising intervals), then it can take a very long time for the two to discover each other (think minutes, not seconds).
If all apps that are scanning for peripherals are in the background, the interval at which your central device scans for advertising packets increases. As a result, it may take longer to discover an advertising peripheral.
The list of recommended advertising intervals is in Bluetooth Design Guidelines section 3.5.
- 152.5 ms
- 211.25 ms
- 318.75 ms
- 417.5 ms
- 546.25 ms
- 760 ms
- 852.5 ms
- 1022.5 ms
- 1285 ms
Useful thread on the topic.