Android - Bluetooth Device Connected Broadcast
I want to have a broadcast receiver listening for BT devices connecting. Could anybody tell me which broadcast I have to listen to? I tried android.bluetooth.device.action.ACL_CONN
Solution 1:
If you are looking whether the device is "connecting" then you'd want android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED
However, this does not get triggered when the device is STATE_CONNECTED. So what I did was when the state is connecting, to send a broadcast in a few seconds.
if (bluetoothAdapter
.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_CONNECTING
|| bluetoothAdapter
.getProfileConnectionState(BluetoothProfile.A2DP) == BluetoothProfile.STATE_CONNECTING) {
finalHandlerhandler=newHandler();
handler.postDelayed(newRunnable() {
@Overridepublicvoidrun() {
if (bluetoothAdapter
.getProfileConnectionState(BluetoothProfile.HEADSET) != BluetoothProfile.STATE_CONNECTING
|| bluetoothAdapter
.getProfileConnectionState(BluetoothProfile.A2DP) != BluetoothProfile.STATE_CONNECTING) {
context.sendBroadcast(newIntent(
BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED));
}
}
}, 2000);
Kind of hackish, which is why I posted this issue in the Android defect tracker. http://code.google.com/p/android/issues/detail?id=25957
Solution 2:
You can pull the device by using the following inside your BroadcastReceiver.onReceive()
function:
BluetoothDevicedevice= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Post a Comment for "Android - Bluetooth Device Connected Broadcast"