我正在从事 BLE 项目,当应用程序在前台时一切正常。它可以发现并连接到外围设备,所有回调方法都可以正常工作。
但问题是,当应用程序处于后台模式时(我按下主页按钮)。仅调用 centralManagerDidUpdateState
委托(delegate)方法。
- (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 文件,以支持后台的 ble central 角色。
知道为什么 didDiscoverPeripheral
方法在后台运行时不被调用吗?
最佳答案
Paulw11 说的对,如果你的应用程序在前台找到外围设备。它不会在进入后台时为相同的外围设备调用didDiscoverPeripheral
。
有关后台模式下 iOS BLE 行为的更多信息。你可以检查这个答案 What exactly can CoreBluetooth applications do whilst in the background?
关于iOS:在后台模式下未调用 didDiscoverPeripheral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32140863/