CoreBluetooth 检测附件接近度(应用程序在后台)

CoreBluetooth to detect accessory proximity (whilst app in background)

当应用程序在后台运行时,我想使用 CoreBluetooth 检测与硬件的接近度(发出 BLE 信号)。

第一步是在功能选项卡中激活 BLE 后台模式。这将允许应用程序在 运行 在 背景 中接收 BLE 信号。现在,第二步是编写代码来检测与 BLE 外围设备的接近度

查看 iOS 开发人员 Bluetooth guide (at page 45/46) 我发现:

CBCentralManagerScanOptionAllowDuplicatesKey constant as a scan option when calling the scanForPeripheralsWithServices:options: method. When you do, a discovery event is generated each time the central receives an advertising packet from the peripheral. Turning off the default behavior can be useful for certain use cases, such as initiating a connection to a peripheral based on the peripheral’s proximity (using the peripheral received signal strength indicator (RSSI) value). T

是的,如您所述,使用 CoreBluetooth 是一种有效的方法。您可以获得在前台检测到的每个数据包的回调(以及在非制造商广告的后台)。然后,您可以将 RSSI 读取为接近度指标。

无论您想使用 CoreBluetooth 还是将 iBeacons 与 CoreLocation 一起使用,在大多数前景测距情况下电池使用情况都是相似的。

如果使用 CoreBluetooth,您可能不希望在后台无限期地接收每个数据包的回调,因为它会更快地耗尽电池电量。 CoreLocation iBeacon API 将每次唤醒事件在后台进行的测距限制为 10 秒,以帮助节省电量。

如果您使用 CoreBluetooth 发现您的应用在后台运行的时间更长,您可能需要添加自己的逻辑以防止电池耗尽。