Read/Write custom characteristic from BLE device

人走茶凉 提交于 2020-01-13 22:40:31

问题


I'm trying to interact with a temperature meter BLE device using Android Studio as IDE and Java as programming language. 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)
  • TEMPERATURE MEASUREMENT characteristic (UUID = 00002A1C-0000-1000-8000-00805F9B34FB) [marked as 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 :

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 :

  • CUSTOM service (UUID = 00001523-1212-EFDE-1523-785FEABCD123)
  • CUSTOM characteristic (UUID = 00001524-1212-EFDE-1523-785FEABCD123) [marked as WRITE/INDICATE/NOTIFY in smartphone app)
  • descriptor (UUID = 00002902-0000-1000-8000-00805F9B34FB marked as READ in smartphone app)

and listing several 8-byte commands to send to the meter waiting for an 8-byte response from it. Commands are sent using a frame with this format

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

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.

I made some mistake during the communication protocol? How can I receive the 8-byte response frame?

Thanks in advance!


回答1:


Your mistake is that you are only allowed to have one outstanding Gatt operation at a time. You must wait for the callback before you send the next one. See Android BLE BluetoothGatt.writeDescriptor() return sometimes false for more info.



来源:https://stackoverflow.com/questions/54050918/read-write-custom-characteristic-from-ble-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!