Android Question Random Bluetooth connection failures after Tablet power cycle

SteveTerrell

Active Member
Licensed User
I am developing a B4A app on a Nexus 7 (5.0.2). The app communicates over Bluetooth with a paired HC-06 module and the admin and serial connection code is essentially what is in the Bluetooth tutorial.

The app is run manually when required. The problem I am having is that following power cycling the Tablet it appears to be a matter of luck/chance whether the app, when first run, will connect to the module. If it first connects following a power cycle then it will always connect (when the app is run) until power is cycled again. After that it may or may not connect.

If it fails to connect then it may connect following a power cycle or it may not. So far a work around is to "forget" the module then re pair it after which the connection succeeds.

Fail to connect means serial_connected is called with success=false and the last exception is java.io.IOException: read failed, socket might be closed.....

Although the connection fails the module can be found during admin discovery and of course it is in the Bluetooth settings list of paired devices.

I suspect this is not a B4A issue but would appreciate it if anyone can share any thoughts about a solution.
 

Erel

Administrator
Staff member
Licensed User
Have you tried to reset the bluetooth adapter with BluetoothAdmin? I've added this feature to B4A-Bridge and it sometimes solves similar issues.
 

SteveTerrell

Active Member
Licensed User
Have you tried to reset the bluetooth adapter with BluetoothAdmin? I've added this feature to B4A-Bridge and it sometimes solves similar issues.
Thank you for that suggestion. I had hoped that would help, it may help but it is not a reliable solution. Here are my observations so far.

The usual sequence is power up Tablet, power up the BT module then run the app.
Connection success or failure appears random with perhaps a bias towards success. If connection succeeds the app can be started and stopped multiple times and it will always connect.

If the connection fails it will probably also fail if another app like BlueTerm is tried. Restarting the app generally, but not always, still gives a fail to connect.
In this state however a PC can connect to the module without a problem.

Turning Bluetooth off then on again in the settings system screen then re-running the app usually still results in failure to connect.
I have tried a modified version of the program where BluetoothAdmin first disables Bluetooth then enables it (following the full turning off, turned off, turning on, turned on flow) but the connection still usually fails.

BluetootAdmin discovery always finds the module.

The only guaranteed way I seem to be able to connect is to manually un-pair the module then pair it again. Connection, so far, always succeeds after that.

I guess the next step is to try to un-pair and re-pair in the program.

For those that are interested I will report back. As usual any thoughts or suggestions welcomed.
 
Top