What you see is the last read value of the characteristic. Since you never read it before, it is nil
. You need to explicitly read the value to get it updated on the central side with the readValueForCharacteristic:
method.
peripheral writeValue: forCharacteristic: type: return null error and value
-
15-07-2023 - |
Question
I am using the code below written by Apple.
Here is the writeValue section written by Apple
if ([aChar.UUID isEqual:[CBUUID UUIDWithString:@"2A39"]])
{
uint8_t val = 1;
NSData* valData = [NSData dataWithBytes:(void*)&val length:sizeof(val)];
[aPeripheral writeValue:valData forCharacteristic:aChar type:CBCharacteristicWriteWithResponse];
}
I added didWriteValueForCharacteristic
- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSLog(@"Did write characteristic value : %@ with ID %@", characteristic.value, characteristic.UUID);
NSLog(@"With error: %@", [error localizedDescription]);
}
The print out from didWriteValueForCharacteristic is the following:
Did write characteristic value : (null) with ID Unknown (<2a39>)
With error: (null)
I have used two different heart monitors and Blue Light app (simulates ble device) but I get the same outcome.
Why do I get null back for the value and error for didWriteValueForCharacteristic?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow