我正在尝试使用Android Studio作为IDE和Java作为编程语言与温度计BLE设备进行交互。通过智能手机上的应用程序,我发现了该设备在运行过程中公开的服务:有很多通用服务/特征以及一个自定义服务。
首先,我尝试读取健康温度计服务(UUID = 00001809-0000-1000-8000-00805F9B34FB)和温度测量特征(UUID = 00002A1C-0000-1000-8000-00805F9B34FB) [标记为INDICATE]。
从服务列表中恢复特征并访问其描述符:
在这种情况下,在onCharacteristicChanged回调中,我可以看到测量结果:
然而,在设备附带的文档中,提示通过以下GATT连接到计量表:
我可以使用gatt.writeCharacteristic发送帧,但无法接收响应帧,总是从计量表获取0x01 0x00作为唯一答案(仅为2字节而不是8字节)。
这是我的操作步骤:
首先,我尝试读取健康温度计服务(UUID = 00001809-0000-1000-8000-00805F9B34FB)和温度测量特征(UUID = 00002A1C-0000-1000-8000-00805F9B34FB) [标记为INDICATE]。
从服务列表中恢复特征并访问其描述符:
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连接到计量表:
- CUSTOM服务(UUID = 00001523-1212-EFDE-1523-785FEABCD123)
- CUSTOM特征(UUID = 00001524-1212-EFDE-1523-785FEABCD123)[在智能手机应用程序中标记为WRITE/INDICATE/NOTIFY]
- 描述符(UUID = 00002902-0000-1000-8000-00805F9B34FB,在智能手机应用程序中标记为READ)
[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,我想在那里找到帧响应。
我在通信协议中犯了一些错误吗?如何接收8字节的响应帧?
提前感谢!