CoreBluetooth writeValue:forDescriptor: issue
Asked Answered
B

2

8

My CoreBluetooth application need to enable the "indication bit" in Client Characteristic Configuration descriptors. Here is what I did:

  1. Start to scan
  2. Start to connect to the device
  3. Call discoverServices
  4. Call discoverCharacteristics inside the callback

    -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

  5. Call discoverDescriptorsForCharacteristic inside callback

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

  6. Inside callback

    -(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

I called:

        if ( [[descriptor.UUID representativeString] isEqualToString:@"2902" ] )
        {
            const unsigned char raw_data[] = {0x02};
            NSData *myData = [NSData dataWithBytes: raw_data length: 2];
            [self.cBCP writeValue:myData forDescriptor:descriptor];
        }

But My app crashes in writeVale: . The error message in console is :

Cannot write Client Characteristic Configuration descriptors using this method!

Any idea? Thanks

Banded answered 26/11, 2012 at 8:31 Comment(4)
I'm wondering if there is any sample code using writeVale:forDescriptor.Banded
Actually my solution is not use writeValue:forDescriptor.Banded
Can you explain how you how to enable indication without using writeValue:forDescriptor? I'm stuck at the same place...Boreas
Did you get any solution here ?Rundown
C
1

Pretty old question, but since it wasn't answered, seems like if the method setNotifyValue(_:for:) will handle that for you, it depends on the charcateristics properties below:

  • Notify only: Notifications will be enabled.
  • Indicate only: Indications will be enabled.
  • Indicate & Notify: ONLY Notifications will be enabled.

So if you need to enable indications, the characteristic must only have the indicate property, and notify should be removed.

I guess the main reasoning behind that is indications are much slower, so iOS will always prefer the fastest possible option unless it's a requirement to indicate.

Read more on the Apple docs

Cochin answered 13/7, 2017 at 8:29 Comment(0)
A
0

The problem is that you cannot use the writeValue:forDescriptor: method to write the value of a Client Configuration Descriptor (UUID = 2902) as you did.

Instead, you should use the setNotifyValue:forCharacteristic: method of the CBPeripheral class to configure client indications or notifications of a characteristic’s value on a server.

Acupuncture answered 31/8, 2013 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.