Android 蓝牙 LE - 读取浮点特性

Unb*_*ger 5 android bluetooth-lowenergy android-bluetooth gatt

我正在尝试读取连接的蓝牙 LE 设备(Genuino 101)的浮动特性。出于测试目的,设备提供带有硬编码值“55.3”的 FloatCharacteristic。虽然我能够收到一个以某种方式类似于浮点数的字符串,但我无法读取实际的浮点值。
这是处理字符串的代码片段:

// For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
        }
Run Code Online (Sandbox Code Playgroud)

这是直接从android 开发者主页的https://developer.android.com/samples/BluetoothLeGatt/index.html BLE 演示项目复制的。然后此代码段处理意图:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Broadcast received");
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {

        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {

        } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {

        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
           displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }
    }
};

private void displayData(String data) {
    if (data != null) {
        System.out.println("Data Received: " + data);
    }
}
Run Code Online (Sandbox Code Playgroud)

这导致输出

I/System.out: Data Received: 33]B 
I/System.out: 33 33 5D 42
Run Code Online (Sandbox Code Playgroud)

因此,撇开交换的字节序,这是 55.3f 的正确十六进制值。
但是,如果我尝试使用characteristic.getFloatValue(),我只会得到垃圾。这是我尝试弄清楚如何接收实际浮点数的方法:

final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            for (int i = 0; i< 333; i++) {
                try {
                    final float fData = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_FLOAT, i);
                    System.out.println("Offset = "+ i + ". Data that gets sent: " + fData + "/Data that we would expect: " + 55.3f);
                } catch (Exception e) {
                    System.out.println("Exception at offset " + i);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

输出总是

I/System.out: Offset = 0. Data that gets sent: Infinity/Data that we would expect: 55.3
I/System.out: Exception at offset 1
I/System.out: Exception at offset 2
...
Run Code Online (Sandbox Code Playgroud)

我在这里有什么错误?另外,我不确定应该如何理解 Offset 参数。它是以位为单位的偏移量,以字节为单位吗?从 MSB 开始计数,从 LSB 开始计数?此外,getFloatValue() 的文档声称“如果请求的偏移量超过值大小,则返回浮点 - 给定偏移量处特征的缓存值或 null。”。但是上面的代码片段严重超过了任何 gatt 特征的最大大小,但方法调用抛出异常而不是返回 'null'。那么在这里获得浮点数的正确方法是什么?

Unb*_*ger 4

目前,我通过使用以下命令格式化数据来帮助自己

\n\n
 float f1 = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getFloa\xe2\x80\x8c\xe2\x80\x8bt();\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n