Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting whether a headset is plugged into an Android device or not.

How can I determine whether a headset is plugged into an Android device or not?

like image 688
coderslay Avatar asked Jun 06 '11 07:06

coderslay


People also ask

How do you check if headphones are plugged in android programmatically?

AudioManager audioManager = (AudioManager)getSystemService(Context. AUDIO_SERVICE); audioManager. isWiredHeadsetOn(); (Don't worry about the deprecation, it's still usable for ONLY checking if the headset are plugged in.)

How can my phone detect my headphones?

DC resistance measurement between the microphone pin and the ground pin of the socket can be used to detect the kind of device plugged in - it will be 0 Ohm for a headphone, infinitely high for no device connected, and about 2 kOhm thereabouts for a headset with microphone.

How do I know if my Bluetooth headset is connected Android?

Using Bluetooth is safe by touching it and holding it. Tap a smartphone with an attached Bluetooth device on the list of paired devices. Once the phone and Bluetooth device are connected, the connected vehicle is labeled connected.


1 Answers

You can use the broadcast receiver.

So, You might write this code in "AndroidManifest.xml"

<receiver android:name="com.juno.brheadset.HeadsetStateReceiver">     <intent-filter>         <action android:name="android.intent.action.HEADSET_PLUG"/>     </intent-filter> </receiver>--> 

But, This doesn't work. When OS send this "HEADSET_PLUG" intent, OS set the flag "Intent.FLAG_RECEIVER_REGISTERED_ONLY" So, You should write the code like below in Activity or Service class instead of "AndroidManifest" things.

public class BRHeadsetActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);     HeadsetStateReceiver receiver = new HeadsetStateReceiver();     registerReceiver( receiver, receiverFilter );   } 

I hope this article help you. Bye!

This is the part of "HeadsetObserver.java", Android SDK Source.

private final void sendIntent(int headset, int headsetState, int prevHeadsetState, String headsetName) {     if ((headsetState & headset) != (prevHeadsetState & headset)) {         //  Pack up the values and broadcast them to everyone         Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);          **intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);**          int state = 0;         int microphone = 0;          if ((headset & HEADSETS_WITH_MIC) != 0) {             microphone = 1;         }         if ((headsetState & headset) != 0) {             state = 1;         }         intent.putExtra("state", state);         intent.putExtra("name", headsetName);         intent.putExtra("microphone", microphone);          if (LOG) Slog.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);         // TODO: Should we require a permission?         ActivityManagerNative.broadcastStickyIntent(intent, null);     } } 
like image 95
cmcromance Avatar answered Sep 30 '22 00:09

cmcromance