Stephen Lau wrote:
Anyone know how I can detect when the Bluetooth audio is disconnected? I
want to be able to pause the currently playing audio when it's
disconnected, similar to how I do for the ACTION_HEADSET_PLUG intent,
but can't figure out how to do it for a Bluetooth device.
Any ideas?
cheers,
steve
Nevermind, figured it out. Sample code below for anyone interested...
(obviously RDIOPlayer is my own media playing app... substitute with
whatever state query mechanism you have for your own app)
headphoneReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Boolean pausePlayer = false;
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", 0);
if (state == 0 && player.getState() == RDIOPlayer.STATE_PLAYING) {
Log.i(TAG, "Wired headset unplugged while player was playing.");
pausePlayer = true;
}
} else if
(intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
Log.i(TAG, "Bluetooth low level device disconnect requested");
BluetoothDevice btDevice =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (btDevice.getBluetoothClass().getMajorDeviceClass() ==
BluetoothClass.Device.Major.AUDIO_VIDEO && player.getState() ==
RDIOPlayer.STATE_PLAYING) {
Log.i(TAG, "Bluetooth audio/video device disconnected while
player was playing");
pausePlayer = true;
}
}
if (pausePlayer) {
// Do your thing here
}
}
};
IntentFilter headsetIntentFilter = new
IntentFilter(Intent.ACTION_HEADSET_PLUG);
headsetIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(headphoneReceiver, headsetIntentFilter);
--
stephen lau | [email protected] | http://whacked.net | @stevel
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en