8

Ein bisschen hier stecken, könnte Ihre Hilfe benötigen. Ich möchte mehrere BLE-Eigenschaften gleichzeitig lesen, einige Leute schlagen vor, PriorityQueue dafür zu verwenden. Ich kenne bereits alle Uuids, etc. brauchen nur eine Möglichkeit, mehrere gleichzeitig zu lesen. Kann mir jemand erklären, wie genau es aussehen soll? Oder vielleicht gibt es noch eine andere einfachere Lösung?Android Wie man mehrere BLE-Eigenschaften mit einer PriorityQueue liest

Vielen Dank im Voraus, hier ist mein Code:

public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 

    PriorityQueue<BluetoothGattCharacteristic> queue = new PriorityQueue<BluetoothGattCharacteristic>(); 

    // When connection state changes 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      Log.v(TAG, "Connected!"); 
      gatt.discoverServices(); 
     } 
     if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      Log.v(TAG, "Disconnected..."); 

     } 
    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 

     List<BluetoothGattService> services = gatt.getServices(); 
     BluetoothGattService rightService = null; 

     for (int i = 0; i < services.size(); i++) { 
      if (services.get(i).getCharacteristics().size() > 8) { 
       rightService = services.get(i); 
      } 
     } 

     List<UUID> uuidsList; 

     UUID TRANSMISSION_POWER = rightService.getCharacteristics().get(4).getUuid(); 
     UUID BROADCASTING_INTERVAL = rightService.getCharacteristics().get(6).getUuid(); 
     UUID BEACON_NAME = rightService.getCharacteristics().get(8).getUuid(); 
     UUID CONNECTION_MODE = rightService.getCharacteristics().get(9).getUuid(); 
     //UUID SOFT_REBOOT = rightService.getCharacteristics().get(10).getUuid(); 

     uuidsList = new ArrayList<UUID>(); 

     uuidsList.add(TRANSMISSION_POWER); 
     uuidsList.add(BROADCASTING_INTERVAL); 
     uuidsList.add(BEACON_NAME); 
     uuidsList.add(CONNECTION_MODE); 
     //uuidsList.add(SOFT_REBOOT); 

     queue.add(rightService.getCharacteristic(uuidsList.get(0))); 
     queue.add(rightService.getCharacteristic(uuidsList.get(1))); 
     queue.add(rightService.getCharacteristic(uuidsList.get(2))); 

    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 

     Log.v(TAG, "CHARACTERISTIC VALUE___: " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)); 
     onServicesDiscovered(gatt, 0); 

    } 

}; 

UPDATE:

auch nach sie auf verschiedene Threads es nach wie vor nur reagiert auf eine gatt.readCharacteristic (...) setzen. wie folgt vor:

// Gatt Callback 
public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 

    // When connection state changes 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      Log.v(TAG, "Connected!"); 
      gatt.discoverServices(); 
     } 
     if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      Log.v(TAG, "Disconnected..."); 

     } 
    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 


     List<BluetoothGattService> services = gatt.getServices(); 

     /* 
     DISPLAY ALL SERVICES AND CHARACTERISTICS 

     for (int i = 0; i < services.size(); i++) { 
      Log.v(TAG, "SERVICE____: " + services.get(i).getUuid()); 

      for (int k = 0; k < services.get(i).getCharacteristics().size(); k++) { 
       Log.v(TAG, "CHARACTERISTIC____: " + services.get(i).getCharacteristics().get(k).getUuid()); 
      } 

     } 
     */ 

     BluetoothGattService rightService = null; 

     for (int i = 0; i < services.size(); i++) { 
      if (services.get(i).getCharacteristics().size() > 8) { 
       rightService = services.get(i); 
      } 
     } 

     List<UUID> uuidsList; 

     UUID TRANSMISSION_POWER = rightService.getCharacteristics().get(4).getUuid(); 
     UUID BROADCASTING_INTERVAL = rightService.getCharacteristics().get(6).getUuid(); 
     UUID BEACON_NAME = rightService.getCharacteristics().get(8).getUuid(); 
     UUID CONNECTION_MODE = rightService.getCharacteristics().get(9).getUuid(); 
     //UUID SOFT_REBOOT = rightService.getCharacteristics().get(10).getUuid(); 

     uuidsList = new ArrayList<UUID>(); 

     uuidsList.add(TRANSMISSION_POWER); 
     uuidsList.add(BROADCASTING_INTERVAL); 
     uuidsList.add(BEACON_NAME); 
     uuidsList.add(CONNECTION_MODE); 
     //uuidsList.add(SOFT_REBOOT); 


     class powerThread extends Thread{ 

      UUID uuid; 
      BluetoothGatt gatt; 
      BluetoothGattService service; 
      public powerThread(UUID uuid, BluetoothGatt gatt, BluetoothGattService service) { 
       this.gatt = gatt; 
       this.service = service; 
       this.uuid = uuid; 
      } 
      @Override 
      public void run() { 
       gatt.readCharacteristic(service.getCharacteristic(uuid)); 
      } 
     } 
     powerThread pt = new powerThread(TRANSMISSION_POWER, gatt, rightService); 
     pt.run(); 


     class intervalThread extends Thread{ 

      UUID uuid; 
      BluetoothGatt gatt; 
      BluetoothGattService service; 
      public intervalThread(UUID uuid, BluetoothGatt gatt, BluetoothGattService service) { 
       this.gatt = gatt; 
       this.service = service; 
       this.uuid = uuid; 
      } 
      @Override 
      public void run() { 
       gatt.readCharacteristic(service.getCharacteristic(uuid)); 
      } 
     } 
     intervalThread it = new intervalThread(BROADCASTING_INTERVAL, gatt, rightService); 
     it.run(); 


    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 

     Log.v(TAG, "CHARACTERISTIC VALUE___: " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)); 

    } 

}; 
+0

Versuchte Lesung eins nach dem anderen? Was ist das Problem mit dem – AAnkit

+0

Ich tat, und ich wählte diese Lösung für jetzt. Problem ist, dass ich mich mehrfach verbinden muss und das mehr Zeit braucht, als ich möchte. –

Antwort

10

Für jeden, der das gleiche Problem auftreten kann, hier ist eine einfache Lösung, die eine Liste <> von Eigenschaften ausgewählt werden. genannt requestCharacteristics (Gatt) und übergeben Sie die gatt Objekt, um es

public static final BluetoothGattCallback readGattCallback = new BluetoothGattCallback() { 

    List<BluetoothGattCharacteristic> chars = new ArrayList<>(); 

    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 

     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      Log.v(TAG, "Connected!"); 
      broadcastingInterval = 999; 
      transmissionPower = 999; 
      gatt.discoverServices(); 
     } 
     if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      Log.v(TAG, "Disconnected..."); 

     } 
    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 

     List<BluetoothGattService> services = gatt.getServices(); 
     BluetoothGattService rightService = null; 

     for (int i = 0; i < services.size(); i++) { 
      if (services.get(i).getCharacteristics().size() > 8) { 
       rightService = services.get(i); 
      } 
     } 

     chars.add(rightService.getCharacteristics().get(4)); 
     chars.add(rightService.getCharacteristics().get(6)); 

     requestCharacteristics(gatt); 

    } 

    public void requestCharacteristics(BluetoothGatt gatt) { 
     gatt.readCharacteristic(chars.get(chars.size()-1)); 
    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     if (status == 0) { 

      if (characteristic.getUuid().toString().substring(7, 8).equals("5")) { 
       transmissionPower = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); 
       Log.v(TAG, "tPOWER READ"); 

      } else if (characteristic.getUuid().toString().substring(7,8).equals("7")) { 
       broadcastingInterval = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); 
       Log.v(TAG, "INTERVAL READ"); 
      } 

      chars.remove(chars.get(chars.size() - 1)); 

      if (chars.size() > 0) { 
       requestCharacteristics(gatt); 
      } else { 
       gatt.disconnect(); 
      } 
     } 
    } 

}; 
  1. Erstellen Sie eine Liste von Merkmalen
  2. In onServicesDiscovered die Liste mit Eigenschaften füllen Sie/schreiben
  3. Erstellen Sie eine neue Methode lesen möchten. Rufen Sie diese Methode von onServicesDiscovered auf, nachdem Sie Merkmale zur Liste hinzugefügt haben.
  4. In requestCharacteristics() -Methode Aufruf gatt.readCharacteristic (chars.get (chars.size() - 1));
  5. In onCharacteristicRead prüfen, ob die Größe Ihrer Liste nicht Null ist, dann lesen Sie Ihr Merkmal, entfernen Sie das letzte Element Ihrer Liste und rufen Sie requestCharacteristic() erneut.
  6. Das ist alles