Paul
Paul

Reputation: 303

How to listen for changes in audio routing during call in Android

I can't seem to figure out how to detect when a user changes how audio is routed while in a call. Specifically, listening for when a user toggles between the audio routed through a bluetooth headset and the device earpiece. Can someone direct me to a good example? Thanks.

Upvotes: 4

Views: 1298

Answers (1)

Paul
Paul

Reputation: 303

In onCreate or onStartCommand

MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
registerReceiver(myReceiver, filter);

In onDestroy

unregisterReceiver(myReceiver);

BroadcastReceiver class

private class MyBroadcastReceiver extends BroadcastReceiver {       
    @Override 
    public void onReceive(Context context, Intent intent) {         
        if(intent.getAction().equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)){
            int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,-1);
            switch(state){
            case BluetoothHeadset.STATE_AUDIO_CONNECTED:
                Log.d(TAG,"Bluetooth connected");
                break;
            case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
                Log.d(TAG,"Bluetooth disconnected");
                break;
            }
        }   
    }   
}

Upvotes: 2

Related Questions