Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether headset has microphone

I need to detect whether the plugged in wired headset has microphone or not.

I can check if a headset is plugged in using isWiredHeadSetOn(), but for microphone does not seem to be such a method in AudioManager class.

I have found some suggestions using ACTION_HEADSET_PLUG, but I am interested to find out this information even if the headset has been plugged in before opening my application, this event won't be fired during the lifetime of my app.

Any ideas regarding this issue? Thank you in advance.

like image 754
niculare Avatar asked Feb 05 '13 13:02

niculare


People also ask

Why is my headset mic not detected?

Try the following solutions: If your headset has a Mute button, make sure it isn't active. Make sure that your microphone or headset is connected correctly to your computer. Make sure that your microphone or headset is the system default recording device.

Where is the mic on headphones?

Wireless headphones and earbuds may have an inline microphone embedded in the casing or connector band.


1 Answers

UPDATE: Go ahead and register ACTION_HEADSET_PLUG in your activity's onResume(). If user has ever plugged in/out her headset after boot-up, platform will deliver the latest state to your activity when it resumes.

Following test code worked:

package com.example.headsetplugtest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class HeadSetPlugIntentActivity extends Activity {

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
                Log.d("HeadSetPlugInTest", "state: " + intent.getIntExtra("state", -1));
                Log.d("HeadSetPlugInTest", "microphone: " + intent.getIntExtra("microphone", -1));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        getApplicationContext().registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();

        getApplicationContext().unregisterReceiver(mReceiver);
    }
}
like image 116
ozbek Avatar answered Oct 07 '22 16:10

ozbek