简体   繁体   English

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

[英]Read/Write custom characteristic from BLE device

I'm trying to interact with a temperature meter BLE device using Android Studio as IDE and Java as programming language.我正在尝试使用 Android Studio 作为 IDE 和 Java 作为编程语言与温度计 BLE 设备进行交互。 Using an app on my smartphone I discovered the services that this device exposes during its functioning: there were a lot of generic services/characteristic and one custom service.使用智能手机上的应用程序,我发现了该设备在其运行期间公开的服务:有很多通用服务/特性和一个自定义服务。

First of all I tried to read the首先,我尝试阅读

  • HEALTH THERMOMETER service (UUID = 00001809-0000-1000-8000-00805F9B34FB)健康温度计服务(UUID = 00001809-0000-1000-8000-00805F9B34FB)
  • TEMPERATURE MEASUREMENT characteristic (UUID = 00002A1C-0000-1000-8000-00805F9B34FB) [marked as INDICATE]温度测量特性 (UUID = 00002A1C-0000-1000-8000-00805F9B34FB) [标记为 INDICATE]

recovering the characteristic from the list of services and accessing to its descriptors:从服务列表中恢复特征并访问其描述符:

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

In this case, I can see the result of the measurement in the onCharacteristicChanged callback :在这种情况下,我可以在 onCharacteristicChanged 回调中看到测量结果:

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

However, in the documentation that came with the device it is hinted to connect to meter by following GATT :但是,在设备随附的文档中,提示按照 GATT 连接到仪表:

  • CUSTOM service (UUID = 00001523-1212-EFDE-1523-785FEABCD123)自定义服务(UUID = 00001523-1212-EFDE-1523-785FEABCD123)
  • CUSTOM characteristic (UUID = 00001524-1212-EFDE-1523-785FEABCD123) [marked as WRITE/INDICATE/NOTIFY in smartphone app)自定义特性(UUID = 00001524-1212-EFDE-1523-785FEABCD123)[在智能手机应用程序中标记为WRITE/INDICATE/NOTIFY)
  • descriptor (UUID = 00002902-0000-1000-8000-00805F9B34FB marked as READ in smartphone app)描述符(UUID = 00002902-0000-1000-8000-00805F9B34FB 在智能手机应用程序中标记为 READ)

and listing several 8-byte commands to send to the meter waiting for an 8-byte response from it.并列出几个 8 字节命令发送到仪表等待它的 8 字节响应。 Commands are sent using a frame with this format使用具有此格式的帧发送命令

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

and the response has the same one with some little differences.并且响应具有相同的响应,但略有不同。

I can send the frame using the gatt.writeCharacteristic, but I can't receive the response frame, getting always 0x01 0x00 as the only answer from the meter (2-byte instead of 8).我可以使用 gatt.writeCharacteristic 发送帧,但我无法接收响应帧,总是将 0x01 0x00 作为仪表的唯一答案(2 字节而不是 8 字节)。

This is what I do:这就是我所做的:

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

The only callback that is not triggered is the OnCharacteristicRead, where I suppose I'll find the frame response.唯一未触发的回调是 OnCharacteristicRead,我想我会在其中找到帧响应。

I made some mistake during the communication protocol?我在通信协议中犯了一些错误? How can I receive the 8-byte response frame?如何接收 8 字节的响应帧?

Thanks in advance!提前致谢!

Your mistake is that you are only allowed to have one outstanding Gatt operation at a time.你的错误是你一次只允许有一个未完成的 Gatt 操作。 You must wait for the callback before you send the next one.在发送下一个之前,您必须等待回调。 See Android BLE BluetoothGatt.writeDescriptor() return sometimes false for more info.有关更多信息,请参阅Android BLE BluetoothGatt.writeDescriptor() 有时返回 false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM