didWriteValueForCharacteristic将characteristic.value返回为null ios

时间:2014-11-10 06:13:20

标签: ios objective-c core-bluetooth

我使用[peripheral writeValue:startFrame forCharacteristic:cbWriteCharacteristic type:CBCharacteristicWriteWithResponse];编写数据,调用“ didWriteValueForCharacteristic ,如下所示”

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{ 
    NSLog(@"characteristic.value %@",characteristic.value);
    NSLog(@"Write Data characteristic.value %@",characteristic.value);

    CBCharacteristic * cbReadCharacterstic = [self getCharacteristicWtihCBUUID:MHSW_READ_CHARACTERISTIC_ID];
    [peripheral setNotifyValue:TRUE forCharacteristic:cbReadCharacterstic];

}

当我在写入方法中打印“ characteristic.value ”的值时,它返回null。 相反,它应该给出要发送的数据的价值。当我在写方法中打印特征值时: - “<CBCharacteristic: 0x166d0, UUID = D0F0-9DD7-7047, properties = 0x8, value = (null), notifying = NO>” 值在write方法本身中显示为null。为什么会这样呢?

我该怎么办才能解决。提前谢谢。

1 个答案:

答案 0 :(得分:5)

didWriteValueForCharacteristic的调用通知您已完成写入响应(即已收到响应)。它没有告诉你关于该特征值的任何信息,所以nil是正确的值。

在执行写入和接收响应之间,特征值可能已经发生了变化。

如果您想要特征的当前值,则需要发出读取请求(或通过通知订阅值更改)。