Hi I'm writing an Android app to connect to a BLE peripheral device. Android 4.4.2, Galaxy Nexus.

I have an LED on the device to indicate connection state.

The issue is the duration from connectGatt() call to the point of receiving onConnectionStateChange STATE_CONNECTED are so inconsistent. Some time it is very quick but most of the time it takes 5s or more. Turning Bluetooth off/on does not have any effect.

I tried TI BTool on PC with the TI BLE Dongle and it always establishes connection very fast.

I also tried with an iPhone 5S and it is fast too.

  1. Any one has experienced this issue too?
  2. Is there any chance we can improve this?
OK, I found the cause, I think I misused the APIs: Previously I call connectGatt with the 2nd param = true connectGatt(context, true, gattCallback); But now I change it to connectGatt(context, false, gattCallback); And the connecting time improved greatly – user2810131 Mar 6 at 9:43 I've experimented slow connection but only when attempting to reconnecting the remote device, connecting device at first time gets no problem but reconnecting remains the connection in the onClientRegistered() method

Passing true to connectGatt() autoconnect argument requests a background connection, while passing false requests a direct connection. BluetoothGatt#connect() always requests a background connection.

Background connection (according to Bluedroid sources from 4.4.2 AOSP) has scan interval of 1280ms and a window of 11.25ms. This corresponds to about 0.9% duty cycle which explains why connections, when not scanning, can take a long time to complete.

Direct connection has interval of 60ms and window of 30ms so connections complete much faster. Additionally there can only be one direct connection request pending at a time and it times out after 30 seconds. onConnectionStateChange() gets called with state=2, status=133 to indicate this timeout.

I have verified this behavior on Nexus5 but obviously YMMV.

I should mention that there is a race condition in BluetoothGatt.java that can cause a direct connection request even if autoconnect=true is passed into BluetoothDevice#connectGatt().

http://stackoverflow.com/questions/22214254/android-ble-connect-slowly

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. App安全(一) Android防止升级过程被劫持和
  2. Android(安卓)TTS文本转语音TextToSpeech
  3. Android使用HttpURLConnection请求网络资
  4. Android(安卓)studio 使用git提交项目到o
  5. Android(安卓)Service相关
  6. android Volley网络通信框架学习
  7. Android(安卓)PopupWindow介绍及实现菜单
  8. Android(安卓)NDK的C/C++代码中利用JNI回
  9. Android调用手机新浪微博客户端分享
  10. 初学Android,启动,关闭Activity(七)