I have app what working with BLE device. I have characteristic value, but I can't convert it to string. For example,
value = <aa640400 0000001e 0f>
How convert it to string?
I have app what working with BLE device. I have characteristic value, but I can't convert it to string. For example,
value = <aa640400 0000001e 0f>
How convert it to string?
let dd = getCharacteristic.value!;
print(String(data: dd, encoding: String.Encoding.ascii)!);
I was using the answer from sandy above and condensed it to
let data = (characteristic.value)!
let nsdataStr = NSData.init(data: (characteristic.value)!)
print("Raw: \(nsdaraStr)")
All I wanted to do was to get the raw value (in hex) from the BLE peripheral (HR monitor in this case). The output of the above gives me:
// This is the Decimal HRArray:[22, 64, 90, 3]
Raw: {length = 4, bytes = 0x16404e03}
This code below basically just removes the white space
let str = nsdataStr.description.trimmingCharacters(in:charSet).replacingOccurrences(of: " ", with: "")
making it
Raw: {length=4, bytes=0x16404e03}
which I didn't need.
Follow below code:
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)