問題:
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