如何管理多个BLE写特征和读取特征调用?



我目前正在研究与CC2650低功耗蓝牙(BLE(设备通信的Android应用程序。

我必须进行一次writeCharacteristic调用,然后使用一个函数进行多次readCharacteristic调用。此顺序可以颠倒,而不会影响功能。


问题 1:当仅单独调用writeCharacteristicreadCharacteristic时,软件将按预期工作。但是当按顺序进行调用时,软件似乎不起作用。

下面是代码。


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

具有读取特征(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:由于我有多个读取,因此我创建了一个队列来处理。您认为读写是导致问题的原因吗?如果是这样,关于如何管理和阻止读取和写入的任何建议?

注意:代码适用于安卓 API 21 及更高版本

引用:

  • 什么是BLE中的"可靠写入"?
  • onCharacteristicWrite(( 被调用,但它并不总是写入

通过了解您需要排队,您已经完成了一半。但您必须确保将其用于所有关贸总协定操作。查看我的完整答案:Android BLE BluetoothGatt.writeDescriptor(( 返回有时为假。

这是因为您需要等待回调返回才能再次写入/读取。这里的答案也有类似的问题。

Android BLE 蓝牙GattDescriptor writeDescriptator issue

除了写入之外,您可能还需要等待读取描述符/特征。

最新更新