我正在研究bluetooth iOS
项目,并设法从bluetooth
设备获取一些数据。 但是,我正在努力将这些数据转换为一些有用的数据,例如NSString
。每当我尝试NSLog
NSString
从NSData
转换而来,它是一堆gibberish
。输出是:将NSData转换为NSString返回随机字符
ēဥ၆䄀
蓝牙设备是在亚洲制造商心脏监护,他们提供了关于如何使该设备的调用协议引用。他们在协议中提到的一件事:
The PC send 16-byte packets to the device, then the device sent back the 16-byte packets. Except for some special commands, all others can use this communication mode.
谁能告诉我我做错了什么?我尝试了我所知道的一切,包括苹果文档中的每一种编码以及initWithData
和initWithBytes
。谢谢!
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if (error)
{
NSLog(@"erorr in read is %@", error.description);
return;
}
NSData *data= characteristic.value;
NSString *myString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF16StringEncoding];
NSLog(@"Value from device is %@", myString); //OUTPUT IS ēဥ၆䄀
}
“16位数据包”应该代表人类可读的文本吗? – Joni
@Joni我相信这是。我正在尝试读取心率监视器上的值。还有其他建议吗?谢谢 – Teddy13
Joni提到:你不会说这些16字节数据包的规格是什么。大多数BT协议不通过无线电发送HR数据,因为这会浪费大量时间。这16个字节可能是4个32位双变量(IEEE 754)等等。您可能可以通过串行控制台(TeraTerm,CoolTerm)链接到此BT设备,并试用此功能。 –