How to capture key events from bluetooth headset with android

Add a broadcast listener to MEDIA_BUTTON:

<intent-filter android:priority="<some number>"> 
   <action android:name="android.intent.action.MEDIA_BUTTON" /> 
</intent-filter> 

You should register your broadcast receiver inside your application (not in manifest file). Otherwise Google Music player will catch your broadcast and aboard it.

Your IntentFilter priority should be higher that other media players priorities in your phone)

Add android.permission.BLUETOOTH permission in manifest to support Bluetooth headset

After received you key you have to manually abort the broadcast using abortBroadcast().

However 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

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 is pressed.

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

Override the onKeyDown method in any activity and check for
the KeyEvent.KEYCODE_MEDIA_KEYCODE_pressed_key

e.g.

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

Volume key integration example
Android – Volume Buttons used in my application

This one may need permission

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Or you can try slimier implementations over the following link

Android Developer Blog : Handling remote control buttons
Android Tales : Add Headset button support to your Android application

Leave a Comment