Android的4.3蓝牙低能量不稳定 [英] Android 4.3 Bluetooth Low Energy unstable

查看:695
本文介绍了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.

当一个设备断开我一定要调用close()的BluetoothGatt对象,并将其设置为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芯片。我得到的<一个href="http://www.ti.com/ww/en/wireless_connectivity/sensortag/index.shtml?DCMP=sensortag&HQS=sensortag-bn">TI SensorTag (也是基于CC2541),玩弄,发现TI发布的一个Android应用程序的SensorTag昨天。但是,这个程序有同样的问题我​​测试了其他两个的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操作系统的更新。

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

  1. 如Nexus 4与Android 4.3 拿45+秒,连接使用现有的关贸总协定实例。解决方法:始终关闭关贸总协定实例断开连接,并创建关贸总协定各连接一个新的实例。
  2. 请不要忘记调用 android.bluetooth.BluetoothGatt#关闭()
  3. 重要的三星Galaxy S3采用Android 4.3 (至少对于构建JSS15J.I9300XXUGMK6): BluetoothDevice类#connectGatt(上下文的背景下,布尔自动连接,BluetoothGattCallback回调)总是失败,如果调用里面 LeScanCallback(){...}。onLeScan(BluetoothDevice类设备,诠释RSSI,byte []的scanRecord)在同一个线程。解决:启动一个新的线程 onLeScan(..)内,然后连接
  4. <一个href="http://stackoverflow.com/questions/19502853/android-4-3-ble-filtering-behaviour-of-startlescan">Most设备过滤广告
  5. 最好不要使用 android.bluetooth.BluetoothAdapter#startLeScan(UUID [] serviceUuids,LeScanCallback回调)的参数来对某些服务的UUID过滤器,因为这是完全打破三星Galaxy S3采用Android 4.3和<一href="http://stackoverflow.com/questions/18019161/startlescan-with-128-bit-uuids-doesnt-work-on-native-android-ble-implementation?lq=1">doesn't对于128位的UUID工作的一般。
  6. 盖特总是可以处理一次一个命令。如果多个命令被调用后,另一个短,第一个被取消<一href="http://stackoverflow.com/questions/18011816/has-native-android-ble-gatt-implementation-synchronous-nature">due关贸总协定执行的同步性。
  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. Important for Samsung Galaxy S3 with Android 4.3 (at least for build JSS15J.I9300XXUGMK6): 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. Work around: Start a new thread inside onLeScan(..) and then connect.
  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.

一个pretty的确定切入点,新人可能是这个视频教程:开发蓝牙智能应用程序为Android HTTP: //youtu.be/x1y4tEHDwk0

A pretty OK entry point for newcomers could be this video tutorial: Developing Bluetooth Smart Applications for Android http://youtu.be/x1y4tEHDwk0

的问题与工作围绕以下描述可能是由操作系统的更新已经得到解决:

解决方法:我能稳住我的应用程序这样做......

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

  1. 在我公司提供的用户设置重新启动蓝牙。如果设置已启用,我重新启动蓝牙在某些点上,表明BLE堆栈开始变得不稳定。例如。如果startScan返回false。好点的也可能是,如果serviceDiscovery failes。我只是把蓝牙和关闭。
  2. 在我提供了另一种设置打开WiFi的。如果设置已启用,我的应用程序关闭无线应用程序运行时(并重新开启该选项之后)

这工作大约是以follwoing经验...

This work around is based on follwoing experiences...

  • 重新启动蓝牙帮助解决问题BLE在大多数情况下
  • 如果您关闭WiFi的BLE堆栈变得更加稳定。然而,它也能正常工作在大多数的设备具有WiFi打开。
  • 如果您关闭WiFi,蓝牙重启全面复苏的BLE堆栈,而不需要重启设备,在大多数情况下。

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

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