我正在研究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設備,並試用此功能。 –