I am trying to retrieve data from two BLE characteristics values. They both contain structures, and I want to save this data as structures in my application too. Data from first BLE characteristic is all Int16, I was able to save it. But the problem is that second characteristic contains both Int8 & Int16, and I can't find a solution to deal with it.
In my application, I have created two different structures for both characteristics. The first structure looks like this, it's all Int16.
struct FirstStruct {
let a1: Int16
...
let a6: Int16 }
The second is like that, has 2 types:
struct SecondStruct {
let b1: Int16
let b2: Int8
let b3: Int8
}
When I get data from characteristic.value for first characteristic, I am able to simply do it like this:
data = characteristic.value
let firstData = data.withUnsafeBytes {(int16Ptr: UnsafePointer<Int16>)-> FirstStruct in
FirstStruct(a1: Int16(littleEndian: int16Ptr[0]),
...
a6: Int16(littleEndian: int16Ptr[5]))
}
But how can I do it for the 2nd characteristic? Same way doesn't work, because it has both Int16 & Int8, results in error. I guess, I could try to interpret all values as Int16 and then convert to Int8? But this is probably a bad idea? Is there other way to extract the data from second characteristic and put it into my desired structure?