Android Question Is it possible to get the list of connected bluetooth devices?

ema01

Member
Licensed User
Longtime User
Hi, i have a problem with a device using a HM-11 Module to communicate with the phone.
The application i wrote launches a service that, if not already connected, looks for devices with the required service/characteristic, then connects to them then does what it has to do. If the disconnected callback is called then it resets the state machine and goes back to looking for the devices.
Most of the time it's all working well but sometimes the following happens:
- Device powers on
- CONNECTED LED on device signals connection to the phone, but the phone can't find the device
- And, as strange as it may sound manual/forced exit from the app, turning off and on the bluetooth on the phone won't disconnect the device, only turning off the phone will (yeah, strange). Also, of course, power cycling the device.. but this is not possible to do at will.

Assume it's not a problem in the app. Believe me, i have checked EVERYTHING and put up an extensive logging mechanism to confirm that. At the very basic, on a search the ID of the device never comes up (and why would it, if it's connected it won't advertse anymore)

I have done everything i could to diagnose the issue, but there is this one that i want to try now: It is possible that there's an issue in the bluetooth module and accompaining hardware so that it is not initialized properly at a power cycle and it tries to connect automatically to the last known ID (so, the phone). I am testing this theory, but i can't modify all the boards already out there so i have also try to find out if
- This is indeed the problem (device connected BEFORE the app starts, or connected in a different process so the app doesn't execute the callback exhibit the same behaviour)
- I can fix it from the app

and to do so i have to get the list of connected devices. Let's say, when the app starts.
I have searched the forum and saw this thread https://www.b4x.com/android/forum/t...-are-connected-to-bluetooth.16555/#post-94499
I tried the code but
- s2 gets called only ONCE.
the testing method was:
connect to device using a BLE Scanner;
launch app;
s2 starts, logs, destroys itself.
Exit app;
launch app;
s2 will not start anymore, until i disconnect and reconnect to the device from the BLE Scanner.
- Even if i can get s2 to start every time, how do i disconnect from the device within the application?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
s2 will not start anymore, until i disconnect and reconnect to the device from the BLE Scanner.
This is expected.

Even if i can get s2 to start every time, how do i disconnect from the device within the application?
Try to turn off Bluetooth and then turn it back on. This can be done programmtically.
 
Upvote 0
Top