0

I need to detect the bluetooth device button click in my application. i followed many stackoverflow links, but doesn't seem to work for me.

I am using the broadcast receiver as shown below:

public class RemoteControlReceiver extends BroadcastReceiver {    
    @Override    
    public void onReceive(Context context, Intent intent) {    
        if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {    
            KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);    
            if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {    
                //call my method    
            }    
        }    
    }    
}

and my manifest is as follows:

<receiver android:name=".RemoteControlReceiver" >    
   <intent-filter>    
       <action android:name="android.intent.action.MEDIA_BUTTON" />    
       <category android:name="android.intent.category.DEFAULT"/>    
   </intent-filter>    
</receiver>

can any one suggest a way out? Thanks in advance.

user851296
  • 121
  • 3
  • 6
  • hey i've been looking for a solution for days as well and this link worked for me, have a look if u haven yet solve it http://stackoverflow.com/questions/6287116/android-registering-a-headset-button-click-with-broadcastreceiver – moltencrap Sep 30 '13 at 13:01

1 Answers1

0

Is your API level at least 11? The code KEYCODE_MEDIA_PLAY was added in API level 11. The KEYCODE_MEDIA_PLAY_PAUSE code exists since API level 3.

Also, have you tried to configure your intent filter without specifying a category?

Is your RemoteControlReceiver class in the root package of your application? It might have not been able to find ".RemoteControlReceiver".

Other than that, I can't see where you could be doing anything wrong.

I've read in a few posts that you might have to also call registerMediaButtonEventReceiver and unregisterMediaButtonEventReceiver. Have you tried this?

To register:

audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
componentName = new ComponentName(getPackageName(),
    RemoteControlReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(componentName);

And to unregister:

audioManager.unregisterMediaButtonEventReceiver(componentName);
Alex Machado
  • 1,171
  • 1
  • 12
  • 22