首页 > 解决方案 > 如何一次从两个服务中读取值

问题描述

我正在使用 BLE 开发一个项目,并希望一次从两个服务中读取值,但我只从第一个服务中获取值

我的 onServicesDiscovered 代码是

public void onServicesDiscovered(BluetoothGatt gatt, int status) 
{
        for (BluetoothGattService service : gatt.getServices()) {
            if ((service == null) || (service.getUuid() == null)) {
                continue;
            }

            if (BleUuid.SERVICE_DEVICE_INFORMATION.equalsIgnoreCase(service
                    .getUuid().toString())) 
            {  
              mConnGatt.readCharacteristic(service.getCharacteristic(UUID.fromString(BleUuid.CHAR_MANUFACTURER_NAME_STRING)));
            }
            if (BleUuid.CHAR_ALERT_LEVEL.equalsIgnoreCase(service
                    .getUuid().toString())) 
            {
              mConnGatt.readCharacteristic(service.getCharacteristic(UUID.fromString(BleUuid.CHAR_SERIAL_NUMBEAR_STRING)));
            }

}

标签: javaandroidbluetooth-lowenergy

解决方案


一次只能有一个未完成的 GATT 操作(readCharacteristic、writeCharacteristic、readDescriptor、writeDescriptor、requestMtu)。您需要等待相应的回调(onCharacteristicRead 等),直到您可以执行新的回调。


推荐阅读