peripheral didWriteValueForCharacteristic: not updating value

When we:


[[self peripheral] writeValue:data forCharacteristic:self.cbCharacteristic type:CBCharacteristicWriteWithResponse];

Delegate is called with error == nil:

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


The characteristic's value (NSData) does not equal the written data. It is old data. Should it equal what we wrote?

We've worked around this by immediately calling:


[[self peripheral] readValueForCharacteristic:self.cbCharacteristic];


Delegate is called with error == nil:


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


Behold, the characteristic's value (NSData) equals the written data. This workaround causes more BLE traffic than is necessary. The CBCharacteristic's value is read-only, so we can not modify it to suit reality.


Thoughts?

I don't think that the data property actually gets refreshed until you read it.


A callback with a nil error would mean that your write was successful, if you want to keep track of what you wrote, either re-read the char or keep track of it manually.

After receiving the notification, you have to explicitly read the value using readValueForCharacteristic: per the Core Bluetooth Programming Guide:


"After you have successfully subscribed to a characteristic’s value, the peripheral device notifies your app when the value has changed. Each time the value changes, the peripheral calls the

peripheral:didUpdateValueForCharacteristic:error:
method of its delegate object. To retrieve the updated value, you can implement this method in the same way as described above in Reading the Value of a Characteristic."
peripheral didWriteValueForCharacteristic: not updating value
 
 
Q