1 year ago

#57823

test-img

Viktor Pesic

Bluetooth headphone's buttons listener android, Play next and back works but volume up and down dont

I am trying to read Bluetooth Headphone's buttons input and I successfully made it so the play button next button and back button works but volume up and down won't work. So when I click play, it processed in onPlay(), but when I click next(hold volume up button) or back(hold volume down button) it is caught in onMediaButtonEvent() Just pressing volume up and down not working.

 public class BluetoothListener extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
private MediaSessionCompat.Callback callback = new MediaSessionCompat.Callback()
{
    @Override
    public void onPlay() {
        super.onPlay();
        Toast.makeText(getApplication(),"Play Button is pressed!",Toast.LENGTH_SHORT).show();
        Log.d("RACKU","racku");
    }



    @Override
    public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
        String intentAction = mediaButtonEvent.getAction();
        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction))
        {
            KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (event != null)
            {


                int action = event.getAction();
                if (action == KeyEvent.ACTION_DOWN) {
                    switch (event.getKeyCode()) {
                        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                            Toast.makeText(getApplication(),"Fast forward is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_MEDIA_NEXT:
                            Toast.makeText(getApplication(),"Next is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                            Toast.makeText(getApplication(),"Play is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                            Toast.makeText(getApplication(),"Previous is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_MEDIA_REWIND:
                            Toast.makeText(getApplication(),"Rewind is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_MEDIA_STOP:
                            Toast.makeText(getApplication(),"Stop Button is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_VOLUME_UP:
                            Toast.makeText(getApplication(),"Volume up is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                        case KeyEvent.KEYCODE_VOLUME_DOWN:
                            Toast.makeText(getApplication(),"Volume down is pressed!", Toast.LENGTH_SHORT).show();
                            return true;
                    }
                    return false;

                }
            }
        }
        return super.onMediaButtonEvent(mediaButtonEvent);
    }
};

private MediaSessionCompat mediaSession;

@Override
public void onCreate() {
    mediaSession = new MediaSessionCompat(this, "MEDIA");
    mediaSession.setCallback(callback);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    PlaybackStateCompat.Builder mStateBuilder = new PlaybackStateCompat.Builder()
            .setActions(
                    PlaybackStateCompat.ACTION_PLAY |
                            PlaybackStateCompat.ACTION_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                            PlaybackStateCompat.ACTION_PLAY_PAUSE);

    mediaSession.setPlaybackState(mStateBuilder.build());
    mediaSession.setActive(true);
}

@Override
public void onDestroy() {
    mediaSession.release();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MediaButtonReceiver.handleIntent(mediaSession, intent);
    return super.onStartCommand(intent, flags, startId);
}
 }

Android manifest:

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



    <uses-feature android:name="android.hardware.bluetooth" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <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/Theme.Hp">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    

        <receiver android:name="androidx.media.session.MediaButtonReceiver" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
                <action android:name="android.media.VOLUME_CHANGED_ACTION" />
            </intent-filter>
        </receiver>

        <service android:name="BluetoothListener" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
                <action android:name="android.media.VOLUME_CHANGED_ACTION" />
            </intent-filter>
        </service>
    </application>

</manifest>

java

android

kotlin

bluetooth

android-mediasession

0 Answers

Your Answer

Accepted video resources