2017-09-22 151 views
0

我寫了一個簡單的應用程序,可以寫入特定的特性。我基於谷歌示例 - https://github.com/googlesamples/android-BluetoothLeGatt的基礎上我的應用程序。我添加了按鈕,連接後可以寫入特定的特徵字節。Android - BLE模塊在連接數秒後長時間忙碌

現在我注意到,連接幾秒鐘後(總是小於5)它工作正常,但然後函數writeCharacteristic(https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#writeCharacteristic(android.bluetooth.BluetoothGattCharacteristic))開始返回false。我進行了調試,結果發現設備很忙。我能夠每1.5秒成功調用writeCharacteristic,而在連接的前幾秒內沒有任何延遲,這是非常緩慢的。

這裏是我的代碼片段,其中的onClick功能:

public void onClick(View v) { 
    byte value[] = {0}; 
    switch (v.getId()) { 

     case R.id.button1: 
      value[0] = 1; 
      mBulbCharacteristic.setValue(value); 
      mBluetoothLeService.writeCharacteristic(mBulbCharacteristic); 
      break; 

     case R.id.button2: 
      value[0] = 2; 
      mBulbCharacteristic.setValue(value); 
      mBluetoothLeService.writeCharacteristic(mBulbCharacteristic); 
      break; 

     case R.id.button3: 
      value[0] = 3; 
      mBulbCharacteristic.setValue(value); 
      mBluetoothLeService.writeCharacteristic(mBulbCharacteristic); 
      break; 

     default: 
      break; 
    } 

} 

回答

1

該設備是「忙」並不僅僅意味着響應是懸而未決。 Android的API要求您在發出新請求後等待相應的回調(如寫入的onCharacteristicWrite)。如果您認爲需要太多時間才能降低連接間隔。

+0

我實際上發現自己的連接間隔很長。由於談判過程可能在最初的幾秒鐘內降低。 +1仍然 –