MediaSession.SetMetadata doesn't work on bluetooth device

José Santos 1 Reputation point
2021-07-25T11:41:26.477+00:00

I am developing an application in Xamarin Android to play music in streaming. When setting the metadata, they are updated and displayed correctly in the mobile notification, but when I connect it to a device via bluetooth such as the car, the metadata is not updated. Could someone tell me why?

The code where I create the MediaSession and update the metadata is as follows.

private void InitMediaSession()
{
    MediaSession = new MediaSessionCompat(ApplicationContext, ApplicationContext.PackageName);

    Intent intent = PackageManager?.GetLaunchIntentForPackage(PackageName);
    PendingIntent pendingIntent = PendingIntent.GetActivity(ApplicationContext, 0, intent, PendingIntentFlags.UpdateCurrent);
    MediaSession.SetSessionActivity(pendingIntent);

    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        MediaSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls);
    }

    MediaSession.Active = true;
    SessionToken = MediaSession.SessionToken;
}

private void UpdateMetadata(string title, string artist)
{
    var metadataBuilder = new MediaMetadataCompat.Builder();
    metadataBuilder.PutString(MediaMetadata.MetadataKeyTitle, title)
                   .PutString(MediaMetadata.MetadataKeyArtist, artist);

    var metadata = metadataBuilder.Build();
    MediaSession.SetMetadata(metadata);
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,354 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.