How smart watches and smart bands that use BLE connectivity be able to control Music playback in android phones?

129 views Asked by At

I'm trying to implement two sample BLE applications. One application will be a client application and the other will be a peripheral application in two different devices. The client app will do a BLE scan for the available BLE devices and will connect to the BLE peripheral application which will start the GattServer. The main purpose of these two applications is to control the media playback happening in the client app device from any of the media playback applications. I have defined a custom BLE service and characteristics for the communication between the client app and peripheral app whenever the media playback status changes in the client app device or whenever user try to change the media playback status from the peripheral app. I make use of the notification feature and characteristics write feature for this two-way communication between the client app and peripheral app. The communication between the client app and peripheral app is working fine so far.

I tried to use the MediaSessionManager API to get the details of the media session used by the media application whenever a media playback happens. But in order to use this API we have to define the permission in android manifest file. Since this permission is a system app permission, I will not be able to generate apk to test in my phone. This made me wonder how these BLE smart bands are able to control the media playback happening in their coonnected android phones without having a system app installed on the corresponding android phone. Is there any other API out there that I'm not aware of which will serve my use case of awareness of media playback controls happening in a phone?

1

There are 1 answers

0
Daredevil On

I found the solution. In order to use MediaSession manager API for this use case without the system app permission, we need to create a notification listener and has to pass it to obtain the mediasession details without throwing the system app permission error:

val mediaSessionManager = getSystemService(MEDIA_SESSION_SERVICE) as MediaSessionManager
    val listenerComponent =
        ComponentName(this, NotificationListener::class.java);

    var mSessionsChangedListener: MediaSessionManager.OnActiveSessionsChangedListener =
        MediaSessionManager.OnActiveSessionsChangedListener {
            Log.e("MainActivity", "Total controller ${it?.size}")
        }

    mediaSessionManager.addOnActiveSessionsChangedListener(
        mSessionsChangedListener,
        listenerComponent
    )

    val sessions = mediaSessionManager.getActiveSessions(listenerComponent)

The above code will work fine without throwing the error.

NotificationListener.kt can be created as below:

public class NotificationListener : NotificationListenerService() {
     fun isEnabled(context: Context): Boolean {
        return NotificationManagerCompat
            .getEnabledListenerPackages(context)
            .contains(context.packageName)
    }
}

This way our application will be able to listen to the playback state changes of the media happening in another media application and also to know the metadata and to control the playback from our application as well.