Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if headset is connected on different API levels

I have an application that targets API Level 19+. I need to check if headset is connected (either of wired or bluetooth).

I see that AudioManager.isWiredHeadsetOn() is deprecated starting from API Level 16 and the documentation offers to use AudioManager.getDevices() which was introduced only starting from API Level 23.

Question: What is the proper way to check if headset is connected for API levels 16 - 22?

Note: I know that I can still use the deprecated method, but I don't want to. If they deprecated it they should have introduced a replacement API (which I though can't find).

like image 787
Sasha Shpota Avatar asked Sep 01 '18 11:09

Sasha Shpota


2 Answers

You need to listen for ACTION_HEADSET_PLUG which fires on headset events and also apparently fires periodically even when nothing changes.

From my existing code I have this in onCreate of my main service:

    // listen for headphone events so we can auto switch the audio output when headphones are plugged in
    myHeadphoneMonitor = new HeadphoneMonitor();
    android.content.IntentFilter headphone_filter = new 
    android.content.IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myHeadphoneMonitor, headphone_filter);

And my monitor looks like this:

  package com.myname.foo;

  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.util.Log;
  import android.os.Message;


  public class HeadphoneMonitor extends BroadcastReceiver
  {
    public boolean headphonesActive=false;

    @Override
    public void onReceive(final Context context, final Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG))
        {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
                    case 0:
                        Log.d("HeadphoneMonitor", "Headset is unplugged");
                        headphonesActive=false;
                        break;
                    case 1:
                        Log.d("HeadphoneMonitor", "Headset is plugged in");
                        headphonesActive=true;
                        break;
                    default:
                        Log.d("HeadphoneMonitor", "I have no idea what the headset state is");
                        break;
            }

            // push this event onto the queue to be processed by the Handler
            Message msg = MyApp.uiHandler.obtainMessage(MyApp.HEADPHONE_EVENT);
            MyApp.uiHandler.sendMessage(msg);
        }
    }
  }

Nothing is needed in the manifest to listen for this broadcast event.

like image 71
spartygw Avatar answered Oct 21 '22 08:10

spartygw


Since AndroidStudio warns that AudioManager.getDevices requires VersionCode M the best way is making version check like this:

    // ... some code
    boolean isWiredHeadsetOn=false;
    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    Log.d(TAG, "onCreate:::: retrieved AudioManager instance");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Log.d(TAG, "onCreate:::: BuildVersion>=M");
        AudioDeviceInfo[] mAudioDeviceInfos = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
        Log.d(TAG, "onCreate:::: got AudioDeviceInfo[]");
        for (int i = 0; i < mAudioDeviceInfos.length; i++) {
            if (mAudioDeviceInfos[i].getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
                Log.d(TAG, "onCreate:::: \n\nfind wiredHeadset!!!\n\n");
                isWiredHeadsetOn=true;
            } else {
                Log.d(TAG, "onCreate:::: find device type: " + mAudioDeviceInfos[i].getType() + ", id: " + mAudioDeviceInfos[i].getProductName());
            }
        }
    } else {
        Log.d(TAG, "onCreate:::: BuildVersion<M");
        isWiredHeadsetOn=mAudioManager.isWiredHeadsetOn();
    }
    // ... continue code

Hope I could help. Best regards, Cs

like image 20
Csongi77 Avatar answered Oct 21 '22 09:10

Csongi77