问题:
I am currently developing an application that will use Bluetooth Low Energy (testing on the Nexus 4). 我目前正在开发一种将使用蓝牙低功耗的应用程序(在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. 在开始使用Android 4.3中的官方BLE API之后,我注意到在我第一次连接设备后,我很少能够成功连接到该设备或与该设备或任何其他设备进行通信。
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. 一旦发生这种情况,我必须卸载应用程序,禁用蓝牙,然后重新启动手机才能再次开始工作。
Whenever a device is disconnected I make sure to call close() on the BluetoothGatt object and set it to null. 每当设备断开连接时,我都要确保在BluetoothGatt对象上调用close()并将其设置为null。 Any insights? 任何见解?
EDIT: 编辑:
Log dumps: For these logs I rooted my phone and upped the trace levels of related items in /etc/bluetooth/bt_stack.conf 日志转储:对于这些日志,我根据我的手机并在/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. 我能够连接,发现所有服务/特性,以及读/写。
Failed Attempt 1 - This is the next attempt after disconnecting from the successful connection above. 失败尝试1 - 这是断开上述成功连接后的下一次尝试。 It seems I was able to discover characteristics, but the first attempt to read returned a null value and disconnected soon thereafter. 似乎我能够发现特征,但是第一次尝试读取时返回了一个空值并且很快就断开了。
Failed Attempt 2 - An example where I am not even able to discover services/characteristics. 失败的尝试2 - 我甚至无法发现服务/特征的示例。
EDIT 2: 编辑2:
The device to which I am trying to connect is based on TI's CC2541 chip. 我尝试连接的设备基于TI的CC2541芯片。 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. 我获得了TI SensorTag (也基于CC2541),并发现TI昨天为SensorTag发布了一个Android应用程序 。 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. 我在另外两个Nexus 4上进行了测试,结果相同:第一次或第二次连接SensorTag成功,但是(根据日志)之后无法发现服务,导致各种崩溃。 I'm starting to wonder if it's an issue with this specific chip? 我开始怀疑这个特定芯片是否存在问题?
解决方案:
参考一: https://en.stackoom.com/question/1Cyqr参考二: https://stackoom.com/question/1Cyqr