2

What is the proper way to handle Bluetooth media buttons in an Android app? At this point, all I want is an event to fire when I press the pause/play button on my Bluetooth headset. Somehow, I had a working solution yesterday, and today it just doesn't work. Without reinstalling the app, or any update happening on my phone, the onReceive method of my subclass of BroadcastReceiver is never entered. I'm targeting Android 9.0, since this is just for myself.

I used the information in these two questions for the solution:

How to capture key events from bluetooth headset with android

BroadcastReceiver for ACTION_MEDIA_BUTTON not working

This is a bare minimum version of what was working yesterday, which currently does not work:

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.myapplication">

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

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            tools:ignore="GoogleAppIndexingWarning">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".MediaButtonIntentReceiver">
                <intent-filter android:priority="2139999999">
                    <action android:name="android.intent.action.MEDIA_BUTTON" />
                </intent-filter>
            </receiver>
        </application>

    </manifest>

MainActivity.java

    package com.example.myapplication;

    import androidx.appcompat.app.AppCompatActivity;

    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.media.AudioManager;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

        MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
        IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);

        AudioManager mAudioManager = null;
        ComponentName mReceiverComponent = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mAudioManager =  (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            mReceiverComponent = new ComponentName(this, MediaButtonIntentReceiver.class);

            mediaFilter.setPriority(2139999999);
            registerReceiver(mMediaButtonReceiver, mediaFilter);
        }
    }

MediaButtonIntentReceiver.java

    package com.example.myapplication;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;

    public class MediaButtonIntentReceiver extends BroadcastReceiver {

        public MediaButtonIntentReceiver() {
            super();
            Log.i("mylog",  "init");
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("mylog", "receive");
            abortBroadcast();
        }
    }
John
  • 35
  • 7
  • Any luck with this? I need to get the hook button activity from the headset. – Andrew Lindzon Mar 14 '20 at 12:24
  • 1
    @AndrewLindzon Nope, I got the classic stack overflow response to anything that isn't "How do I add two numbers?", aka nothing. I've given up on the android app completely because Android 10 disabled reading music files from an absolute path and there's no documentation on how to get and play music from a playlist without it. – John Mar 15 '20 at 20:15

0 Answers0