I'm working on a product which sends data over Bluetooth to a custom app.
While I got the communication link working, I can't seem to find a solution for converting the raw data to a struct.
I'm sending data from an ESP32 over bluetooth, which sends a struct that looks like :
struct dataStruct {
float value = 0.45;
int temp = 23;
byte filler[250];
} dataRCV;
, puts it in a BLE characteristic and notifies the "client" (phone connected to it).
(The filler is a placeholder for future values)
The data is retrieved with characteristic.value!
, and gives me a 256 byte array I would like to convert back to a swift struct that would look like :
struct dataStruct {
var value: Float
var temp: UInt16
var filler: [UInt8]
}
I found some code snippets for converting structs to raw bytes, and some obscure ones to convert raw byte arrays of integers back to structs consisting only of UInt8's, but not for mixed types of data...
Is there any way to do that ? Or is there any way to do it any other way ?
EDIT :
The "256 byte array" comes directly from the data that is being sent from the ESP32 with pCharacteristic->setValue((byte*)&dataRCV, sizeof(dataRCV));
, and sizeof(dataRCV)
is equal to 256 (because I defined the struct that way).
When, in my swift code, I print(characteristic.value!)
, it just prints 256 bytes
in the console; and this data is what I want to convert back to a struct.
In C/C++ it's possible with memcpy as explained here, but I couldn't find any information on how it would be done in Swift, so I haven't really tried anything that got me closer to what I want to do yet.