How can read all characteristic's value of my BLE device?

后端 未结 3 1224
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 06:10

I\'m building an app with Android Studio that can read the value from a device BLE. This device, have 4 services. The fourth services have 3 characteristic. I want to read t

3条回答
  •  悲哀的现实
    2021-01-07 06:46

    You can achive multiple reads with the following code: First create a List

    private int ReadQueueIndex;
    private List ReadQueue;
    

    In onServicesDiscovered add characteristics in ReadQueue:

    ReadQueue = new ArrayList<>();
    ReadQueue.add(characteristicsList.get(2));        
    ReadQueue.add(characteristicsList.get(1));            
    ReadQueue.add(characteristicsList.get(0));
    ReadQueueIndex = 2;
    ReadCharacteristics(ReadQueueIndex);
    

    Create ReadCharacteristics method

    private void ReadCharacteristics(int index){
       bluetoothGatt.readCharacteristic(ReadQueue.get(index));
    }
    

    Then in your onCharacteristicRead callback:

    String value =  Arrays.toString(characteristic.getValue());
    if(characteristic.getUuid().equals(characteristicsList.get(0).getUuid())){
      Log.i("Characteristic 0:", value);
    } else if(characteristic.getUuid().equals(characteristicsList.get(1).getUuid())){
      Log.i("Characteristic 1:", value);
    } else if(characteristic.getUuid().equals(characteristicsList.get(2).getUuid())){
      Log.i("Characteristic 2:", value);
    }
    ReadQueue.remove(ReadQueue.get(ReadQueueIndex));
    if (ReadQueue.size() >= 0) {
        ReadQueueIndex--;
        if (ReadQueueIndex == -1) {
           Log.i("Read Queue: ", "Complete");
        }
        else {
           ReadCharacteristics(ReadQueueIndex);
        }
     }
    

    Hope its helpful.

提交回复
热议问题