I am working on a fitness app that is to supports two BLE sensors (heart rate, stride). I ended up with two bounded services (one for each sensor) called and bounded by my main activity. This is running on Nexus 7 II, with KitKat. I have spent days trying to get the app to stable state. The Gatt connections, and notifications work perfectly right after a clean reboot. The problem I am having is getting things back to a working state after one or both of the devices get disconnected and needs to be reconnected. Since similar issues have been reported by others, I have developed and tested various scenarios which included keeping the service and the connections going when the activity is restarted, closing the connections and the server and then starting fresh, closing only and not disconnecting, waiting before reconnecting, etc. Currently, with one sensor, I can get the app to reconnect most of the time. With two sensors, the connections do not get re-established most of the time. It is such a shame that android BLE API is so fragile considering that there are many new BLE devices every week. I am wondering if anyone else has had much luck with multiple BLE sensors and if they would share their approach to get it working.
-
Unfortunately, from my (relatively small) experience with BLE, I can only echo your frustration. The whole framework seems incredibly unstable. – Kevin Coppock Jan 21 '14 at 23:51
-
3I would suggest rewriting this to de-emphasize the ranting (title, second to last sentence) and focus more on the code that is not working (source, LogCat entries of relevance, etc.). – CommonsWare Jan 21 '14 at 23:54
-
I am in the same situation and have been trying different things since August. It's all to easy to get the system into a state where you have to reboot. Sometimes switching Bluetooth on and off helps but not always. I can get it into a state where the switching off fails and it comes back on 10 seconds latter... I would advice poking the various Android bug reports that are relevant. – Ifor Jan 22 '14 at 13:50
-
Forgot to say that sometimes it works perfectly especially after a reboot. I use HR and bike speed and cadence but users have had it going with bike power in the mix as well. Not sure if anyone has had 3 devices at once but two will work just not start reliably from a random state. e.g. the framework has serious bugs. – Ifor Jan 22 '14 at 14:03
-
That is EXACTLY my observation. I can get things to work after a reboot but as soon as once device gets disconnected or attempting to re-connect, the system becomes unstable. I never thought I would spend so much on this part of the app. The other annoying thing is that the logcat messages from the API don't make sense and sometimes onConnectionStateChanged I get a value that is not documented (0x85). I plan to explore the suggestion by Lo-Tan in this post [link](http://stackoverflow.com/questions/20069507/solved-gatt-callback-fails-to-register). Will report if I find anything useful. – Jason Porter Jan 22 '14 at 22:45
-
Interesting stuff from Lo-Tan. All my personal testing has been with a Nexus4 and I have enough trouble with that. I was getting the impression from users that things were better (but not perfect) on S4 and S3 devices. – Ifor Jan 23 '14 at 17:42
1 Answers
This isn't a very question-y question but your comment indicates knowing what 0x85 means would help. The Android BLE API was taken from Samsung's second BLE API, but not all of the constants Samsung had defined made it over.
0x85 = 133 (which you also see in the logs) is GATT_ERROR. Basically this means that something went wrong, it could be the peripheral went out of range of the device, or the Bluetooth chip just messed up. I've found calling connect() on the BluetoothGatt in onConnectionStateChange is a decent solution since that will wait for things to get sorted out and connect when it can.
0x8D = 141 GATT_ALREADY_OPEN This one is pretty self-explanatory.
The other thing to watch out for is making sure anything happening to Bluetooth happens sequentially. Multiple threads sending commands to a BluetoothGatt before the result of the previous action happens tends to not be a good thing.

- 2,522
- 1
- 23
- 27