没有在后台发现外设ibeacon

时间:2017-05-22 06:29:13

标签: ios swift bluetooth-lowenergy ibeacon

当我指定这样的服务时:

centralManager.scanForPeripherals(withServices: [BEAN_SERVICE_UUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])

我确定我输入了BEAN_SERVICE_UUID。事实证明,当我在前台执行该功能时,我可以得到didDiscover外设回调。

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { print(advertisementData) }

这是我的常规设置功能和我的info.plist

enter image description here

enter image description here

那么,我必须做些什么来使ScanForPeripheral / didDiscover外围设备工作?

*编辑 经过多次测试后,我得知我的应用程序可以进行后台扫描外设,但只是在安装应用程序后的初始启动时(仅一次),之后,ScanForPeripheral / didDiscover外设无法在后台运行。

1 个答案:

答案 0 :(得分:0)

当您在后台发现蓝牙外围设备时,CBCentralManagerScanOptionAllowDuplicatesKey会自动被忽略并设置为false。据说你在后台只收到一个广告包,你必须用它做一些动作。如果你不这样做,这个外围设备将永远不会再次调用didDiscover方法。

对于iBeacons,您应该使用CoreLocation框架。