Audio stops after startBluetoothSco in Samsung

The moment I bind a Samsung Android 10+ device using startBluetoothSco I loose audio. It works fine with other devices irrespective of Android version.

Sample code I tried with below.

Audio works as it should when i do not bind using startBluetoothSco. I did try changing the TTS audio between Samsung (default) and google. Nothing helps.

package com.example.ttssample;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    public static final Integer RecordAudioRequestCode = 1;
    Intent speechRecognizerIntent;
    private EditText         editText;
    private TextToSpeech     textToSpeech;
    private EditText         editText2;
    private SpeechRecognizer speechRecognizer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            checkPermission();
        }

        editText     = findViewById(R.id.editText);
        textToSpeech = new TextToSpeech(getApplicationContext(), i -> textToSpeech.setLanguage(Locale.ENGLISH));

        editText2              = findViewById(R.id.editTextRecord);
        speechRecognizer       = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        speechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

            @Override
            public void onBufferReceived(byte[] bytes) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                ArrayList<String> matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                if (matches != null)
                    editText2.setText(matches.get(0));
            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        });

        findViewById(R.id.btnRecord).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {

                    case MotionEvent.ACTION_UP:
                        speechRecognizer.stopListening();
                        editText2.setHint("You will see the input here");
                        break;

                    case MotionEvent.ACTION_DOWN:
                        editText2.setText("");
                        editText2.setHint("Listening...");
                        speechRecognizer.startListening(speechRecognizerIntent);
                        break;
                }
                return false;
            }
        });

    }

    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, RecordAudioRequestCode);
        }
    }

    public void TextToSpeechButton(View view) {
        textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null, null);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == RecordAudioRequestCode && grantResults.length > 0) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
        }
    }

    public void doBind(View view) {
        redirectToBTHeadset();
    }

    private void redirectToBTHeadset() {
        final AudioManager localAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (localAudioManager.isBluetoothScoOn())
            return;
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        if (btAdapter != null && btAdapter.isEnabled() && btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_CONNECTED) {
            if (localAudioManager.isBluetoothScoAvailableOffCall()) {
                Bundle extrasBundle = registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        int conState = intent.getExtras().getInt(AudioManager.EXTRA_SCO_AUDIO_STATE);
                        if (conState == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
                            localAudioManager.setBluetoothScoOn(true);
                            context.unregisterReceiver(this);
                        } else {
                            if (conState == AudioManager.SCO_AUDIO_STATE_CONNECTING) {
                                System.out.println("Bluetooth Receiver :SCO Connecting....");
                            } else if (conState == AudioManager.SCO_AUDIO_STATE_ERROR) {
                                System.out.println("Bluetooth Receiver : SCO Error.");
                                context.unregisterReceiver(this);
                            } else if (conState == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
                                System.out.println("Bluetooth Receiver :SCO Disconnected");
                                localAudioManager.setBluetoothScoOn(false);
                            }
                        }
                    }
                }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)).getExtras();
                Iterator<String> iter = extrasBundle.keySet().iterator();
                while (iter.hasNext()) {
                    String key = iter.next();
                    System.out.println("Bluetooth Receiver  Key :" + key + ", value: " + extrasBundle.get(key));
                }
                if (extrasBundle.getInt(AudioManager.EXTRA_SCO_AUDIO_STATE) != 2) {
                    localAudioManager.setMode(AudioManager.MODE_IN_CALL);
                    localAudioManager.startBluetoothSco();
                }
            }
        }
    }

    public void doUnBind(View view) {
        stopBluetoothSco();
    }

    private void stopBluetoothSco() {
        System.out.println("stopBluetoothSco called");
        AudioManager localAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        localAudioManager.setMode(AudioManager.MODE_NORMAL);
        localAudioManager.setSpeakerphoneOn(true);
        localAudioManager.setBluetoothScoOn(false);
        localAudioManager.stopBluetoothSco();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="60dp"
        android:onClick="TextToSpeechButton"
        android:text="@string/text_to_speech" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="150dp"
        android:ems="10"
        android:gravity="start|top"
        android:hint="@string/your_text_here"
        android:text="Hello"
        android:inputType="textMultiLine"
        android:autofillHints="" />

    <EditText
        android:id="@+id/editTextRecord"
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="300dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/btnRecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="400dp"
        android:text="Click to Record" />

    <Button
        android:id="@+id/btnBind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="500dp"
        android:onClick="doBind"
        android:text="Bind" />
    <Button
        android:id="@+id/btnUnBind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="600dp"
        android:onClick="doUnBind"
        android:text="Unbind" />
</RelativeLayout>