Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Indications on Client Configuration descriptor from iOS8

I am trying to get the notification from a bluetooth device upon the characteristic value change. For this I need to enable notification for Client Characteristic Configuration(CCC) descriptor. I have used setNotifyValue(enabled: Bool, forCharacteristic characteristic: CBCharacteristic) for the characteristic but not getting the update for value changes.

I tried to enable the indication for CCC using writeValue(data: NSData, forDescriptor descriptor: CBDescriptor) but my app crashes for this API and shows the error as

Cannot write Client Characteristic Configuration descriptors using this method!

Any help!!

like image 401
Vikas Goyal Avatar asked Nov 02 '15 08:11

Vikas Goyal


1 Answers

Provide more codes might help to imporove accuracy of an answer; however, let's be assuming you have already been able to discover all characteristic values. Usually you just need to iterate all characteristics and set/write value according to each Client Characteristic Configuration(CCC) descriptor in CBPeripheralDelegate implementation.

An example is attached below:


- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if (error) {
        NSLog(@"Error discovering characteristics: %@", error);
        return;
    }

    for (CBCharacteristic *characteristic in service.characteristics) {

        if ([characteristic.UUID isEqual:[CBManager accelDataUUID]]) {
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        } else if ([characteristic.UUID isEqual:[CBManager accelConfigUUID]]) {
            [peripheral writeValue:[CBManager switchData:YES]
                 forCharacteristic:characteristic
                              type:CBCharacteristicWriteWithResponse];
        }
        //... if you have more to iterate
    }
}
like image 183
Allen Avatar answered Sep 21 '22 21:09

Allen