我目前正在使用与CC2650蓝牙低功耗(BLE)设备通信的Android应用程序。
我必须先进行writeCharacteristic
调用,然后再通过一个函数进行多个readCharacteristic
调用。此顺序可以颠倒而不会影响功能。
问题1::仅分别调用writeCharacteristic
或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及更高版本
参考文献:
答案 0 :(得分:1)
这是因为您需要等待回调返回后才能再次写入/读取。这里的答案也有类似的问题。
Android BLE BluetoothGattDescriptor writeDescriptor issue
相反,除了写入之外,您可能还需要等待readDescriptor / Characteristic。
答案 1 :(得分:0)
通过了解需要排队,您已经中途了。但是您必须确保将其用于所有GATT操作。请参阅我的完整答案:Android BLE BluetoothGatt.writeDescriptor() return sometimes false。