简体   繁体   中英

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. But, Peripherel device gets connected to Central before accepting pairing request pop up, basically central passes some data to peripheral before accepting pairing request.

-- (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 .
BLE Device's data can be written and readed without paired.It's totally fine.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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