我想使用 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
- 这是正确的方向吗?
- 在 iOS 设备电池使用方面,这种方法是否比使用 iBeacon 效率低?
最佳答案
是的,如您所述,这是使用 CoreBluetooth 的有效方法。您可以获得在前台检测到的每个数据包的回调(以及在非制造商广告的后台)。然后,您可以将 RSSI 解读为距离的指示器。
无论您想使用 CoreBluetooth 还是使用带有 CoreLocation 的 iBeacons,在大多数前景测距情况下电池使用情况都是相似的。
如果使用 CoreBluetooth,您可能不希望在后台无限期地获取每个数据包的回调,因为它会更快地耗尽电池电量。 CoreLocation iBeacon API 将每次唤醒事件在后台进行的测距限制为 10 秒,以帮助节省电量。
如果您发现您的应用使用 CoreBluetooth 在后台长时间运行,您可能需要添加自己的逻辑以防止电池耗尽。
关于ios - CoreBluetooth 检测附件接近度(应用程序在后台),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31271195/