iOS 蓝牙 : central- peripheral got connected before accepting pairing request

标签 ios iphone bluetooth cbcentralmanager

我在使用一台设备与其他设备通信时遇到了一些蓝牙连接问题,这些设备遵循 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设备的数据无需配对即可写入和读取。完全可以。
仅当您尝试访问加密字符的数据时才会启动配对请求,该数据在外围端配置如下:

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

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

你可以在CorebluetoothGuide中看到更多

关于iOS 蓝牙 : central- peripheral got connected before accepting pairing request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358639/

相关文章:

c++ - Serial.Read() 代码

reactjs - React Native - 全局事件监听器

ios - 从 JSON 响应中解析

ios - iPhone X 键盘 Controller 背景色

ios - 支持 IOS4 & IOS 5 和 IOS6 的 Twitter 集成

iphone - 如何在iPhone中创建钢琴UI?

iphone - 界面方向 iOS

ios - 如何在不使用 coreBluetooth 的情况下接近 BT 设备列表?

iphone - CGImageCreateWithImageInRect 没有正确裁剪

IOS Facebook 好友邀请在 facebook sdk 中不起作用