[android-developers] How to detect Bluetooth headset key press

2679 views
Skip to first unread message

Doug

unread,
Apr 27, 2010, 10:03:08 PM4/27/10
to Android Developers
Hi All,

I'm new to Bluetooth programming and I was wondering if I had a
Bluetooth headset that was paired to an Android phone. What's the
best way to detect that a button was pressed? Are there modifiers to
tell which kind of button it was (e.g. volume up/down, main button) or
what kind of press it was(regular, long press etc).

Thanks!
Doug

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-develop...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

mort

unread,
Apr 29, 2010, 2:31:16 AM4/29/10
to Android Developers
Hi,

it's pretty simple: add a broadcast listener to MEDIA_BUTTON:
<intent-filter android:priority="<some number>">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>

You need to give it a "priority", which decides whether it's handled
before or after other apps. In other words, it's pure coincidence who
picked the highest number...
If you handled the button press, you should abort the broadcast with
abortBroadcast(). Trouble with this handling is, the priorities and
abortBroadcast() work fine as long as each app only responds while
e.g. something is played. But several users also expect a "default
player" to be launched (or start playing) upon button press, like the
default player, so it might happen some app with a higher priority
number won't let the intent come through to your app... (I already
filed an issue to improve that: http://code.google.com/p/android/issues/detail?id=7772
- no response so far...)

In the onReceive, you can get the button event with
KeyEvent key = (KeyEvent)
intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

key.getKeyAction() tells you whether the button was released or
pressed, key.getKeyCode() tells which button. The values are
documented and pretty self explaining, like KeyEvent.ACTION_DOWN or
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE.
Regarding press and release, keep in mind several headsets do own
processing which prevents this (like e.g. closing the connection or
sending "stop" on long press on play/pause) or simply send both at
once when the key is pressed or released. Also, some BT drivers are a
bit unreliable in that regard...

If you want to handle single button cable headsets as well, also
regard the key code KEYCODE_HEADSETHOOK.

btw: HTC Hero doesn't implement this standard, it needs a workaround
app.

HTH,
Mirko

gcstang

unread,
Apr 29, 2010, 8:20:07 AM4/29/10
to Android Developers
I thought this was disabled at the OS level....otherwise wouldn't we
be able to press to start a dialing app on our BT Headsets?

Jeffrey Palm

unread,
May 24, 2010, 11:26:15 PM5/24/10
to Android Developers
You can override the onKeyDown method in any activity and check for
the KeyEvent.KEYCODE_MEDIA_* key codes. e.g.

boolean onKeyDown(int keyCode, KeyEvent event) {
AudibleReadyPlayer p;
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
// something for fast forward
return true;
case KeyEvent.KEYCODE_MEDIA_NEXT:
// something for next
return true;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
// something for play/pause
return true;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
// something for previous
return true;
case KeyEvent.KEYCODE_MEDIA_REWIND:
// something for rewind
return true;
case KeyEvent.KEYCODE_MEDIA_STOP:
// something for stop
return true;
}
return false;
}

Jeff
Reply all
Reply to author
Forward
0 new messages