Android - BLE模块在连接数秒后长时间忙碌

问题描述:

我写了一个简单的应用程序,可以写入特定的特性。我基于谷歌示例 - 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; 
    } 

} 

该设备是“忙”并不仅仅意味着响应是悬而未决。 Android的API要求您在发出新请求后等待相应的回调(如写入的onCharacteristicWrite)。如果您认为需要太多时间才能降低连接间隔。

+0

我实际上发现自己的连接间隔很长。由于谈判过程可能在最初的几秒钟内降低。 +1仍然 –