0

I'm making an app for a specialist purpose that requires a headset microphone to be connected. I don't want users to mistakenly use their phone's built-in microphone and have a substandard experience, so ideally the app should enforce a 3.5mm headset to be used.

Unfortunately, I haven't been able to find anything about this online, only posts about checking if a microphone is available. I want to instead check for the presence of a headset plugged into the 3.5mm jack.

I can see at least one app where this functionality is present. Lesser AudioSwitch will display whether my input is from the Headset or Internal Microphone when I connect/disconnect my headset.

How would I achieve this in my own app?

OscarVanL
  • 701
  • 1
  • 6
  • 21

1 Answers1

1

You can use the ACTION_HEADSET_PLUG broadcast action to detect when a wired headset is plugged.

Note that the broadcast receiver must be registered manually, instead of in the manifest:

 yourBroadcastHandler = new BroadcastHandler();
 registerReceiver(yourBroadcastHandler, new IntentFilter(Intent.ACTION_HEADSET_PLUG));

And implement a handler for your broadcast:

public final class BroadcastHandler extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        // Do what you need
    }
}
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • Thanks, that looks like it solves part of the problem. The other issue is, what if the app is launched with the headset already plugged in? In this case, I assume the broadcast wouldn't be triggered. – OscarVanL Oct 18 '20 at 12:18
  • It seems this is possible, the app Lesser AudioSwitch (https://play.google.com/store/apps/details?id=com.nordskog.LesserAudioSwitch) detects when I am using my headset or Internal microphone as microphone input. This is exactly what I need :) – OscarVanL Oct 18 '20 at 12:23
  • You should receive the broadcast every time it gets registered, as the ACTION_HEADSET_PLUG intent is being broadcast by the system as sticky, meaning that it will be sent to any new BroadcastReceiver being registered. – PerracoLabs Oct 18 '20 at 12:26
  • Oh interesting, I'll play around with that then. Thank you for the suggestion – OscarVanL Oct 18 '20 at 12:29
  • This worked perfectly. Thank you! For anyone else looking at this thread, this answer has been taken from this thread (which has an implementation of BroadcastHandler): https://stackoverflow.com/questions/7481083/androidbroadcastreceiveraction-headset-plug-not-firing Do you know if this also works for USB-C to Headphone jack converters for phones without headphone jacks? – OscarVanL Oct 18 '20 at 14:12
  • @OscarVant, Let me correct you. The answer hasn't been taken from any other one, This is how is done when registering for a broadcast. The fact the other answer names the receiver a BroadcastHandler is coincidental and expected in similar questions, as we developers give sometimes such type of naming as examples, which in this case means a class for "handling your broadcast". Of course, you could instead also implement a nested anonymous receiver directly when registering for it, but would make the code messy. – PerracoLabs Oct 18 '20 at 14:40
  • Oh, sorry. I didn't mean that in an accusatory sense. I just thought the link would be helpful to those that wanted more details for actual implementation :) – OscarVanL Oct 18 '20 at 17:32