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!!

1 Answers

1
Allen On

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