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

时间:2014-05-04 20:11:18

标签: ios bluetooth core-bluetooth

我正在使用Apple编写的以下代码。

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

这是Apple编写的writeValue部分

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];
}

我添加了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]);
}   

didWriteValueForCharacteristic的打印输出如下:

Did write characteristic value : (null) with ID Unknown (<2a39>)
With error: (null)

我使用了两种不同的心脏监测器和Blue Light应用程序(模拟ble设备),但我得到了相同的结果。

为什么我为didWriteValueForCharacteristic的值和错误得到null?

1 个答案:

答案 0 :(得分:2)

您看到的是该特征的最后读取值。由于您以前从未阅读过它,因此它是nil。您需要使用readValueForCharacteristic:方法显式读取值以使其在中央更新。