从BLE设备读取/写入自定义特征 [英] Read/Write custom characteristic from BLE device

查看:322
本文介绍了从BLE设备读取/写入自定义特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

首先,我尝试阅读

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

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

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

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

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

但是,在设备随附的文档中,建议您遵循GATT来连接到仪表:

However, in the documentation that came with the device it is hinted to connect to meter by following 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字节的响应.使用具有这种格式的框架发送命令

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数据_0数据_1数据_2数据_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.

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

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

这就是我要做的:

    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,我想我会在其中找到帧响应.

The only callback that is not triggered is the OnCharacteristicRead, where I suppose I'll find the frame response.

我在通讯协议中犯了一些错误?如何接收8字节的响应帧?

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

提前谢谢!

推荐答案

您的错误是您一次只能执行一次出色的Gatt操作.在发送下一个回调之前,您必须等待回调.请参见 Android BLE BluetoothGatt.writeDescriptor()有时返回false 了解更多信息.

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.

这篇关于从BLE设备读取/写入自定义特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆