2016-11-10 97 views
0

我在我的應用程序中遇到了藍牙問題。我有很多特徵,只能讀取2.當我嘗試閱讀更多時,我沒有得到任何回調。我讀到我應該創建隊列,但不知道如何去做。請幫忙嗎?藍牙沒有讀取每個特徵

這是我的特點:

private void registerParametersCharacteristics(List<BluetoothGattCharacteristic> characteristics) { 
     for (BluetoothGattCharacteristic characteristic : characteristics) { 
      UUID characteristicUuid = characteristic.getUuid(); 
      if (Characteristics.EON_PARAMS_EDDYSTONE_UID_CHAR.equals(characteristicUuid)) { 
       euidCharacteristic = characteristic; 
       Log.e(TAG, "registerParametersCharacteristics: EUID"); 
      } 
      else if (Characteristics.EON_PARAMS_EDDYSTONE_EID_CHAR.equals(characteristicUuid)) { 
       eeidCharacteristic = characteristic;} 
      else if (Characteristics.EON_PARAMS_EDDYSTONE_URL_CHAR.equals(characteristicUuid)) { 
       eurlCharacteristic = characteristic;} 
      else if (Characteristics.EON_PARAMS_EDDYSTONE_TLM_CHAR.equals(characteristicUuid)) { 
       etlmCharacteristic = characteristic;} 
      else if (Characteristics.EON_PARAMS_EDDYSTONE_UID_UUID.equals(characteristicUuid)) { 
       eUidUUidCharacteristic = characteristic;} 
      else if (Characteristics.EON_PARAMS_EDDYSTONE_URL_UUID.equals(characteristicUuid)) { 
       eUrlUuidCharacteristic = characteristic; 
       Log.e(TAG, "registerParametersCharacteristics: EurlUUID"); 
      } 
      else if (Characteristics.MINOR_CHARACTERISTIC_UUID.equals(characteristicUuid)) { 
       minorCharacteristic = characteristic;} 
      else if (Characteristics.MAJOR_CHARACTERISTIC_UUID.equals(characteristicUuid)) { 
       majorCharacteristic = characteristic; 
      } 
      else if (Characteristics.UUID_CHARACTERISTIC_UUID.equals(characteristicUuid)){ 
       uuidCharacteristic = characteristic; 
      } 
      else if (Characteristics.INTERVAL_CHARACTERISTIC_UUID.equals(characteristicUuid)) { 
       intervalCharacteristic = characteristic; 
      } 
      else if (Characteristics.TX_CHARACTERISTIC_UUID.equals(characteristicUuid)) { 
       txCharacteristic = characteristic; 
      } 
      else if (Characteristics.BATTERY_CHARACTERISTIC_UUID.equals(characteristicUuid)) { 
       batteryCharacteristic = characteristic; 
       Log.e(TAG, "registerParametersCharacteristics: BAT"); 
      } 
      Log.e(TAG, "registerParametersCharacteristics: " + characteristic.getUuid().toString()); 
     } 
    } 

,這是我onServicediscovered方法:

@Override 
     public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
      super.onServicesDiscovered(gatt, status); 
      if (status == BluetoothGatt.GATT_SUCCESS) { 
       List<BluetoothGattService> services = gatt.getServices(); 
       for (BluetoothGattService service : services) { 
        registerService(service); 
        Log.e(TAG, "onServicesDiscovered: " + service.getUuid().toString()); 
       } 
       callback.connectedStateChanged(true); 
      } else 
       disconnect(); 
     } 
     @Override 
     public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
      super.onCharacteristicRead(gatt, characteristic, status); 
      characteristicValueRead(characteristic); 
      BluetoothCommunicationManager.getInstance().processNext(); 
     } 

我失去了兩天寫這個隊列,並不能做到這一點。在此先感謝您的幫助。

回答

0

你應該使用'command'設計模式。基本上,你將擁有抽象的類或接口與單個「執行(..)」方法。您將創建類,擴展'Command'類,但是每個實現都將完成自己的工作。 然後您可以創建「ArrayList的」,添加所有任務,把它和處理在「for」循環像那些任務:

for (Command c : commands){ 
c.execute(..); 
{ 
+0

亞歷任何例子嗎? – Bartos

+1

當然,https://en.wikipedia.org/wiki/Command_pattern#Java –