我正在尝试开发一个用于连接到 BLE 设备并从中读取一些数据的 BT 应用程序。设备信息部分,我循环遍历设备的所有特征,读取特征并从BluetoothGattCallback的onCharacteristicRead方法收集返回数据。
问题是对于循环,onCharacteristicRead 方法只被调用一次。请参考下面的代码和logcat。
@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);
}
}
日志猫 -
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