0

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?

Alice A.
  • 85
  • 1
  • 7
  • Does this help https://stackoverflow.com/questions/39390211/how-to-convert-nsdata-to-multiple-type-ints ? – Martin R Jun 08 '17 at 15:13

1 Answers1

0

May be this will help you:

let a:Int8 = 1
let b:Int16 = 1
let aSize = MemoryLayout.size(ofValue:a)
print(aSize) // will return 1
let bSize = MemoryLayout.size(ofValue:b)
print(bSize) // will return 2

Using this code, you can check the size of received values.

Serhii Didanov
  • 2,200
  • 1
  • 16
  • 31