android - 如何管理多个 BLE writeCharacteristic 和 readCharacteristic 调用?
问题描述
我目前正在开发与 CC2650 蓝牙低功耗 (BLE) 设备通信的 Android 应用程序。
我必须先拨打一个writeCharacteristic
电话,然后再readCharacteristic
拨打一个函数。此顺序可以颠倒而不影响功能。
问题 1:当仅单独调用 awriteCharacteristic
或readCharacteristic
时,软件按预期工作。但是当按顺序拨打电话时,软件似乎不起作用。
下面是代码。
代码部分引用writeCharacteristic
代码(UI 线程)
final BluetoothGattCharacteristic characteristic_select = mGattCharacteristicMap.get("hotstate");
if (characteristic_select != null) {
final int charaProp = characteristic_select.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
String strData = "00";
int len = strData.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(strData.charAt(i), 16) << 4)
+ Character.digit(strData.charAt(i + 1), 16));
}
characteristic_select.setValue(data);
mBLE_Service.writeCharacteristic(characteristic_select);
}
}
带有 readCharacteristic(UI 线程)的代码部分。注意 排队的多次读取调用
final BluetoothGattCharacteristic characteristic_time = mGattCharacteristicMap.get("timestate");
if (characteristic_time != null) {
final int charaProp = characteristic_time.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
for (int i = 0; i < 10; i++) {
mBLE_Service.readCharacteristic(characteristic_time);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
}
}, 5000);
}
}
}
代码readCharacteristic
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
// Queue the characteristic to read, since several reads are done on startup
characteristicQueue.add(characteristic);
// If there is only 1 item in the queue, then read it. If more than 1, it is handled
// asynchronously in the callback
if((characteristicQueue.size() <= 1)) {
mBluetoothGatt.readCharacteristic(characteristic);
}
}
代码writeCharacteristic
public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.writeCharacteristic(characteristic);
}
代码onCharacteristicRead
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
// Read action has finished, remove from queue
characteristicQueue.remove();
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
// Handle the next element from the queues
if(characteristicQueue.size() > 0)
mBluetoothGatt.readCharacteristic(characteristicQueue.element());
else if(descriptorWriteQueue.size() > 0)
mBluetoothGatt.writeDescriptor(descriptorWriteQueue.element());
}
代码onCharacteristicWrite
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (status==BluetoothGatt.GATT_SUCCESS){
broadcastUpdate(ACTION_WRITE_SUCCESS, characteristic);
}
}
问题 2:由于我有多次读取,我创建了一个队列来处理。你认为读写是导致问题的原因吗?如果是这样,关于如何管理和阻止读写的任何建议?
注意:代码适用于 Android API 21 及更高版本
参考:
解决方案
这是因为在再次写入/读取之前,您需要等待回调返回。这里的答案类似的问题。
Android BLE BluetoothGattDescriptor writeDescriptor 问题
除了写入之外,您可能还需要等待 readDescriptor/Characteristic。
推荐阅读
- python - 如何知道在框架内选择了哪个按钮并稍后在 tkinter 中使用其文本?
- python - 子进程在 Python 3.7+ 中包含有问题的引号
- javascript - 如何在 Bootstrap 4 中切换折叠图标的动画?
- svg.js - 如何绘制 45 度的 svg 折线?
- python - 为什么 pandas 25.3 版的 groupby 和 agg 不再起作用了?
- r - 反应性地过滤数据以生成地图
- sql - 向存储过程添加列是否有性能风险
- android - Apk 在 libs 中不包含 arm64-v8a 文件夹,但我的项目在 jnilibs/arm64-v8a/xyz.so 下有 .so 文件
- python - 多项式朴素贝叶斯分类器python
- amazon-web-services - AWS S3 CloudFormation 只读权限