11

How can I check if headphones are currently plugged in. I don't want a broadcastreceiver which informs me when they have been connected to the device. I need something like:

if(/*headphone is connected*/)
 ...
user283494
  • 1,897
  • 4
  • 19
  • 26

3 Answers3

14

It looks like you'll be interested in the isWiredHeadsetOn() method and isBluetoothA2dpOn() method of the AudioManager class.

However, the isWiredHeadsetOn() method is only available in Android 2.0 or later. (The isBluetoothA2dpOn() method has been available since Android 1.5.)

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • 1
    Note that this is now deprecated and you need to check the types of `audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)` instead – yonix Mar 13 '18 at 09:47
2

Use this code snippet

AudioManager am1 = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Log.i("am1.isWiredHeadsetOn()", am1.isWiredHeadsetOn()+"");
Log.i("am1.isMusicActive()", am1.isMusicActive()+"");
Log.i("am1.isSpeakerphoneOn()", am1.isSpeakerphoneOn()+"");
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Mohsin Bagwan
  • 333
  • 4
  • 9
0

This seems to do the job at least on 1.6; not sure whether it's supported in later versions (a is an instance of AudioManager)

boolean headphones = (a.getRouting(a.getMode()) & AudioManager.ROUTE_HEADSET) == AudioManager.ROUTE_HEADSET;
andrewsi
  • 10,807
  • 132
  • 35
  • 51