Android Question union struct convert to byte array


Licensed User
Longtime User

with serial bluetooth connection I try to transfer information to a microcontroller. Best would be to have same structure of data to work on both sides.

For ex. I have following struct programmed in C language at microcontroller side:
typedef struct
unsigned int ID;
unsigned char len;
unsigned char B[8];
unsigned int W[4];
} Data;
} CANMessageDef;

Inside b4a I try following structure:
Type CanMsgType (ID As Short,  len, Data(8) As Byte)
Dim myTypeVar As myType

How can I send this via blue to have a byte stream with only valid bytes to copy direct at receiving side into the stucture?
Data may have length from 0 to 8 bytes. All data should be byte aligned. I thought ConvertObjectToBytes will work for this, but wrong way!

Is there any method to create union like data structures?


B4X founder
Staff member
Licensed User
Longtime User
The structure will be formatted differently on different platforms.
If you were using B4R to program the microcontroller then you could have used B4RSerializator.

You need to use RandomAccessFile to write to the data to an array (RAF.Initialize3) and then send the array. You will need to read the data on the microcontroller and build the structure.
Upvote 0