user3268403
user3268403

Reputation: 213

Android startBluetoothSco() not working

I am on a LG phone (L38C) running android 2.3.6. I am having trouble obtaining audio input from bluetooth headset programatically. This seems to be a common issue but my research did not lead to any solutions. I have tried the solution proposed here but the CO_AUDIO_STATE_CONNECTED event is not triggered.

Using the Android RecognizerIntent with a bluetooth headset

Any thoughts?

PS: My headset (JABRA BT2046) is paired and connected to phone audio when I call the start() function.

Upvotes: 4

Views: 4949

Answers (1)

user3193413
user3193413

Reputation: 634

In my case I was looking for a way to connect the bluetooth device when user click a button if the user wants to use the bluetooth headset during a Voip call.

My solution tested on Samsung S7 SM-G930F with android version 7.0 is:

public class ExampleActivity extends Activity{

private AudioManager mAudioManager;
private BroadcastReceiver mBroadcastReceiver;

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) 
{
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.example_activity);

    initActivity();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    registerReceiver(mBroadcastReceiver, intentFilter);
}

@Override
public void onDestroy() 
{
    super.onDestroy();
    unregisterReceiver(mBroadcastReceiver);
}

private void initActivity()
{
    initBroadcasrReceiver();
    initAudioManager();
}

private void initBroadcasrReceiver()
{
    mBroadcastReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            if (intent.getAction().equals(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) 
            {
                int scoAudioState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
                if(scoAudioState == AudioManager.SCO_AUDIO_STATE_CONNECTED)
                {
                    // This method should be called only after SCO is connected!
                    mAudioManager.setBluetoothScoOn(true);
                }
            }
        }
    };
}

private void initAudioManager()
{
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    resetSco();
    mAudioManager.startBluetoothSco();
}

private void resetSco()
{
    mAudioManager.setMode(AudioManager.MODE_NORMAL);
    mAudioManager.stopBluetoothSco();
    mAudioManager.setBluetoothScoOn(false);
    mAudioManager.setSpeakerphoneOn(false);
    mAudioManager.setWiredHeadsetOn(false);
}}

Once the SCO is connected you'll be able to hear and speak from your bluetooth device. Hope it helps.

Upvotes: 4

Related Questions