diff options
author | Scott Jackson <daneren2005@gmail.com> | 2012-10-19 22:00:34 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2012-10-19 22:00:34 -0700 |
commit | 79f51fdff7a0f862f9d1aa1e9167c755cfebdf94 (patch) | |
tree | 6135734230aa16ddf08307d742a59abe0550a4b8 /subsonic-android/src/github | |
parent | 12f49b1f5234c57846a4e3c52cc891c2f1e3f0da (diff) | |
download | dsub-79f51fdff7a0f862f9d1aa1e9167c755cfebdf94.tar.gz dsub-79f51fdff7a0f862f9d1aa1e9167c755cfebdf94.tar.bz2 dsub-79f51fdff7a0f862f9d1aa1e9167c755cfebdf94.zip |
Detect Bluetooth connect/disconnect in ICS
Diffstat (limited to 'subsonic-android/src/github')
-rw-r--r-- | subsonic-android/src/github/daneren2005/dsub/receiver/BluetoothIntentReceiver.java | 58 |
1 files changed, 38 insertions, 20 deletions
diff --git a/subsonic-android/src/github/daneren2005/dsub/receiver/BluetoothIntentReceiver.java b/subsonic-android/src/github/daneren2005/dsub/receiver/BluetoothIntentReceiver.java index b87b6c6c..5b65760a 100644 --- a/subsonic-android/src/github/daneren2005/dsub/receiver/BluetoothIntentReceiver.java +++ b/subsonic-android/src/github/daneren2005/dsub/receiver/BluetoothIntentReceiver.java @@ -31,23 +31,41 @@ import github.daneren2005.dsub.util.Util; * @author Sindre Mehus */ public class BluetoothIntentReceiver extends BroadcastReceiver { - - private static final String TAG = BluetoothIntentReceiver.class.getSimpleName(); - - @Override - public void onReceive(Context context, Intent intent) { - int state = intent.getIntExtra("android.bluetooth.a2dp.extra.SINK_STATE", -1); - Log.i(TAG, "android.bluetooth.a2dp.extra.SINK_STATE, state = " + state); - boolean connected = state == 2; // android.bluetooth.BluetoothA2dp.STATE_CONNECTED - if (connected) { - Log.i(TAG, "Connected to Bluetooth A2DP, requesting media button focus."); - Util.registerMediaButtonEventReceiver(context); - } - - boolean disconnected = state == 0; // android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED - if (disconnected) { - Log.i(TAG, "Disconnected from Bluetooth A2DP, requesting pause."); - context.sendBroadcast(new Intent(DownloadServiceImpl.CMD_PAUSE)); - } - } -}
\ No newline at end of file + private static final String TAG = BluetoothIntentReceiver.class.getSimpleName(); + // Same as constants in android.bluetooth.BluetoothProfile, which is API level 11. + private static final int STATE_DISCONNECTED = 0; + private static final int STATE_CONNECTED = 2; + @Override + public void onReceive(Context context, Intent intent) { + Log.i(TAG, "GOT INTENT " + intent); + if (isConnected(intent)) { + Log.i(TAG, "Connected to Bluetooth A2DP, requesting media button focus."); + Util.registerMediaButtonEventReceiver(context); + } else if (isDisconnected(intent)) { + Log.i(TAG, "Disconnected from Bluetooth A2DP, requesting pause."); + context.sendBroadcast(new Intent(DownloadServiceImpl.CMD_PAUSE)); + } + } + private boolean isConnected(Intent intent) { + if ("android.bluetooth.a2dp.action.SINK_STATE_CHANGED".equals(intent.getAction()) && + intent.getIntExtra("android.bluetooth.a2dp.extra.SINK_STATE", -1) == STATE_CONNECTED) { + return true; + } + if ("android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED".equals(intent.getAction()) && + intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1) == STATE_CONNECTED) { + return true; + } + return false; + } + private boolean isDisconnected(Intent intent) { + if ("android.bluetooth.a2dp.action.SINK_STATE_CHANGED".equals(intent.getAction()) && + intent.getIntExtra("android.bluetooth.a2dp.extra.SINK_STATE", -1) == STATE_DISCONNECTED) { + return true; + } + if ("android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED".equals(intent.getAction()) && + intent.getIntExtra("android.bluetooth.profile.extra.STATE", -1) == STATE_DISCONNECTED) { + return true; + } + return false; + } +}
\ No newline at end of file |