我正在使用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)));
            }

}

最佳答案

您一次只能执行一项出色的GATT操作(readCharacteristic,writeCharacteristic,readDescriptor,writeDescriptor,requestMtu)。您需要等待相应的回调(onCharacteristicRead等),直到您可以执行新的回调为止。

关于java - 如何一次从两个服务读取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55714663/

10-10 06:24