2017-04-21 313 views
0

我目前正在開發一款Android應用程序,旨在從腕帶獲取數據並處理它們以獲取健康評估內容。而我被困在這個問題上,我不能讓onCharacteristicRead()函數來工作...它只是不會得到所謂的...android ble無法調用onCharacteristicRead()

這裏是相對於這部分代碼:

private BluetoothGattCallback gattcallback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, final int newState) { 
     super.onConnectionStateChange(gatt, status, newState); 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       String status; 
       switch (newState) { 
        case BluetoothGatt.STATE_CONNECTED: 
         lianjiezhuangtai.setText("connection succeed"); 
         bluetoothAdapter.stopLeScan(callback); 
         bluetoothGatt.discoverServices(); 
         break; 
        case BluetoothGatt.STATE_CONNECTING: 
         lianjiezhuangtai.setText("trying to connect"); 
         break; 
        case BluetoothGatt.STATE_DISCONNECTED: 
         lianjiezhuangtai.setText("connection lost"); 
         break; 
        case BluetoothGatt.STATE_DISCONNECTING: 
         lianjiezhuangtai.setText("disconnecting"); 
         break; 
       } 
       //pd.dismiss(); 
      } 
     }); 
    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     jibu.setText("run: trying to get steps"); 
     super.onCharacteristicRead(gatt, characteristic, status); 

     if (status == bluetoothGatt.GATT_SUCCESS) { 
      final int sum = 100;//characteristic.getValue()[0]; 

      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        jibu.setText("walked" + sum + "steps"); 
       } 
      }); 

      Log.e(TAG, "onCharacteristicRead: " + characteristic.getValue()[0]); 

     } 

    }   
    @Override 
    public void onServicesDiscovered(final BluetoothGatt gatt,final int status) { 
     super.onServicesDiscovered(gatt, status); 
     Log.i(TAG, "run: trying to get steps"); 
     if (status == bluetoothGatt.GATT_SUCCESS) { 
      final List<BluetoothGattService> services = bluetoothGatt.getServices(); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        //List<String> serlist = new ArrayList<>(); 
        for (final BluetoothGattService bluetoothGattService : services) { 
         bluetoothGattServices = bluetoothGattService; 

         Log.i(TAG, "onServicesDiscovered: " + bluetoothGattService.getUuid()); 

         List<BluetoothGattCharacteristic> charc = bluetoothGattService.getCharacteristics(); 

         for (BluetoothGattCharacteristic charac : charc) { 
          Log.i(TAG, "run: " + charac.getUuid()); 
          // 00002a06-0000-1000-8000-00805f9b34fb 
        //  bluetoothGatt.setCharacteristicNotification(characteristic_zd,true); 
          if (charac.getUuid().toString().equals("0000ff06-0000-1000-8000-00805f9b34fb")) { 
           characteristic_zd = charac; 

          } else if (charac.getUuid().toString().equals("00002a06-0000-1000-8000-00805f9b34fb")) { 

           characteristic_jb = charac; 

           // bluetoothGatt.setCharacteristicNotification(characteristic_jb,true); 
           bluetoothGatt.readCharacteristic(characteristic_jb); 
           //sum = charac.getValue()[0]; 
           gattcallback.onCharacteristicRead(gatt, characteristic_jb, status); 
           Log.i(TAG, "run: trying to get steps"); 
          } else if (charac.getUuid().toString().equals("")) { 
          } 
         } 


         serviceslist.add(bluetoothGattService.getUuid().toString()); 

        } 
//      ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//        MainActivity.this, android.R.layout.simple_expandable_list_item_1, serviceslist); 
        //list.setAdapter(adapter); 
       } 
      }); 
     } 

    } 



    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 
    } 


    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { 
     super.onCharacteristicChanged(gatt, characteristic); 



    } 

}; 

所以任何人都可以幫助我解決這個問題嗎?一直在尋找了一段時間的解決方案,仍然解決不了這個......非常感謝

回答

0

三件事:

  1. 你並不需要調用super.method()中每個回調。
  2. 你不應該調用onCharacteristic讀你自己,一旦讀取響應被調用readCharacteristic方法後,藍牙堆棧會這樣做。
  3. 如果存在多個特徵,則您的代碼將無法工作,因爲Android的GATT api一次只能有一個未完成的操作。這意味着您需要等待onCharacteristicRead回調,然後再執行另一個readCharacteristic。 (如果您打印readCharacteristic的返回值,您將看到它在第一次打印時爲真,但在以下時間爲false。)