This topic pops up sometimes but i can't have that thing working. My app used to handle media button, but API after Android 5.0 changed and it need to adapt this app to "catch" bluetooth buttons and handle them "my way".
I've done a lot of searching and found that:
final MediaSession session = new MediaSession(this, "spy tag");
session.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
Toast.makeText(me, "on media button event", Toast.LENGTH_SHORT);
Log.i("TAG", "GOT EVENT");
return super.onMediaButtonEvent(mediaButtonIntent);
}
});
session.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackState state = new PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE |
PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE |
PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
session.setPlaybackState(state);
session.setActive(true);
but... it still doesn't work. whenever i press any media button i get this in Logcat:
2019-06-16 19:40:14.458 2936-4422/? D/MediaSessionService: Sending KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_MEDIA_NEXT, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to the last known PendingIntent PendingIntent{afe4538: PendingIntentRecord{13648e com.google.android.youtube broadcastIntent}} 2019-06-16 19:40:14.460 3230-3230/? V/Avrcp: recordKeyDispatched: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_MEDIA_NEXT, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } dispatched to com.google.android.youtube 2019-06-16 19:40:14.466 2936-4422/? D/MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_NEXT, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to the last known PendingIntent PendingIntent{afe4538: PendingIntentRecord{13648e com.google.android.youtube broadcastIntent}} 2019-06-16 19:40:14.475 3230-3230/? V/Avrcp: recordKeyDispatched: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_NEXT, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } dispatched to com.google.android.youtube
and my app is not receiving any callbacks. what am I missing?