在Android中绑定到Bluetooth LE描述符的读取数据结构值

时间:2018-08-24 20:33:37

标签: android bluetooth-lowenergy descriptor bluetooth-gatt

我正在编写一个Android应用,该应用读取通过BLE(具有特征和描述符)从ARM处理器发送的数据。在硬件部分,我正在使用以下功能:

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 
)   

如果我们将descValue作为字符串或众所周知的类型值传递,我们可以很容易地在android端提取值,如下所示:

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

或者对于浮点数,我使用:

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

但是,我想用C发送一个结构而不是字符串或任何单一数据类型(例如,特征表示格式(0x2904 UUID):

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

但是问题是当我们在Android中调用getValue()函数时,它返回一个字节数组,而我不知道如何解释该字节数组并从中提取数据

我正在寻找一种发送struct的方法,而不是严格地寻找Char Pres。形成。 (这是可能的,因为nRF connect应用程序可以读取数据结构,但是源代码不可用(即使在其nRF工具箱中也是如此)

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我找到了答案。由于数据是在Android端使用字节数组接收的,因此只要我们知道数据结构,我们就可以转换每个位。例如,有关格式的第一个字节可以解释为int。如果我们有字符串(例如,在将字符串添加到上述结构的情况下),我们的字符串将从第8个字节或字节7开始。因此在Android端,我们必须将字节从7解码为字符串的最后一个。