如何一次从两个服务读取值

时间:2019-04-16 18:39:40

标签: java android bluetooth-lowenergy

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

}

1 个答案:

答案 0 :(得分:1)

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