【发布时间】:2017-01-24 18:09:55
【问题描述】:
我有应用程序与 BLE 设备一起使用。我有特征值,但我无法将其转换为字符串。 例如,
value = <aa640400 0000001e 0f>
如何转成字符串?
【问题讨论】:
标签: ios objective-c core-bluetooth
我有应用程序与 BLE 设备一起使用。我有特征值,但我无法将其转换为字符串。 例如,
value = <aa640400 0000001e 0f>
如何转成字符串?
【问题讨论】:
标签: ios objective-c core-bluetooth
let dd = getCharacteristic.value!;
print(String(data: dd, encoding: String.Encoding.ascii)!);
【讨论】:
我使用了上面桑迪的答案并将其浓缩为
let data = (characteristic.value)!
let nsdataStr = NSData.init(data: (characteristic.value)!)
print("Raw: \(nsdaraStr)")
我想要做的就是从 BLE 外围设备(在本例中为 HR 监视器)获取原始值(十六进制)。 上面的输出给了我:
// This is the Decimal HRArray:[22, 64, 90, 3]
Raw: {length = 4, bytes = 0x16404e03}
下面这段代码基本上只是去掉了空格
let str = nsdataStr.description.trimmingCharacters(in:charSet).replacingOccurrences(of: " ", with: "")
制作
Raw: {length=4, bytes=0x16404e03}
我不需要。
【讨论】:
按照以下代码:
let data = (characteristic?.value)!
print(data) //-> It will be of type Data we need to convert Data to String
let charSet = CharacterSet(charactersIn: "<>")
let nsdataStr = NSData.init(data: (characteristic?.value)!)
let str = nsdataStr.description.trimmingCharacters(in:charSet).replacingOccurrences(of: " ", with: "")
print(str) // you will get String or Hex value (if its Hex need one more conversion)
【讨论】: