Question

I am using the code below written by Apple.

https://developer.apple.com/library/mac/samplecode/HeartRateMonitor/Listings/HeartRateMonitor_HeartRateMonitorAppDelegate_m.html#//apple_ref/doc/uid/DTS40011322-HeartRateMonitor_HeartRateMonitorAppDelegate_m-DontLinkElementID_4

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top