OGeek|极客世界-中国程序员成长平台

标题: ios - 设备锁定时扫描外围设备 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:09
标题: ios - 设备锁定时扫描外围设备

我的中央管理器可以在前台和后台检测新的外围设备 - 我知道这一点,因为当它发现新的外围设备时我会触发 UNNotification。

但是,当设备被锁定时,它似乎不会继续扫描新的外围设备。

在我的能力范围内,我为使用 LE 配件以及远程通知启用了后台模式。我是否缺少 BLE 的某些功能,可以让我在设备锁定时检测到新的外围设备?

更新更多信息:

以下是我实例化中央管理器并开始扫描的方法:

central = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : kRestoreIdKey])
central!.scanForPeripherals(withServices: [kMyService, kMyOtherService], options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])

这里启用了后台模式: enter image description here

此外,后台模式的 plist 键: enter image description here

知道为什么我的本地通知会在前台或后台扫描时触发,而不是在屏幕锁定时触发吗?



Best Answer-推荐答案


作为蓝牙中央后台应用程序,您应该能够在设备锁定时继续扫描,但前提是您正在扫描特定的服务 UUID。

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.

您无法在后台扫描任意设备。 (在某些情况下您可能会侥幸成功,但这并不意味着它受支持。)

也就是说,当设备被锁定时,扫描间隔可以大大增加(因为没有前台应用程序扫描并且系统通常试图通过关闭天线来节省电力)。如果您的设备也有很长的广告间隔(特别是如果它没有推荐的广告间隔之一),那么两者可能需要很长时间才能发现对方(想想几分钟,而不是几秒钟)。

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.

推荐的广告间隔列表位于Bluetooth Design Guidelines第 3.5 节。

Useful thread on the topic.

关于ios - 设备锁定时扫描外围设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42775327/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4