The method AudioManager.isWiredHeadsetOn() is deprecated from api level 14, how do we now detect if a wired headset is connected?
Asked
Active
Viewed 1.4k times
6 Answers
27
This is my solution:
private boolean isHeadsetOn(Context context) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am == null)
return false;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return am.isWiredHeadsetOn() || am.isBluetoothScoOn() || am.isBluetoothA2dpOn();
} else {
AudioDeviceInfo[] devices = am.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo device : devices) {
if (device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET
|| device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADPHONES
|| device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP
|| device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
return true;
}
}
}
return false;
}
-
1The deprecation of `isWiredHeadsetOn` and `isBluetoothA2dpOn` is from API 14. What should have been done between 14 and before M (23) ? I think even the docs are weird here, so I wrote this: issuetracker.google.com/issues/111788828 . Anyway, is it expected to work fine with this workaround for accepting calls : https://stackoverflow.com/a/29651130/878126 (replacement for `isWiredHeadsetOn`) ? – android developer Jul 25 '18 at 11:46
-
Maybe you have to expand your solution, The document said that TYPE_WIRED_HEADSET: A device type describing a headset, which is the combination of a headphones and microphone. https://developer.android.com/reference/android/media/AudioDeviceInfo.html#TYPE_WIRED_HEADSET – Leon Chang Nov 30 '18 at 08:59
22
The documentation's deprecation message states:
Use only to check is a headset is connected or not.
So I guess it is okay to keep using it to check whether or not a wired headset is connected, but not to check whether or not audio is being routed to it or played over it.

Raghav Sood
- 81,899
- 22
- 187
- 195
-
2I guess the answer is as simple as that. I find it confusing to deprecate a method, if its not really deprecated, it just changed purpose a bit. Anyway, thanks. :) – Bjarke Freund-Hansen Jan 23 '13 at 07:20
11
Try this solution. It's working in my case. May be this helps you!
IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent iStatus = context.registerReceiver(null, iFilter);
boolean isConnected = iStatus.getIntExtra("state", 0) == 1;
-
1But this won't tell you about the headset state when you plug in the headset BEFORE launching your app. – goodfellow Feb 26 '19 at 08:11
1
It works for me correctly:
if(context.registerReceiver(null, new IntentFilter(Intent.ACTION_HEADSET_PLUG)).getIntExtra("state", 0)==1){
//if(audioManager.isWiredHeadsetOn()){
System.out.println("Headset is wiredOn");
}
else{
System.out.println("Headset is not wiredOn");
}

Mehdi Raeisi
- 11
- 2
0
IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent iStatus = context.registerReceiver(null, iFilter);
boolean isConnected = iStatus.getIntExtra("state", 0) == 1;
I do know this code can be used on which andorid platform version. It does not work for me on Android 8. Result iStatus is null.

sanren1024
- 37
- 5
-2
we have to be using broadcast Receivers for finding the status of bluetooth connection.
Here is a good example

Community
- 1
- 1

Ranjithkumar
- 697
- 6
- 16