How to turn-on SpeakerPhone for incoming call in Xamarin.Forms Android 13

Universal Camera 276 Reputation points
2022-10-18T17:44:34.07+00:00

I used this posting

https://learn.microsoft.com/en-us/answers/questions/1049896/how-to-turn-on-speakerphone-during-a-call-in-xamar.html

I integrated it into my PhoneCallListener BroadcastReceiver to turn-on the SpeakerPhone on Android13 Unsuccessfully.

[BroadcastReceiver(Enabled = true, Exported = true)]  
[IntentFilter(new[] { TelephonyManager.ActionPhoneStateChanged }, Priority = (int)IntentFilterPriority.HighPriority)]  
public class PhoneCallListener : BroadcastReceiver  
{  
    public override void OnReceive(Context context, Intent intent)  
    {  
        var state = intent.GetStringExtra(TelephonyManager.ExtraState);  
        AudioManager audioManager = (AudioManager)Android.App.Application.Context.GetSystemService(Context.AudioService);  

        if (intent.Action == TelephonyManager.ActionPhoneStateChanged)  
        {  
            if (state == TelephonyManager.ExtraStateOffhook)  
            {  
                Task.Run(() =>  
                {  
                    Task.Delay(5000).Wait();  
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>  
                    {  
                        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.S)  
                        {  
                            Utils.setCommunicationDevice(context, AudioDeviceType.BuiltinSpeaker);  
                        }  
                        else  
                        {  
                            audioManager.SpeakerphoneOn = true;  
                        }  
                    });  
                });  
            }  
        }  
    }  
}  

This works for Android11 and lower but NOT for Android12&13

Some Insight PLEASE!!!!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,316 questions
{count} votes