BLE后台重连
BLE background reconnect
我想在设备被用户移动 out/terminated 或在后台模式下 system/reboted 后重新连接到 BLE 设备。
我知道这是可能的 : - see this question with description
问题 - 如何设置 centralManager
以在应用程序终止时自动重新连接到后台模式下的外围设备?有人可以逐步描述如何完成吗?
关于当前实现的几句话:
我使用以下选项创建 centralManager:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{
CBCentralManagerOptionRestoreIdentifierKey: @"myCentralManagerIdentifier",
CBCentralManagerRestoredStatePeripheralsKey : @YES,
CBCentralManagerRestoredStateScanServicesKey : @YES,
CBCentralManagerRestoredStateScanOptionsKey : @YES
}];
之后我开始扫描 BLE 设备
[self.centralManager scanForPeripheralsWithServices:[self discoverableCharacteristics] options:nil];
在- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
我连接到外设:
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
[self.centralManager stopScan];
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options: @{
CBConnectPeripheralOptionNotifyOnNotificationKey : @YES
}];
之后我可以发现服务和特征 - 一切看起来都不错。当我发现特征和 read/write 数据时,我 cancelPeripheralConnection
在 didDisconnect 中我重新连接到设备
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error
{
[central connectPeripheral:peripheral options:nil];
}
我还实现了 centralManager:willRestoreState:
,例如:
NSArray *peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey];
for (CBPeripheral *peripheral in peripherals) {
[central connectPeripheral:peripheral options:nil];
peripheral.delegate = nil;
}
在 plist 中。添加了必需的密钥 App communicates using CoreBluetooth
.
目前,如果我连接到设备并终止它 - 它会自动重新启动并连接到设备 - 一切正常,但如果它再次终止 - 没有任何反应。
此外,如果我从外围设备中移出并返回 - 什么也没有发生。
更新
关于第 5 点 - 我的秋天 - 应该将此密钥与 connectPeripheral
一起使用
在WillRestoreState:
NSArray *peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey];
if (!peripherals.count) {
peripherals = [central retrievePeripheralsWithIdentifiers:[self discoverableCharacteristics]];
}
if (peripherals.count) {
for (CBPeripheral *peripheral in peripherals) {
[central connectPeripheral:peripheral options:@{
CBCentralManagerRestoredStatePeripheralsKey : @YES,
CBCentralManagerRestoredStateScanServicesKey : @YES,
CBCentralManagerRestoredStateScanOptionsKey : @YES
}];
}
} else {
[self startScanning];
}
当前结果 - 如果应用程序没有从托盘中滑出,它将重新启动。我使用我的 mac 作为外围设备,所以有时当我不启动应用程序时,扮演外围中心角色的应用程序可以连接到 mac 本身而不是所需的服务。
另一个问题 - 在失去连接时重新连接到外围设备以保持连接是否是一个不错的选择,例如:
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error
{
[central connectPeripheral:peripheral options:@{
CBCentralManagerRestoredStatePeripheralsKey : @YES,
CBCentralManagerRestoredStateScanServicesKey : @YES,
CBCentralManagerRestoredStateScanOptionsKey : @YES
}];
}
同时尝试更改外围设备上的通知特性并在设备上读取它。如果一切都在前台完成——一切都完美,但如果连接是在后台完成的,有时 didUpdateValueForCharacteristic
根本没有被调用,但是 didUpdateNotificationStateForCharacteristic
被调用没有错误——这意味着(我认为)在我身边做错了。也许你可以建议问题出在哪里
还有一个问题 - 将数据写入特征是否有一些限制?因为在 apple 示例中它被设置为 20 个字节。
首先我想说的是,我已经使用 CoreBluetooth 工作了大约两年,从我注意到的情况来看,CoreBluetooth 状态保存和恢复 不能可靠地工作全部。您可以让它正常工作 "ok",但除非 Apple 某天修复它,否则您永远无法让它可靠地重新连接。
话虽如此,我想在您的设置中注意一些事项。
1) 在 centralManager:willRestoreState:
中,您只能检索在应用程序终止时进行过任何通信的外围设备。这意味着您还应该实现 centralManagerDidUpdateState:
,如果状态为 CBCentralManagerStatePoweredOn
,那么您可以使用 retrievePeripheralsWithIdentifiers:
方法来检索其他外围设备并重置它们的委托。这当然意味着您必须将外围设备标识符存储在应用程序中的某个位置。还记得在这里也重置挂起的连接。
2) 您在 centralManager:willRestoreState:
中将委托设置为 nil!所以即使它确实连接了你也不会知道它 i:P
3) 只有当应用程序被系统终止时,您的应用程序才会重新启动。如果您从应用程序列表中手动滑动杀死它,它将不会重新启动。不幸的是,如果设备重新启动,它也不会重新启动。
4) CBConnectPeripheralOptionNotifyOnConnectionKey
在使用 bluetooth-central 后台模式时不是必需的,只是对用户来说很烦人,所以我不会使用它。
5) CBCentralManagerRestoredStatePeripheralsKey
、CBCentralManagerRestoredStateScanServicesKey
、CBCentralManagerRestoredStateScanOptionsKey
不是有效的初始化选项所以我不明白你为什么要使用这些..
5) 如果蓝牙在应用程序终止时切换状态,则所有挂起的连接都将丢失,您将不会重新启动以了解它。这本身就意味着状态恢复是相当无用的。
无论如何,我很难过地说,但是如果你正在开发一个必须依赖于在后台重新连接的外围设备的应用程序,那么我不建议这样做。你会感到沮丧。我可能会写一篇关于 Core Bluetooth 中 Apple 不想修复的所有错误的文章。更可怕的是,你可以很容易地通过一个应用程序破坏设备上全局的蓝牙连接,这样在设备重新启动之前,任何应用程序都无法使用蓝牙。这非常糟糕,因为它违背了 Apple 自己的沙盒原则。
如果您需要更多帮助,请告诉我!
/A
我想在设备被用户移动 out/terminated 或在后台模式下 system/reboted 后重新连接到 BLE 设备。
我知道这是可能的 : - see this question with description
问题 - 如何设置 centralManager
以在应用程序终止时自动重新连接到后台模式下的外围设备?有人可以逐步描述如何完成吗?
关于当前实现的几句话:
我使用以下选项创建 centralManager:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{
CBCentralManagerOptionRestoreIdentifierKey: @"myCentralManagerIdentifier",
CBCentralManagerRestoredStatePeripheralsKey : @YES,
CBCentralManagerRestoredStateScanServicesKey : @YES,
CBCentralManagerRestoredStateScanOptionsKey : @YES
}];
之后我开始扫描 BLE 设备
[self.centralManager scanForPeripheralsWithServices:[self discoverableCharacteristics] options:nil];
在- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
我连接到外设:
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
[self.centralManager stopScan];
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options: @{
CBConnectPeripheralOptionNotifyOnNotificationKey : @YES
}];
之后我可以发现服务和特征 - 一切看起来都不错。当我发现特征和 read/write 数据时,我 cancelPeripheralConnection
在 didDisconnect 中我重新连接到设备
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error
{
[central connectPeripheral:peripheral options:nil];
}
我还实现了 centralManager:willRestoreState:
,例如:
NSArray *peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey];
for (CBPeripheral *peripheral in peripherals) {
[central connectPeripheral:peripheral options:nil];
peripheral.delegate = nil;
}
在 plist 中。添加了必需的密钥 App communicates using CoreBluetooth
.
目前,如果我连接到设备并终止它 - 它会自动重新启动并连接到设备 - 一切正常,但如果它再次终止 - 没有任何反应。
此外,如果我从外围设备中移出并返回 - 什么也没有发生。
更新
关于第 5 点 - 我的秋天 - 应该将此密钥与 connectPeripheral
在WillRestoreState:
NSArray *peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey];
if (!peripherals.count) {
peripherals = [central retrievePeripheralsWithIdentifiers:[self discoverableCharacteristics]];
}
if (peripherals.count) {
for (CBPeripheral *peripheral in peripherals) {
[central connectPeripheral:peripheral options:@{
CBCentralManagerRestoredStatePeripheralsKey : @YES,
CBCentralManagerRestoredStateScanServicesKey : @YES,
CBCentralManagerRestoredStateScanOptionsKey : @YES
}];
}
} else {
[self startScanning];
}
当前结果 - 如果应用程序没有从托盘中滑出,它将重新启动。我使用我的 mac 作为外围设备,所以有时当我不启动应用程序时,扮演外围中心角色的应用程序可以连接到 mac 本身而不是所需的服务。
另一个问题 - 在失去连接时重新连接到外围设备以保持连接是否是一个不错的选择,例如:
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error
{
[central connectPeripheral:peripheral options:@{
CBCentralManagerRestoredStatePeripheralsKey : @YES,
CBCentralManagerRestoredStateScanServicesKey : @YES,
CBCentralManagerRestoredStateScanOptionsKey : @YES
}];
}
同时尝试更改外围设备上的通知特性并在设备上读取它。如果一切都在前台完成——一切都完美,但如果连接是在后台完成的,有时 didUpdateValueForCharacteristic
根本没有被调用,但是 didUpdateNotificationStateForCharacteristic
被调用没有错误——这意味着(我认为)在我身边做错了。也许你可以建议问题出在哪里
还有一个问题 - 将数据写入特征是否有一些限制?因为在 apple 示例中它被设置为 20 个字节。
首先我想说的是,我已经使用 CoreBluetooth 工作了大约两年,从我注意到的情况来看,CoreBluetooth 状态保存和恢复 不能可靠地工作全部。您可以让它正常工作 "ok",但除非 Apple 某天修复它,否则您永远无法让它可靠地重新连接。
话虽如此,我想在您的设置中注意一些事项。
1) 在 centralManager:willRestoreState:
中,您只能检索在应用程序终止时进行过任何通信的外围设备。这意味着您还应该实现 centralManagerDidUpdateState:
,如果状态为 CBCentralManagerStatePoweredOn
,那么您可以使用 retrievePeripheralsWithIdentifiers:
方法来检索其他外围设备并重置它们的委托。这当然意味着您必须将外围设备标识符存储在应用程序中的某个位置。还记得在这里也重置挂起的连接。
2) 您在 centralManager:willRestoreState:
中将委托设置为 nil!所以即使它确实连接了你也不会知道它 i:P
3) 只有当应用程序被系统终止时,您的应用程序才会重新启动。如果您从应用程序列表中手动滑动杀死它,它将不会重新启动。不幸的是,如果设备重新启动,它也不会重新启动。
4) CBConnectPeripheralOptionNotifyOnConnectionKey
在使用 bluetooth-central 后台模式时不是必需的,只是对用户来说很烦人,所以我不会使用它。
5) CBCentralManagerRestoredStatePeripheralsKey
、CBCentralManagerRestoredStateScanServicesKey
、CBCentralManagerRestoredStateScanOptionsKey
不是有效的初始化选项所以我不明白你为什么要使用这些..
5) 如果蓝牙在应用程序终止时切换状态,则所有挂起的连接都将丢失,您将不会重新启动以了解它。这本身就意味着状态恢复是相当无用的。
无论如何,我很难过地说,但是如果你正在开发一个必须依赖于在后台重新连接的外围设备的应用程序,那么我不建议这样做。你会感到沮丧。我可能会写一篇关于 Core Bluetooth 中 Apple 不想修复的所有错误的文章。更可怕的是,你可以很容易地通过一个应用程序破坏设备上全局的蓝牙连接,这样在设备重新启动之前,任何应用程序都无法使用蓝牙。这非常糟糕,因为它违背了 Apple 自己的沙盒原则。
如果您需要更多帮助,请告诉我!
/A