代码之家  ›  专栏  ›  技术社区  ›  Kshitij

BluetoothGattCallBack函数onCharacteristicRead只调用一次

  •  2
  • Kshitij  · 技术社区  · 7 年前

    我正在尝试开发一个BT应用程序,用于连接到BLE设备并从中读取一些数据。 对于设备信息部分,我遍历了设备的所有特性,读取特性并从BluetoothGattCallback的onCharacteristicRead方法中收集返回的数据。

    问题是,对于循环,onCharacteristicRead方法只调用一次。请参阅下面的代码和日志。

     @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                String value = characteristic.getStringValue(0);
                Log.e("TAG", "onCharacteristicRead: " + value + " UUID " + characteristic.getUuid().toString() );
    
            }
    

    特征循环为

    private void getDeviceInformation() {
        BluetoothGattService deviceInfoService = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE);
        BluetoothGattCharacteristic deviceSerialNumber, deviceHardwareRevision, deviceSoftwareRevision;
    
    
        for (BluetoothGattCharacteristic characteristic : bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE).getCharacteristics()) {
            bluetoothGatt.setCharacteristicNotification(characteristic, true);
            bluetoothGatt.readCharacteristic(characteristic);
        }
    }
    

    Logcat公司-

    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a25-0000-1000-8000-00805f9b34fb enable: true
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a27-0000-1000-8000-00805f9b34fb enable: true
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a28-0000-1000-8000-00805f9b34fb enable: true
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a23-0000-1000-8000-00805f9b34fb enable: true
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a50-0000-1000-8000-00805f9b34fb enable: true
    W/BluetoothGatt: onCharacteristicRead() - Device=F6:8B:C2:F3:EB:EE handle=14 Status=0
    E/TAG: onCharacteristicRead: 86a691595263 UUID 00002a25-0000-1000-8000-00805f9b34fb
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   HeyAlex    7 年前

    你应该同步进行所有操作。因此,在:

    bluetoothGatt.readCharacteristic(characteristic);
    

    你应该等回电话

    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    

    所以,如果你想做你描述的事情,就做这样的事情:

    List<BluetoothGattCharacteristic> characteristics = new ArrayList<>();
    boolean isGettingDeviceInformation;
    int count = 0;
    
    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        String value = characteristic.getStringValue(0);
        Log.e("TAG", "onCharacteristicRead: " + value + " UUID " + characteristic.getUuid().toString() );
        if(isGettingDeviceInformation) {
            if(count < characteristics.size()) {
                count++;
                bluetoothGatt.setCharacteristicNotification(characteristics.get(count), true);
                bluetoothGatt.readCharacteristic(characteristics.get(count));
            } else {
                isGettingDeviceInformation = false;
            }
        }
    }
    
    private void getDeviceInformation() {
        BluetoothGattService deviceInfoService = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE);
        BluetoothGattCharacteristic deviceSerialNumber, deviceHardwareRevision, deviceSoftwareRevision;
        characteristics = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE).getCharacteristics();
        bluetoothGatt.setCharacteristicNotification(characteristics.get(count), true);
        bluetoothGatt.readCharacteristic(characteristics.get(count));
    }
    

    更新

    不要忘记,在蓝牙中,您的操作可能会消失,所以您可以在循环中堆叠,等待回调。所以为了避免它,你需要在操作时超时,所以如果没有得到回调,你只需取消当前操作,然后可以继续。