Turning on Bluetooth starts MediaBrowserServiceCompat

518 views Asked by At

When I turn on bluetooth on my OnePlus, then the MediaBrowserServiceCompat starts.

My first logcat entry comes from onCreate() method in my class extending MediaBrowserServiceCompat. Several other media player apps fire up as well. There is not much more info in logcat about why and how the Bluetooth fires up the MediaBrowserServiceCompat. Thus, I am lost. The behavior is not completely reproducible, but it happens in 9/10 times, especially if my Bluetooth headphones are on. Just connecting the headphones does not result in the same behavior. Only turning the Bluetooth on on the device does.

What causes MediaBrowserServiceCompat to start when I turn Bluetooth on or how do I figure what causes it? In turn, how do I prevent such behavior?

1

There are 1 answers

3
A-run On

According to https://developer.android.com/guide/topics/media-apps/mediabuttons#restarting-inactive-mediasessions

If Android can identify the last active media session, it tries to restart the session by sending an ACTION_MEDIA_BUTTON Intent to a manifest-registered component (such as a service or BroadcastReceiver). This lets your app restart playback while its UI is not visible, which is the case for most audio apps.

I think in your case, since your application was the last active MediaSession the system is trying to restart the MediaBrowserService. Try debugging your application by switching to other Media applications (then connect BT) and then check whether the System starts your service or not.

Also, check https://developer.android.com/guide/topics/media-apps/mediabuttons for more information.