Android 4.3 低功耗蓝牙不稳定 [英] Android 4.3 Bluetooth Low Energy unstable

查看:47
本文介绍了Android 4.3 低功耗蓝牙不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个将使用蓝牙低功耗的应用程序(在 Nexus 4 上进行测试).在开始使用 Android 4.3 中的官方 BLE API 后,我注意到在我第一次连接设备后,我很少能够再次成功连接到该设备或任何其他设备/与之通信.

I am currently developing an application that will use Bluetooth Low Energy (testing on the Nexus 4). After getting started with the official BLE APIs in Android 4.3, I have noticed that after I connect a device for the first time I am rarely able to successfully connect to / communicate with that device or any other device again.

按照指南这里,我可以成功连接到设备,扫描服务和特性,以及读/写/接收通知,没有任何问题.但是,在断开连接并重新连接后,我经常无法扫描服务/特性或无法完成读/写.我在日志中找不到任何内容来说明发生这种情况的原因.

Following the guide here, I can successfully connect to a device, scan services and characteristics, and read/write/receive notifications without any issues. However, after disconnecting and re-connecting, I am often unable to either scan services/characteristics or unable to complete a read/write. I can't find anything in the logs to indicate why this is happening.

一旦发生这种情况,我必须卸载应用程序、禁用蓝牙并重新启动手机,然后它才能再次开始工作.

Once this happens I have to uninstall the application, disable Bluetooth, and restart the phone before it will start working again.

每当设备断开连接时,我都会确保在 BluetoothGatt 对象上调用 close() 并将其设置为 null.有什么见解吗?

Whenever a device is disconnected I make sure to call close() on the BluetoothGatt object and set it to null. Any insights?


日志转储:对于这些日志,我根植了我的手机并提高了/etc/bluetooth/bt_stack.conf 中相关项目的跟踪级别


Log dumps: For these logs I rooted my phone and upped the trace levels of related items in /etc/bluetooth/bt_stack.conf

连接成功 - 重启手机并安装应用程序后的第一次尝试.我能够连接、发现所有服务/特征以及读/写.

Successful connection - First attempt after rebooting the phone and installing the app. I am able to connect, discover all services/characteristics, and read/write.

尝试 1 失败 - 这是断开上述成功连接后的下一次尝试.似乎我能够发现特征,但第一次尝试读取返回了一个空值,此后不久就断开连接.

Failed Attempt 1 - This is the next attempt after disconnecting from the successful connection above. It seems I was able to discover characteristics, but the first attempt to read returned a null value and disconnected soon thereafter.

尝试 2 失败 - 我什至无法发现服务/特征的示例.

Failed Attempt 2 - An example where I am not even able to discover services/characteristics.

编辑 2:
我尝试连接的设备基于 TI 的 CC2541 芯片.我获得了一个 TI SensorTag(也基于在 CC2541 上)玩弄并发现 TI 发布了一个昨天用于 SensorTag 的 Android 应用程序.但是,这个应用程序有同样的问题.我在另外两个 Nexus 4s 上测试了这个,结果相同:第一次或第二次连接到 SensorTag 成功,但(根据日志)失败此后发现服务,导致各种崩溃.我开始怀疑是不是这个特定芯片的问题?

EDIT 2:
The device to which I am trying to connect is based on TI's CC2541 chip. I obtained a TI SensorTag (also based on the CC2541) to play around with and discovered that TI released an android app for the SensorTag yesterday. However, this app has the same problem. I tested this on two other Nexus 4s with the same result: Connection to the SensorTag is successful the first or second time, but (according to the logs) fails to discover services thereafter, causing all sorts of crashes. I'm starting to wonder if it's an issue with this specific chip?

推荐答案

重要实现提示

(也许由于 Android 操作系统更新,其中一些提示不再必要.)

Important implementation hints

(Perhaps some of those hints aren't necessary anymore due to Android OS updates.)

  1. 某些设备,例如搭载 Android 4.3 的 Nexus 4 使用现有的 gatt 实例需要 45 秒以上的时间进行连接.解决方法:始终在断开连接时关闭 gatt 实例,并在每次连接时创建一个新的 gatt 实例.
  2. 不要忘记调用 android.bluetooth.BluetoothGatt#close()
  3. 在里面开始一个新线程 onLeScan(..) 然后连接.原因:BluetoothDevice#connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) 总是失败,如果在 LeScanCallback() {...}.onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) 在使用 Android 4.3 的三星 Galaxy S3 上的同一线程中(至少对于构建 JSS15J.I9300XXUGMK6)
  4. 大多数设备过滤广告
  5. 最好不要使用 android.bluetooth.BluetoothAdapter#startLeScan(UUID[] serviceUuids, LeScanCallback callback) 带有过滤某些服务UUID的参数 因为这在带有 Android 4.3 和 一般不适用于 128 位 UUID.
  6. Gatt 每次只能处理一个命令.如果几个命令一个接一个地被短时间调用,第一个被取消由于gatt 实现的同步特性.
  7. 我经常看到,即使在搭载 Android 5 的现代设备上,Wifi 也会干扰蓝牙,反之亦然.万不得已,请关闭 wifi 以稳定蓝牙.
  1. Some devices like Nexus 4 with Android 4.3 take 45+ seconds to connect using an existing gatt instance. Work around: Always close gatt instances on disconnect and create a fresh instance of gatt on each connect.
  2. Don't forget to call android.bluetooth.BluetoothGatt#close()
  3. Start a new thread inside onLeScan(..) and then connect. Reason: BluetoothDevice#connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) always fails, if called inside LeScanCallback() {...}.onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) in the same thread on Samsung Galaxy S3 with Android 4.3 (at least for build JSS15J.I9300XXUGMK6)
  4. Most devices filter advertising
  5. Better not use android.bluetooth.BluetoothAdapter#startLeScan(UUID[] serviceUuids, LeScanCallback callback) with the parameter to filter for certain service UUIDs because this is broken completely in Samsung Galaxy S3 with Android 4.3 and doesn't work for 128bit UUIDs in general.
  6. Gatt always can process one command at a time. If several commands get called short after another, the first one gets cancelled due to the synchronous nature of the gatt implementation.
  7. I often see even on modern devices with Android 5, that Wifi interferes withs bluetooth and vice versa. As a last resort, turn off wifi to stabilize bluetooth.

初学者教程

对于新手来说,一个相当不错的切入点可能是这个视频教程:为 Android 开发蓝牙智能应用程序 http://youtu.be/x1y4tEHDwk0

解决方法:我可以稳定"我的应用这样做...

Work around: I could "stabilize" my app doing that...

  1. 我为用户提供了一个重启蓝牙"设置.如果启用该设置,我会在指示 BLE 堆栈开始变得不稳定的某些点重新启动蓝牙.例如.如果 startScan 返回 false.如果 serviceDiscovery 失败,也可能是一个好点.我只是关闭和打开蓝牙.
  2. 我提供了另一个设置关闭 WiFi".如果启用了该设置,我的应用会在应用运行时关闭 Wifi(然后再打开)

这项工作基于以下经验...

This work around is based on follwoing experiences...

  • 在大多数情况下,重新启动蓝牙有助于解决 BLE 问题
  • 如果您关闭 Wifi,BLE 堆栈会变得更加稳定.但是,它在大多数打开 wifi 的设备上也能正常工作.
  • 如果您关闭 Wifi,在大多数情况下,重新启动蓝牙即可完全恢复 BLE 堆栈,而无需重新启动设备.

这篇关于Android 4.3 低功耗蓝牙不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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