Search code examples
androidbluetooth-lowenergydescriptorbluetooth-gatt

Read Data Structure value binded to Bluetooth LE Descriptor in Android


I am writing an android app which reads the data sent from an arm processor over BLE (with characteristics and descriptors). in the hardware part I am using the function below :

tBleStatus aci_gatt_add_char_desc   (   uint16_t    serviceHandle,
uint16_t    charHandle,
uint8_t     descUuidType, //16 or 128 bit
const uint8_t *     uuid, //descriptor uuid
uint8_t     descValueMaxLen,
uint8_t     descValueLen,
const void *    descValue, //actual data
uint8_t     secPermissions,
uint8_t     accPermissions,
uint8_t     gattEvtMask,
uint8_t     encryKeySize,
uint8_t     isVariable,
uint16_t *  descHandle 
)   

And if we pass descValue as string or well-known type value we can easily extract the value in the android side like below :

byte[] descValue = Descriptor.getValue();
String DescString = new String(descValue, "ISO_8859_1");  

or for float I use:

float wrapped = ByteBuffer.wrap(descValue).order(ByteOrder.LITTLE_ENDIAN).getFloat();

However, instead of string or any single data type I want to send a struct in C (for example Characteristic Presentation Format (0x2904 UUID):

typedef __packed struct _charactFormat {
uint8_t format;
int8_t exp;
uint16_t unit;
uint8_t name_space;
uint16_t desc;
} PACKED charactFormat;

But the problem is when we call getValue() function in Android it returns a byte array and I don't know how to interpret this byte array and extract the data out of it

I am looking for a way to send struct and not strictly looking for Char Pres. form. (This is possible since nRF connect app can read the data structure but the source code is not available (even in their nRF Toolbox)

Any help would be appreciated


Solution

  • I found my answer. Since the data is received at the android side using byte array we can translate each bite as long as we know the data structure. For example the first byte regarding format could be interpreted as int. if we have string (for example in case of adding string to above struct) our string will start from 8th byte or byte 7. So in the Android side we have to decode the byte from 7 to last one to string.