OGeek|极客世界-中国程序员成长平台

标题: ios - Corebluetooth 为特征写入十六进制值 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:44
标题: ios - Corebluetooth 为特征写入十六进制值

我正在尝试为一个特性编写十六进制值 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.

有什么想法吗?



Best Answer-推荐答案


我刚刚想通了,我必须用 Bytes Array 而不是 NSString 来初始化 NSData:

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

关于ios - Corebluetooth 为特征写入十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694342/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4