Corebluetooth 为特征写入十六进制值

Corebluetooth write Hexadecimal Value for Characteristic

我正在尝试为特征写入十六进制值 NSString * const SET_BUZZER_ON = @"0xef";,同时调用方法 didDiscoverCharacteristicsForService,然后我检查我是否在正确的特征中:

NSData* valData = [self dataFromHexString:SET_BUZZER_ON];
[aPeripheral writeValue:valData forCharacteristic:aChar type:CBCharacteristicWriteWithResponse];

代码执行没有问题,但是当调用方法 didWriteValueForCharacteristic 时,出现错误:

Error writing characteristic value: The value's length is invalid.

有什么想法吗?

我刚刚弄明白了,我不得不用 Bytes Array 初始化 NSData 而不是 NSString:

const uint8_t bytes[] = {0xef};
NSData *valData = [NSData dataWithBytes:bytes length:sizeof(bytes)];