peripheral writeValue: forCharacteristic: type: return null error and value

一世执手 提交于 2019-12-07 11:47:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23461061/peripheral-writevalue-forcharacteristic-type-return-null-error-and-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!