我正在尝试开发用于连接BLE设备并从中读取一些数据的BT应用程序。
对于设备信息部分,我将遍历设备的所有特征,读取特征并从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

最佳答案

您应该同步进行所有操作。所以之后:

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


更新

不要忘记在蓝牙中您的操作已经消失,因此您可以循环堆叠以等待回调。因此,为避免发生这种情况,您需要使操作超时,因此,如果不获取回调,则只需取消当前操作即可,并且可以走得更远。

10-05 21:20