ios - BLE后台重连

标签 ios objective-c background cbcentralmanager bluetooth-lowenergy

我想在设备被用户或系统移出/终止/在后台模式下重启后重新连接到 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
                                                                }];

之后我可以发现服务和特征 - 一切看起来都不错。当我发现特征并读/写数据时,我 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 中。添加了所需的 key App communicates using CoreBluetooth .

目前,如果我连接到设备并终止它 - 它会自动重新启动并连接到设备 - 一切正常,但如果它再次终止 - 没有任何反应。

此外,如果我从外围设备中移出并返回 - 什么也没有发生。


更新

关于第 5 点 - 我的秋天 - 应该将此 key 与 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 State Preservation and Restoration 根本无法可靠地工作。你可以让它工作得有点“好”,但你永远无法让它可靠地重新连接,除非 Apple 有朝一日修复它。

话虽如此,我想在您的设置中注意一些事项。

1) 在 centralManager:willRestoreState: 中,您只能检索在应用程序终止时进行过任何通信的外围设备。这意味着您还应该实现 centralManagerDidUpdateState:,如果状态是 CBCentralManagerStatePoweredOn,那么您可以使用 retrievePeripheralsWithIdentifiers: 方法来检索其他外设和也重置他们的代表。这当然意味着您必须将外围设备标识符存储在应用程序中的某个位置。还记得在这里重置挂起的连接。

2) 您在 centralManager:willRestoreState: 中将委托(delegate)设置为 nil!所以即使它确实连接了你也不会知道它 i:P

3) 只有当应用程序被系统终止时,您的应用程序才会重新启动。如果您从应用程序列表中手动滑动杀死它,它将不会重新启动。不幸的是,如果设备重新启动,它也不会重新启动。

4) CBConnectPeripheralOptionNotifyOnConnectionKey 在使用 bluetooth-central 后台模式时不是必需的,而且对用户来说很烦人,所以我不会使用它。

5) CBCentralManagerRestoredStatePeripheralsKeyCBCentralManagerRestoredStateScanServicesKeyCBCentralManagerRestoredStateScanOptionsKey 不是有效的初始化选项,所以我不明白你为什么要使用这些..

5) 如果在应用程序终止时蓝牙切换状态,则所有挂起的连接都将丢失,您将不会重新启动以了解它。这本身就有效地意味着状态恢复是相当无用的。

无论如何,我很难过地说,但如果你正在开发一个必须依赖于在后台重新连接的外围设备的应用程序,那么我不建议这样做。你会感到沮丧。我可能会写一篇关于 Core Bluetooth 中 Apple 不想修复的所有错误的文章。更可怕的是,你可以很容易地通过一个应用程序破坏设备上全局的蓝牙连接,这样在设备重新启动之前,任何应用程序都无法使用蓝牙。这非常糟糕,因为它违背了 Apple 自己的沙盒原则。

如果您需要更多帮助,请告诉我!

/一个

关于ios - BLE后台重连,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34290967/

相关文章:

ios - SceneKit 自定义几何

html - 页眉和页脚背景不会拉伸(stretch)页面的整个宽度

java - 为什么这不显示我的背景图片 title.png?

iphone - NSString 中的撇号和反撇号

ios - 如何以编程方式在 iOS 10 上的 Objective-C 中打开 WIFI 设置

ios - 如何使用 sharedApplication 在 ios 6 苹果 map 中显示位置

iphone - “html agility pack” 类似 C/Objective-c/iPhone 的解决方案

google-chrome - 固定背景图像时,chrome 中的滚动不连贯

ios - Xcode 和 Sprite-builder 中的游戏编程。电脑显示 "thinks"

导航栏中的ios标题和副标题居中