我正在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];