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

时间:2019-01-05 10:07:04

标签: java android bluetooth-lowenergy uuid

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

首先,我尝试阅读

  • 健康温度计服务(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来连接到仪表:

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

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

[0x51 CMD数据_0数据_1数据_2数据_3 0xA3 CHK-SUM]

,并且响应具有相同的响应,但有少许差异。

我可以使用gatt.writeCharacteristic发送帧,但是我不能接收响应帧,总是从仪表获得唯一的答案(2字节而不是8字节)0x01 0x00。

这就是我的工作

    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字节的响应帧?

谢谢!

1 个答案:

答案 0 :(得分:2)

您的错误是您一次只能进行一次出色的Gatt手术。在发送下一个回调之前,您必须等待回调。有关更多信息,请参见Android BLE BluetoothGatt.writeDescriptor() return sometimes false

相关问题