Reputation: 100358
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?
Upvotes: 1
Views: 4793
Reputation: 185
Every activity within bluetooth has a timeout associated with it. It varies from device to device.
Bluetooth service will run in the background as long as you have enabled it. ACL [Asynchronous Connection Less] packet is generally applied for transferring non-voice related data. ACL is just as connection channel between two bluetooth device. This may get disconnected based on inactivity timer or timeout value.
you can read the connection timeout set when HCI createConnection API is called.This info can be obtained by executing hcidump tool.http://www.bluez.org/download/
Upvotes: 1