我想在设备被用户或系统移出/终止/在后台模式下重新启动后重新连接到 BLE 设备。
我知道这是可能的:-请参阅此问题及其描述
问题 - 我该如何设置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];
in - (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
}];
之后我可以发现服务和特征 - 一切看起来都不错。当我发现特征并读/写数据时我cancelPeripheralConnection
在 didDisconnect 中我重新连接到设备
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error
{
[central connectPeripheral:peripheral options:nil];
}
我也实施centralManager:willRestoreState:
like:
NSArray *peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey];
for (CBPeripheral *peripheral in peripherals) {
[central connectPeripheral:peripheral options:nil];
peripheral.delegate = nil;
}
在 plist 中。添加了所需的密钥App communicates using CoreBluetooth
.
目前,如果我连接到设备并终止它 - 它会自动重新启动并连接到设备 - 一切都好,但如果它再次终止 - 什么也不会发生。
另外,如果我从外围设备移出并回来-什么也没有发生。
Update
关于第 5 点 - 我的跌倒 - 应该使用这个键connectPeripheral
in 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
被调用时没有错误 - 这意味着(我认为)我这边做错了什么。也许你可以建议问题出在哪里
还有一个问题 - 将数据写入特征是否有一些限制?因为在苹果示例中它被设置为 20 字节。