java - 从 BLE 设备读取/写入自定义特性

标签 java android bluetooth-lowenergy uuid

我正在尝试使用 Android Studio 作为 IDE 并使用 Java 作为编程语言与温度计 BLE 设备进行交互。在我的智能手机上使用一个应用程序,我发现了这个设备在其运行过程中公开的服务:有很多通用服务/特性和一个自定义服务。

首先我试着阅读

  • 健康体温计服务(UUID = 00001809-0000-1000-8000-00805F9B34FB)
  • 温度测量特性(UUID = 00002A1C-0000-1000-8000-00805F9B34FB)[标记为指示]

从服务列表中恢复特征并访问其描述符:

BluetoothGattCharacteristic temp_char = mBluetoothGattServiceList.get(2).getCharacteristics().get(0); 
for (BluetoothGattDescriptor descriptor : temp_char.getDescriptors()) {
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}
mBluetoothGatt.setCharacteristicNotification(temp_char, true);

在这种情况下,我可以在 onCharacteristicChanged 回调中看到测量结果:

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
float char_float_value = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_FLOAT, 1);
}

但是,在设备随附的文档中,提示按照 GATT 连接到仪表:

  • 自定义服务(UUID = 00001523-1212-EFDE-1523-785FEABCD123)
  • CUSTOM 特征(UUID = 00001524-1212-EFDE-1523-785FEABCD123)[在智能手机应用中标记为 WRITE/INDICATE/NOTIFY)
  • 描述符(UUID = 00002902-0000-1000-8000-00805F9B34FB 在智能手机应用中标记为已读)

并列出几个 8 字节的命令发送到仪表等待 8 字节的响应。使用这种格式的帧发送命令

[0x51 CMD Data_0 Data_1 Data_2 Data_3 0xA3 CHK-SUM]

并且响应具有相同的响应,但有一些细微差别。

我可以使用 gatt.writeCharacteristic 发送帧,但我无法接收响应帧,始终将 0x01 0x00 作为仪表的唯一答案(2 字节而不是 8 字节)。

这是我的做法:

    BluetoothGattCharacteristic custom_char = mBluetoothGattServiceList.get(5).getCharacteristics().get(0);                mBluetoothGatt.setCharacteristicNotification(custom_char, true);
    for (BluetoothGattDescriptor descriptor : custom_char.getDescriptors()) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
    byte[] req_frame = new byte[8];
    req_frame[0] = (byte) 0x51;
    req_frame[1] = (byte) 0x24;
    req_frame[2] = (byte) 0x0;
    req_frame[3] = (byte) 0x0;
    req_frame[4] = (byte) 0x0;
    req_frame[5] = (byte) 0x0;
    req_frame[6] = (byte) 0xA3;
    req_frame[7] = (byte) 0x18;
    custom_char.setValue(req_frame);
    mBluetoothGatt.writeCharacteristic(custom_char);

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS {
    mBluetoothGatt.readCharacteristic(characteristic);
        }
    }

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    System.out.println("[onCharacteristicRead] status : " + status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "[onCharacteristicChanged] " + ByteArrayToString(characteristic.getValue()));
    }
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    byte[] response = characteristic.getValue();
    Log.d(TAG, "[onCharacteristicChanged] " + ByteArrayToString(response));
    }
}

唯一未触发的回调是 OnCharacteristicRead,我想我会在其中找到帧响应。

我在通信协议(protocol)中犯了一些错误?我怎样才能收到 8 字节的响应帧?

提前致谢!

最佳答案

您的错误是您一次只能进行一项未完成的 Gatt 操作。在发送下一个之前,您必须等待回调。参见 Android BLE BluetoothGatt.writeDescriptor() return sometimes false了解更多信息。

关于java - 从 BLE 设备读取/写入自定义特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54050918/

相关文章:

java - 如何在 Intellij Idea-2017.2.5 中更改文件创建评论日期格式?

java - 在字符串中显示带有图像位置的图像

android - Robolectric 不支持 API 级别 28

android - 我应该创建单独的 Activity 来检查用户是否登录?

java - 需要sql语句支持搜索数据库,一直出现no database shown的错误提示

java - 为什么 IllegalAccessError 没有双参数构造函数?

java - 使用 OpenGL ES 3.0 渲染三角形

Android 的 BLE 服务发现 (BluetoothGatt#discoverServices()) 和低功耗与 BR/EDR

java - 如何在后台和前台运行一个函数 Android Studio(JAVA)

java - Java 中的低功耗蓝牙 API