2

我正在 iOS 上使用 CoreBluetooth 构建蓝牙应用程序。每次启动应用程序时,用户都会收到一个 ID,该 ID 保存在 Peripheral 的 LocalNameKey 中,开始用它做广告并开始使用 CentralManager 搜索其他用户。每个用户都由他的本地名称标识,这很好。

使用 CentralManager,每个用户都可以将值写入另一个用户的 Peripheral 特征,并通知他们有关更改。这也行得通。问题发生在这里,在连接完成并且 Peripheral 执行了 didRecieveWriteRequests 方法之后,CBPeripheralManager 和 CBCentralMange 都被重置和重新初始化。之后,Peripheral 的 LocalNameKey 不再是我的特定 ID,而是 iPhone 的 Given Name 或(null)。它破坏了应用程序的整个想法,但我很确定它可以完成。

当我关闭并打开蓝牙时,它再次起作用。

这就是我在连接后清理 Central 的方式:

- (void)cleanup
{
// See if we are subscribed to a characteristic on the peripheral
if (self.peripheral.services != nil) {
    for (CBService *service in self.peripheral.services) {
        if (service.characteristics != nil) {
            for (CBCharacteristic *characteristic in service.characteristics) {
                if (characteristic.isNotifying) {
                        // It is notifying, so unsubscribe
                    [self.peripheral setNotifyValue:NO forCharacteristic:characteristic];

                        // And we're done.
                    return;
                }
            }
        }
    }
}
// If we've got this far, we're connected, but we're not subscribed, so we just disconnect
[self.manager cancelPeripheralConnection:self.peripheral];
}

这就是我重新初始化蓝牙的中央和外围设备的方式:

self.bluetoothPeripheral = nil;
self.bluetoothCentral = nil;
self.bluetoothPeripheral = [[BluetoothPeripheral alloc] initWithID:idNumber];
[self.bluetoothPeripheral loadManager];
self.bluetoothCentral = [[BluetoothCentral alloc] init];
4

0 回答 0