首页 > 解决方案 > 如何管理多个 BLE writeCharacteristic 和 readCharacteristic 调用?

问题描述

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

我必须先拨打一个writeCharacteristic电话,然后再readCharacteristic拨打一个函数。此顺序可以颠倒而不影响功能。


问题 1:当仅单独调用 awriteCharacteristicreadCharacteristic时,软件按预期工作。但是当按顺序拨打电话时,软件似乎不起作用。

下面是代码。


代码部分引用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 及更高版本

参考:

标签: androidbluetooth-lowenergy

解决方案


这是因为在再次写入/读取之前,您需要等待回调返回。这里的答案类似的问题。

Android BLE BluetoothGattDescriptor writeDescriptor 问题

除了写入之外,您可能还需要等待 readDescriptor/Characteristic。


推荐阅读