8

有點卡在這裏,可能需要您的幫助。我想一次閱讀幾個BLE特性,有人建議使用PriorityQueue。我已經知道所有的uuids等,只需要一種方法來一次閱讀幾個。 任何人都可以解釋它究竟應該是什麼樣子?或者也許還有另一個更簡單的解決方案?Android如何通過PriorityQueue讀取多個BLE特徵

在此先感謝,這裏是我的代碼:

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:

甚至把他們在不同的線程它仍然只反應一個gatt.readCharacteristic(...)之後。像下面:

// 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

試過逐一閱讀?我做了什麼問題 – AAnkit

+0

,現在我選擇了這個解決方案。問題是我需要連接多次,而且花費的時間比我想要的要多。 –

回答

10

爲了任何人誰可能會遇到同樣的問題,這裏是一個使用特性的名單<>一個簡單的解決方案。

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. 創建特色
  2. 在onServicesDiscovered列表填充你想讀/特性列表寫
  3. 創建一個新的方法叫做requestCharacteristics(GATT)和關貿總協定對象傳遞給它。將特徵添加到列表後,從onServicesDiscovered調用此方法。
  4. 在requestCharacteristics()方法中調用gatt.readCharacteristic(chars.get(chars.size() - 1));
  5. 在onCharacteristicRead檢查您的列表大小是否不爲零,然後讀取您的特徵,刪除列表中的最後一項並再次調用requestCharacteristic()。
  6. 這就是全部