I am using CoreBluetooth library in Swift and I want to read what am I receiving from peripheral in Characteristic. When I convert characteristic's value to NSString it returns nil always. Do you know why ? I think is because of encoding, because I have a Characteristic where I can read and write and when I write something to it I can Read what I wrote. When I write the value of Characteristic is 36 if I write 6 and this code works. If I only want to Read a characteristic the code does not work.
This is what my characteristic contains
<CBCharacteristic: 0x1700a2a60, UUID = FFF2, properties = 0x2, value = <02>, notifying = NO>
And this is the code
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if error != nil {
print("Error on updating value on characteristic: \(characteristic) - \(String(describing: error?.localizedDescription))")
return
}
print(characteristic.value!)
print(NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue)!). //HERE IS NULL
guard let stringFromData = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) else
{
print("Invalid data")
return
}
//ALSO HERE stringFromData IS NULL
}