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!!
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With