简体   繁体   English

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

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

I have some problem with bluetooth connection as i am communicating with one device to other following CBCentral and CBPeripherel concepts. 蓝牙连接时出现问题,因为我正在按照CBCentral和CBPeripherel概念与一台设备进行通讯。 But, Peripherel device gets connected to Central before accepting pairing request pop up, basically central passes some data to peripheral before accepting pairing request. 但是,Peripherel设备在接受配对请求之前就已连接到Central,基本上Central在接受配对请求之前将一些数据传递给外围设备。

-- (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;
}

The problem is not in your APP,it's in your BLE Device . 问题不在您的APP中,而是在BLE设备中。
BLE Device's data can be written and readed without paired.It's totally fine. BLE设备的数据可以不成对写入和读取,完全可以。
A pair request is launched only if you try to access Encryption character's data,which is configured in peripheral side like this: 仅当您尝试访问加密字符的数据时才启动配对请求,这是在外围设备侧配置的,如下所示:

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

So in your case ,some of your peripheral's characters need pair,the others don't. 因此,在您的情况下,外围设备的某些字符需要配对,而其他字符则不需要。

you can see more in CorebluetoothGuide 您可以在CorebluetoothGuide中看到更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM