I want to connect multiple ble device and do read write operation parallely?
4 Answers
Our experience with BLE on Android was clearly indicating the multi threaded operations on the Android BLE stack can cause many issues.. ;)
I would suggest you to do one thing at a time and try to avoid parallel work...
Our 2 cents...

- 1,159
- 8
- 11
-
Thanks for your answer :) – Parth Apr 29 '16 at 09:44
-
@p2pkit "I would suggest you to do one thing at a time and try to avoid parallel work..." - would you recommend this on a per-connection basis, or across all connections being maintained by the app (if I'm connected to more than one device simultaneously)? i.e. this question: http://stackoverflow.com/questions/28016571/robustly-communicating-with-multiple-ble-devices-simultaneously-on-android – stkent Feb 22 '17 at 18:50
-
2I recommend to do only one thing across all connections and if possible open only one connection. In our experience parallel connections led to a lot of issues. – p2pkit Mar 07 '17 at 10:44
To be able to connect to multiple devices at the same time, you have to use the auto connect gatt feature, rather than direct connect (http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback)).
If you use auto connect, the phone can initiate multiple connections at the same time. If you use direct connect, only one connection can be initiated at the same time.
Once you are connected however, you can execute all gatt operations in parallel, although you can only have one outstanding gatt operation at a time per peripheral.

- 16,784
- 2
- 41
- 52
-
1This is partially true. Autoconnect flag doesn't affect parallel connections. You can do them in direct mode as well. We tested it during the library development (RxAndroidBLE). You should be aware that you should queue calls within the connection, but the library does it for you. – pawel.urban Jul 22 '16 at 09:03
-
1How do you mean? If you use direct connect rather than auto connect, it will only attempt to connect to one device at a time. At least for standard Android on Nexus devices. Just look at the hci snooplog. – Emil Jul 22 '16 at 12:27
-
I mean that you can use a queue and connect one after another in the direct mode. Direct connection init speed is much faster. – pawel.urban Jul 22 '16 at 16:27
There are several libs, that give you a possibility to do this, without too much hassle.
RXAndroidBLE is a great piece of work for example. But if you're using Kotlin - there is something easier: BleGattCoroutines.
Basically it uses the Kotlin's coroutines to wrap async (and ugly) Android BLE API into the plain sync-style calls.
Disclaimer: I don't use this lib itself, but use the same approach in my recent commercial embedded project. And it works like a charm.

- 186
- 2
- 7
This is one of the better repos I've found for working with the BLE stack in Android. But it doesn't give parallel processing of the operations. I think if you're working at the SDK level, there really is no way to do parallel operations because you don't have any low level control.
There is a new RXAndroidBLE library, I haven't used it, but it may be helpful. There was some discussion about it here. They apparently were able to connect 5 sensors simultaneously and process data...Although it would have still been sequential.

- 288
- 2
- 7
-
-
FWIW, I have an app that can connect to 5 BLE devices, and it uses a very similar approach to the first link. The basic idea is just setup a queue for processing callbacks as they come in. Of course that is the very high level summary! – Matt Brown Apr 29 '16 at 09:47