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.
Whenever a device is disconnected I make sure to call close() on the BluetoothGatt object and set it to 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
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. 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.
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?
L2CA_EnableUpdateBleConnParams - unknown BD_ADDR <your device address>
which doesn't occour in your "success"-log. Same applies to my testing experiences. However, this is just a consequence of the disconnect, which is stated some lines earlier at 124 withGATT ATT protocol channel with BDA: 9059af043336 is disconnected
I have a BlueGiga chip by the way. – MariselamarishUnknown conn ID:
as you got in pastebin.com/AhZq17bU I get this soon afterregisterApp()
. At the 10th time I getUnknown conn ID: 10 D/BtGatt.btif(1077): btgattc_handle_event: Event 1000 E/bt-btif(1077): Register with GATT stack failed. failed. (about 20 times) D/BtGatt.btif(1077): btif_gattc_upstreams_evt: Event 0 E/BtGatt.btif(1077): bta_to_btif_uuid: Unknown UUID length 10! D/BtGatt.GattService(1077): onClientRegistered() - UUID=00000000-408a-6f08-ffff-fe58b0e98d19, clientIf=0 E/BtGatt.ContextMap(1077): Context not found for UUID 00000000-408a-6f08-ffff-fe58b0e98d19
– Mariselamarishsdk\samples\android-18\legacy\BluetoothLeGatt
which you can download via the Android SDK Manager – Mariselamarish