Android Bluetooth LE - 读取 float 特性

标签 android bluetooth-lowenergy android-bluetooth gatt

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

// 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());
        }

这是直接从 https://developer.android.com/samples/BluetoothLeGatt/index.html 复制的来自 android 开发者主页的 BLE 演示项目。 然后由这个 fragment 处理 Intent :

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(),我只会得到垃圾。以下是我尝试弄清楚如何接收实际 float 的方法:

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);
                }
            }
        }

输出总是

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
...

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

最佳答案

目前,我通过使用

格式化数据来帮助自己
 float f1 = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getFloa‌​t();

.

关于Android Bluetooth LE - 读取 float 特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42113986/

相关文章:

android - 从应用程序打开已安装的应用程序

java - 安卓 : Adding space in between tab layout

java - Android BLE BluetoothAdapter.LeScanCallback scanRecord 长度模糊

bluetooth - 是否有通过 BLE 发送 GPS 导航的标准方法?

android - Bluetooth LE 扫描有时找不到设备

java - 在具有以下 api 21 的设备上运行时,CardView 不显示

android - 如何从 Android Studio 中删除 .aar 文件

ios - 从 CBCentralManagerDelegate 回调以在 IOS8 上检索 CBPeripheral

java - 当 String 是有效的 char 时将 String 转换为 char

audio - 谁能解释语音命令如何通过 Android(Nexus 播放器)中的蓝牙 Remote (Nexus 播放器 Remote )工作?