Android蓝牙LE-读取浮点特性

Android蓝牙LE-读取浮点特性,android,bluetooth-lowenergy,android-bluetooth,gatt,Android,Bluetooth Lowenergy,Android Bluetooth,Gatt,我正在尝试读取已连接蓝牙LE设备(Genuino 101)的浮点特征。出于测试目的,该装置提供了一个带有硬编码值“55.3”的浮动特性。虽然我能够接收到某种程度上类似于浮点的字符串,但我无法读取实际的浮点值。 下面是处理字符串的代码片段: // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); if (da

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

// 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());
        }
这是直接从android开发者主页上的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);
    }
}
这将导致输出

I/System.out: Data Received: 33]B 
I/System.out: 33 33 5D 42
因此,撇开交换的端点不谈,这是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);
                }
            }
        }
我犯了什么错?另外,我不确定应该如何理解Offset参数。它是以位、字节为单位的偏移量吗?从MSB计算,从LSB计算? getFloatValue()的文档还声明“返回给定偏移量处的特征的浮点缓存值,如果请求的偏移量超过值大小,则返回null。”。但是上面的代码片段大大超过了任何gatt特性的最大大小,但是方法调用并没有返回“null”,而是抛出异常。
那么,在这里获得浮点值的正确方法是什么呢?

目前,我通过使用

 float f1 = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getFloa‌​t();

目前,我通过使用
float f1=ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getFloat()格式化数据来帮助自己虽然这是一个“快速且肮脏的攻击”,我更愿意使用专用的getFloatValue()方法。如果您尝试将一个字节解析为int?您将在I上循环,但每个循环都会得到相同的值,因为characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT\u FLOAT,0)中的偏移量始终设置为0。试着把它改为“我”。谢谢你指出,@Nebr,这个错误确实漏掉了。但这并没有解决我的问题,我相应地更新了我的问题。@Daniel,不确定你的建议是什么。您的意思是使用BluetoothGattCharacteristic.getIntValue()方法?那听起来像是更糟糕的黑客行为。
 float f1 = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getFloa‌​t();