1

My goal is to connect to multiple BLE sensor devices (Like http://www.blehome.com/sensorbug-PC.html) and subscribe to notification on various characteristics (like temperature or luminosity sensor) so I can display readings from multiple nearby locations on my app.

So far I've got the app and BLE Service working connecting to a single device. I've created a BleService class that is connected to my app Activity with a Binder and Intent object. The BleService class handles the GATT connections to a single device and contains the BluetoothGattCallback and then uses sendBroadcast(Intent) to relay the updated info from the device back to the app's BroadcastReceiver callback in the MainActivity.

Next step. Make it work with multiple devices. I scan for the devices I want to connect with based on their HW address and get a BluetoothDevice object from the ScanCallback. Then I try to create multiple BleService instances (one for each device) to handle the connections/GattCharacteristicRead and the all important BluetoothGattCallback for each device.

Problem: I can't figure out how to create multiple instances of the BleService class (one for each BLE device) by creating an Intent and using the bindService / startService calls so that the BLE operations occur separately from the Activity. It appears that using bindService / startService doesn't create multiple instances of the Service class which results in only the BluetoothDevice associated with the last service started making any Ble calls and connections.

Maybe I'm not supposed to use the Intent/bindService/startService paradigm in this situation, but when viewing the BLE docs/examples I find that that appears to be the prefered way for an apps Activity to interact with the BLE layer. Any suggestions/examples on how to connect with multiple BLE devices from Android would be appreciated.

I've seen that there is a limit to the number of BLE devices Android can connect to, I'm planning on connecting to 4 devices and it appears that the max number of connections is 7 for most Android phones or tablets so I should be under that limit.

John B
  • 13
  • 5
  • https://stackoverflow.com/questions/21237093/android-4-3-how-to-connect-to-multiple-bluetooth-low-energy-devices and https://stackoverflow.com/questions/47404018/android-ble-multiple-connections/47404489 – ecle Dec 05 '20 at 00:32
  • Thanks. That helps a bit although the suggestion to create a map to manage ble connection instances still results in the 129 status from onCharacteristicRead. As I'm a newbie to SO, whats the proper way to post a followup question with more detailed source to explain? Should I just create a new Question and resolve this as answered? – John B Dec 07 '20 at 22:36

0 Answers0