What is the android api for getting the list of connected audio devices?
Asked Answered
Z

1

21

I used the below added code to get the connected audio devices for android device. Used AudioManager api method getDevices() and got the result with connected devices like earphones, speaker, headphones. But this getDevices() method is available since android api level 23 (Marshmallow) only.

But my application should support from api level 21 (Lollipop). Please can anybody let me know the alternative api to get the available audio device for the android device.

Code used with api level 23.

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    AudioDeviceInfo[] adi = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
}

Please help me by providing a way to get the connected audio devices for the devices running with Android OS version below Marshmallow(api level 23).

Zita answered 22/1, 2016 at 11:10 Comment(2)
did you find answer to this?Languorous
I assume those methods exist even in older APIs. Have you tried using reflection to invoke those non-public methods?Pearly
B
3

It seems there was no such API before API level 23, but one could use boolean methods to check, if some audio device was on:

  • isWiredHeadsetOn() - added in API level 5, deprecated in API level 15
  • isSpeakerphoneOn() - added in API level 1
  • isBluetoothScoOn() - added in API level 1
  • isBluetoothA2dpOn() - added in API level 3, deprecated in API level 26

If you always get false when add MODIFY_AUDIO_SETTINGS permission to AndroidManifest.xml

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 

After the check you may set the devices on or off by passing true or false to methods:

  • setWiredHeadsetOn() - added in API level 5, deprecated in API level 5
  • setSpeakerphoneOn() - added in API level 1
  • setBluetoothScoOn() - added in API level 1
  • setBluetoothA2dpOn() - added in API level 3, deprecated in API level 5
Bowes answered 7/9, 2018 at 10:51 Comment(1)
isWiredHeadSetOn() is deprecated in API 15: developer.android.com/reference/android/media/…Misdoubt

© 2022 - 2024 — McMap. All rights reserved.