iOS: didDiscoverPeripheral 未在后台模式下调用
iOS: didDiscoverPeripheral not called in Background mode
我正在从事 BLE 项目,当 foreground.It 中的应用程序可以发现并连接到外围设备时一切正常,所有回调方法都可以正常工作。
但问题是,当应用程序处于后台模式时(我按下主页按钮)。仅调用 centralManagerDidUpdateState
委托方法。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOn:
[self.cbCentralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
break;
default:
break;
}
}
我使用scanForPeripheralsWithServices:nil
选项,但是当应用程序在后台时,didDiscoverPeripheral
回调从未调用过。我已使用 "bluetooth-central" 选项编辑我的 plist 文件,以支持后台的中心角色。
知道为什么当应用在后台时 didDiscoverPeripheral
方法不被调用吗?
nil
(scanForPeripheralsWithServices:nil
) 服务扫描将无法在后台运行。您必须在后台搜索某些特定服务。
您必须在 scanForPeripheralsWithServices:
方法中设置 UUID
,Peripherals/BLE 设备正在广播。
Paulw11 说的对,如果你的应用程序在前台找到外设。进入后台后不会调用相同外设的didDiscoverPeripheral
有关后台模式下 iOS BLE 行为的更多信息。你可以检查这个答案
What exactly can CoreBluetooth applications do whilst in the background?
来自 Apple 官方参考资料
You can provide an array of CBUUID objects—representing service
UUIDs—in the serviceUUIDs parameter. When you do, the central manager
returns only peripherals that advertise the services you specify
(recommended). If the serviceUUIDs parameter is nil, all discovered
peripherals are returned regardless of their supported services (not
recommended). If the central manager is already scanning with
different parameters, the provided parameters replace them. When the
central manager object discovers a peripheral, it calls the
centralManager:didDiscoverPeripheral:advertisementData:RSSI: method of
its delegate object.
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 CBCentralManagerOptionShowPowerAlertKey
scan option is ignored while scanning in the background.
这里
允许指定bluetooth-central后台模式的应用在后台进行扫描。也就是说,他们必须通过在 serviceUUIDs 参数中指定来明确扫描一项或多项服务。
所以 scanForPeripheralsWithServices:nil
没有它不会在后台工作,你需要指定 UUIDS 列表
我正在研究 Estimote Nearable 类型的信标。 iOS10 SDK 更新后,我遇到了来自 CBCentralManager
的异常:
<CBCentralManager: 0x17009e050> has provided a restore identifier but the delegate doesn't implement the centralManager:willRestoreState: method
要解决此问题,在 Xcode -> 功能 -> 后台模式 中打开 "Background Mode" ]
我正在从事 BLE 项目,当 foreground.It 中的应用程序可以发现并连接到外围设备时一切正常,所有回调方法都可以正常工作。
但问题是,当应用程序处于后台模式时(我按下主页按钮)。仅调用 centralManagerDidUpdateState
委托方法。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOn:
[self.cbCentralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
break;
default:
break;
}
}
我使用scanForPeripheralsWithServices:nil
选项,但是当应用程序在后台时,didDiscoverPeripheral
回调从未调用过。我已使用 "bluetooth-central" 选项编辑我的 plist 文件,以支持后台的中心角色。
知道为什么当应用在后台时 didDiscoverPeripheral
方法不被调用吗?
nil
(scanForPeripheralsWithServices:nil
) 服务扫描将无法在后台运行。您必须在后台搜索某些特定服务。
您必须在 scanForPeripheralsWithServices:
方法中设置 UUID
,Peripherals/BLE 设备正在广播。
Paulw11 说的对,如果你的应用程序在前台找到外设。进入后台后不会调用相同外设的didDiscoverPeripheral
有关后台模式下 iOS BLE 行为的更多信息。你可以检查这个答案 What exactly can CoreBluetooth applications do whilst in the background?
来自 Apple 官方参考资料
You can provide an array of CBUUID objects—representing service UUIDs—in the serviceUUIDs parameter. When you do, the central manager returns only peripherals that advertise the services you specify (recommended). If the serviceUUIDs parameter is nil, all discovered peripherals are returned regardless of their supported services (not recommended). If the central manager is already scanning with different parameters, the provided parameters replace them. When the central manager object discovers a peripheral, it calls the centralManager:didDiscoverPeripheral:advertisementData:RSSI: method of its delegate object.
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 CBCentralManagerOptionShowPowerAlertKey scan option is ignored while scanning in the background.
这里
允许指定bluetooth-central后台模式的应用在后台进行扫描。也就是说,他们必须通过在 serviceUUIDs 参数中指定来明确扫描一项或多项服务。
所以 scanForPeripheralsWithServices:nil
没有它不会在后台工作,你需要指定 UUIDS 列表
我正在研究 Estimote Nearable 类型的信标。 iOS10 SDK 更新后,我遇到了来自 CBCentralManager
的异常:
<CBCentralManager: 0x17009e050> has provided a restore identifier but the delegate doesn't implement the centralManager:willRestoreState: method
要解决此问题,在 Xcode -> 功能 -> 后台模式 中打开 "Background Mode" ]