I'm trying to find out if I can reliably detect when the bluetooth connection with a (non-audio) device has been lost. The Android SDK provides a Bluetooth Chat example, which uses this snippet:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
How reliable is this method when I start the thread and disconnect the device 12 hours later without using the app (The service runs in background). The service could always be killed ofcourse?
Then there are broadcasts. Will my app always receive these broadcasts? When will it not? I've tried implementing that by using this in the AndroidManifest:
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>
I've tested both methods in a short time period, and both work. As said before, I'm not sure if these are reliable when the phone is idle for a long time.
Who can shed a light on this?