Reputation: 3417
i need an help. So i'm getting bytes from BLE device, and i need to get a battery level. This is returned by Uint8 Bytes. So my works code (with comment):
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("\n------DATASTRING------\n")
let data = characteristic.value ?? NSData() as Data
// let stringInt = String.init(data: data, encoding: String.Encoding.utf8) ?? ""
// let int = UInt32.init(stringInt)
// print(int ?? 000)
print(data as NSData)
// if data != nil {
// let dataString = String(data: data!, encoding: String.Encoding.utf8)
// print("\n------DATASTRING------\n")
// print(dataString ?? "")
// } else {
// print("\n------DATASTRING EMPTY------\n")
// }
}
data content:
as you can see i have number 86 (first bit of byte), this is the batter level, but i can't read it. So how can i get this integer data 86 from byte ? I saw online some solution that you can see in the comment code, but nothing works. I need an help i'm new of swift 5. Thanks.
Upvotes: 0
Views: 1129
Reputation: 539685
86 is the first byte (not the first bit) of the data. You get it with
let level = data[0]
(after checking that data
is not empty).
Upvotes: 1