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

android ble connect slowly的更多相关文章

  1. Android ble 蓝牙4.0 总结

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  2. Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能

    Android BLE与终端通信(四)--实现服务器与客户端即时通讯功能 前面几篇一直在讲一些基础,其实说实话,蓝牙主要为多的还是一些概念性的东西,当你把概念都熟悉了之后,你会很简单的就可以实现一些逻 ...

  3. Android BLE与终端通信(三)——客户端与服务端通信过程以及实现数据通信

    Android BLE与终端通信(三)--客户端与服务端通信过程以及实现数据通信 前面的终究只是小知识点,上不了台面,也只能算是起到一个科普的作用,而同步到实际的开发上去,今天就来延续前两篇实现蓝牙主 ...

  4. Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址

    Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...

  5. Android ble 蓝牙4.0 总结一

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  6. Android BLE蓝牙详细解读

    代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...

  7. 快速接入 Android BLE 开发的基础框架

    代码地址如下:http://www.demodashi.com/demo/12092.html ** Android BLE基础操作框架,基于回调,操作简单.包含扫描.多连接.广播包解析.服务读写及通 ...

  8. Android BLE与终端通信(三)——client与服务端通信过程以及实现数据通信

    Android BLE与终端通信(三)--client与服务端通信过程以及实现数据通信 前面的终究仅仅是小知识点.上不了台面,也仅仅能算是起到一个科普的作用.而同步到实际的开发上去,今天就来延续前两篇 ...

  9. Android BLE 蓝牙开发——扫码枪基于BLESSED

    一.蓝牙模式HID与BLE 当扫码枪与手机连接时,通常采用的是蓝牙HID(Human Interface Device)模式.本质上是一个把扫码枪作为一个硬件键盘,按照键盘协议把扫码后的结果逐个输入到 ...

随机推荐

  1. mysql 自增序列(转)

    1:原理是在建立一个触发器TRIGGER tri_NewBH  在table插入时执行序列计算 mysql),content ),`date` DATETIME,val INT); Query OK, ...

  2. android chrome 不支持 audio/video的autoplay 属性

    在chrome 浏览器中输入:chrome://flags,找到"播放媒体时的手势要求",停用就可以了.

  3. 包装BufferedReader的readLine()输出行号

    定义一个类,实现与被增强对象相同的接口,或继承这个类,视情况而定 定义一个变量,记住被增强的对象 定义一个构造函数,接收被增强的对象 覆盖要增强的方法 对于不需要增强的方法,调用被增强对象原有的方法 ...

  4. 初识--Ajax & Json

    1,AJAX是一种进行页面局部异步刷新技术. 用AJAX向服务器发送请求和获得服务器返回的数据并更新到页面中. 不是刷新整个页面,而是在HTML页面中使用JavaScript创建XMLHTTPRequ ...

  5. 二维码zxing源码分析(一)camera部分

    首先,我们先把zxing的源代码给下载下来,这个网上有很多,我下载的是2.3的,不得不说这个谷歌提供的包包含的功能还是很全面的.     我把下载的包解压后,找到android文件夹,导入到ecpli ...

  6. Windows 10 (or 8)Chrome 观看视频发生flash不能加载,即"could't load plugins"原因之一

    最近一直如题,不能看视频,后来发现从一个已经使用管理员权限打开的应用转到Chrome就可以加载flash,而从桌面打开Chrome就加载不了. 今天再次查找信息,从Ubuntu下Chrome不能加载f ...

  7. 收集一些常用的php正则表达式

    1.    平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2.    "^\d+$" //非负整数(正整数 + 0) 3.    "^[0 ...

  8. python:浅析python 中__name__ = '__main__' 的作用

    有句话经典的概括了这段代码的意义: “Make a script both importable and executable” 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可 ...

  9. UNIX 信号基本概念

    1. 信号的基本概念 为了理解信号,先从我们最熟悉的场景说起: 用户输入命令,在Shell下启动一个前台进程. 用户按下Ctrl-C,这个键盘输入产生一个硬件中断. 如果CPU当前正在执行这个进程的代 ...

  10. SpringMvc入门三----控制器

    在传统的Spring MVC开发方法中,必须在Bean配置文件中为每个控制器类配置实例和请求映射和让每个控制器类去实现或者扩展特定于框架的接口或者基类,不够灵活. 如果Spring MVC可以自动侦测 ...