iOS 蓝牙:中央外围设备在接受配对请求之前已连接

iOS Bluetooth: central- peripheral got connected before accepting pairing request

我在使用一台设备与另一台设备通信时遇到蓝牙连接问题,遵循 CBCentral 和 CBPeripherel 概念。 但是,外围设备在接受配对请求弹出之前连接到中央,基本上中央在接受配对请求之前将一些数据传递给外围设备。

-- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

{

for (CBCharacteristic *characteristic in service.characteristics)
{

    NSLog(@"for loop for charactersitc %@",characteristic.UUID);
    // And check if it's the right one
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_SLIDEINDEX_CHARACTERISTIC_UUID]])
    {
        // If it is, subscribe to it
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_SCROLL_CHARACTERISTIC_UUID]])
    {
        // If it is, subscribe to it
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }

    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_FULLSCREEN_CHARACTERISTIC_UUID]])
    {
        // If it is, subscribe to it
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:SLIDELENGTH_CHARACTERISTIC_UUID]])
    {
        // WRITE VALUES TO REMOTE
        NSInteger length = self.presentationDataSourceArray.count;
        NSData *chunk = [NSData dataWithBytes: &length length: sizeof(length)];

        [self.discoveredPeripheral writeValue:chunk forCharacteristic:characteristic
                                         type:CBCharacteristicWriteWithResponse];
        [self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];

    }
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:DEVICENAME_CHARACTERISTIC_UUID]])
    {
        NSString *deviceName = [[UIDevice currentDevice]name];
        NSData *chunk = [deviceName dataUsingEncoding:NSUTF8StringEncoding];
        [self.discoveredPeripheral writeValue:chunk forCharacteristic:characteristic
                                         type:CBCharacteristicWriteWithResponse];
        [self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];

        NSLog(@"Sending Device Name");
    }
    if ([characteristic.UUID isEqual: [CBUUID UUIDWithString:CURRENTSLIDE_CHARACTERISTIC_UUID]])
    {
        self.currentSlideIndexCharacterestics = (CBMutableCharacteristic*) characteristic;
        NSInteger count = self.currentPage;
        NSData *chunk = [NSData dataWithBytes: &count length: sizeof(count)];

        [self.discoveredPeripheral writeValue:chunk forCharacteristic:characteristic
                                         type:CBCharacteristicWriteWithResponse];
        [self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
    }


    _isRemoteConnected = YES;
}

问题不在您的应用程序中,而是在您的 BLE 设备中。
BLE Device 的数据可以在没有 paired.It 的情况下写入和读取完全没问题。
仅当您尝试访问加密字符的数据时才会启动配对请求,该数据在外围端配置如下:

emailCharacteristic = [[CBMutableCharacteristic alloc]
    initWithType:emailCharacteristicUUID
    properties:CBCharacteristicPropertyRead
    | CBCharacteristicPropertyNotifyEncryptionRequired
    value:nil permissions:CBAttributePermissionsReadEncryptionRequired];

所以在你的情况下,你的一些外围设备的角色需要配对,其他的则不需要。

你可以在CorebluetoothGuide

中看到更多