0

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;
}
manish
  • 109
  • 1
  • 11
  • Could it be that some services/characteristics access (read/write) don't need pairing? And you try to access them before the one that trigger the pairing pop-up? – Larme Dec 18 '15 at 15:17
  • can you please tell me any call back after selection of pairing options. – manish Dec 18 '15 at 15:20
  • What do you mean by pairing request? iOS Core Bluetooth uses bluetooth low energy. A peripheral advertises its presence while a central connects to and explores peripheral info. – Wallace Campos Dec 18 '15 at 15:57
  • Yes Right, but when i select "Pair" option then only it should start communicating. But, in my case it start communicating even i don't select any thing and keep waiting for few 15- 20 seconds. – manish Dec 18 '15 at 16:13
  • Can you add more code? Such as code that initialize your central manager and scan for peripherals? – Wallace Campos Dec 18 '15 at 16:24
  • intializing central manager: if (!self.centralManager) { self.discoveredPeripheral = nil; _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } for scanning,[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @NO }]; – manish Dec 21 '15 at 04:31

1 Answers1

2

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

wj2061
  • 6,778
  • 3
  • 36
  • 62
  • Thanks! but i am still getting same problem, the characteristic which create problem i am setting the permission in this way in peripherel side... but its read/write before accepting the pairing request. self.deviceNameCharacterestics = CBMutableCharacteristic(type: CBUUID(string: UUID.getDeviceName), properties: [CBCharacteristicProperties.Notify, CBCharacteristicProperties.Read,CBCharacteristicProperties.NotifyEncryptionRequired], value: nil, permissions: [CBAttributePermissions.ReadEncryptionRequired,CBAttributePermissions.Writeable, CBAttributePermissions.Readable]) – manish Dec 21 '15 at 04:26
  • because this characteristic has to write by central device and it should appear in Peripheral deice. – manish Dec 21 '15 at 05:09
  • then just set it to [CBAttributePermissionsReadEncryptionRequired, CBAttributePermissionsWriteEncryptionRequired], – wj2061 Dec 21 '15 at 05:12
  • Can you please suggest one more thing, like how can start communication in such a way like only one central device can be connected to peripheral device. In my case one peripheral device some time connects to multiple central device and it start communicating too.. – manish Dec 21 '15 at 07:02
  • I don't get it ,for BLE devices ,one central can connect to many peripherals at the same time.but one peripheral can only connect to one central . – wj2061 Dec 21 '15 at 07:06
  • in my case one peripheral able to receive write request from multiple central. can u suggest something here. – manish Dec 30 '15 at 11:37