Scan for peripherals when device is locked

我的未来我决定 提交于 2019-12-22 17:41:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/42775327/scan-for-peripherals-when-device-is-locked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!