android ble 连接缓慢 [英] android ble connect slowly

查看:44
本文介绍了android ble 连接缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Android 应用程序来连接到 BLE 外围设备.安卓 4.4.2,银河 Nexus.

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

我在设备上有一个 LED 来指示连接状态.

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

问题是从 connectGatt() 调用到接收 onConnectionStateChange STATE_CONNECTED 点的持续时间是如此不一致.有时它非常快,但大部分时间需要 5 秒或更长时间.关闭/打开蓝牙没有任何影响.

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.

我在带有 TI BLE 加密狗的 PC 上尝试了 TI BTool,它总是能够非常快速地建立连接.

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

我也试过 iPhone 5S,它也很快.

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

  1. 有人也遇到过这个问题吗?
  2. 我们有机会改进吗?

推荐答案

将 true 传递给 connectGatt() autoconnect 参数请求后台连接,而传递 false 请求直接连接.BluetoothGatt#connect() 总是请求后台连接.

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

后台连接(根据来自 4.4.2 AOSP 的 Bluedroid 来源)具有 1280 毫秒的扫描间隔和 11.25 毫秒的窗口.这对应于大约 0.9% 的占空比,这解释了为什么连接在不扫描时可能需要很长时间才能完成.

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.

直接连接的间隔为 60 毫秒,窗口为 30 毫秒,因此连接完成得更快.此外,一次只能有一个直接连接请求未决,并且在 30 秒后超时.onConnectionStateChange() 以 state=2、status=133 调用以指示此超时.

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.

我已经在 Nexus5 上验证了这种行为,但显然是 YMMV.

I have verified this behavior on Nexus5 but obviously YMMV.

我应该提一下,BluetoothGatt.java 中有一个竞争条件,即使将 autoconnect=true 传递给 BluetoothDevice#connectGatt(),它也会导致直接连接请求.

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().

这篇关于android ble 连接缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆