2

I have an Android application running on a Samsung Galaxy S phone. The application collects sensor data via bluetooth from a device. For the most part, there is no data being sent/received. I have set up my application so that it "auto-reconnects" with the Bluetooth device in case the connection is "lost".

I observe that after about 1.5 hours of my application running, the phone loses connection with the Bluetooth device and the auto-reconnect fails.

The sending of sensor data from the device to the phone is "mission critical". How can I ensure that the connection is not lost. The solution needs to be optimal. That is, to conserve battery, the phone needs to be able to sleep / hibernate.

2 Answers2

1

You should just listen for incoming connections when disconnected (use BluetoothServerSocket). It should run in a foreground service, that will never be stopped. This service will monitor the bluetooth connection state (disconnected, listening, connected) and spam threads for each situations (ASyncTasks).

The listening thread should do exactly as said in the Android documentation:

To create a listening BluetoothServerSocket that's ready for incoming connections, use BluetoothAdapter.listenUsingRfcommWithServiceRecord(). Then call accept() to listen for incoming connection requests.

To further conserve battery, user can turn off bluetooth - this will kill your listening thread but not the entire service (go into disconnected state). When user turns bluetooth back on, your service should notice the change and restart the listening thread.

Radu
  • 2,076
  • 2
  • 20
  • 40
0

A friend of mine had a similar problem, he uses now the Android app "BluetoothKeepalive" to avoid connection losts

Nikhil
  • 16,194
  • 20
  • 64
  • 81
Biber
  • 709
  • 6
  • 19