1
votes

How can I automatically connect to the last connected audio source in Android?

We are building a video doorbell app, now instead of giving the user option to select audio output our UI/UX team asked us to hardcode selecting to the last selected audio source.

Ex: If a bluetooth is connected I have to make it as the audio source, if ear-phones connected i have to make that the last source if both bluetooth and ear-phones connected I need to find the last connected source and make that the audio output, If no sources connected should make speaker as default instead of earpiece.

Thanks.

1

1 Answers

0
votes

Try this:

public int getAudioOutputDevice() {
    AudioManager audiMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) {
        //save to preferences
        return 1;
    }
    else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) {
        //save to preferences
        return 2;
    }
    else {
        return -1;
    }
}

Note: "getRouting" was deprecated in API level 4. Use isSpeakerphoneOn(), isBluetoothScoOn(), isBluetoothA2dpOn() and isWiredHeadsetOn() methods instead.