Search code examples
androidandroid-intentadbandroid-broadcastreceiver

How to check if external microphone is being used via ADB


I'm trying to check through ADB whether or not the external wired headset mic is used or not. This mic is automatically detected when I plug in the wired headset, but for external scripting purposes, it would be very helpful to detect this action.

I couldn't find an intent for the microphone, but looked up the headset intent here: http://developer.android.com/reference/android/content/Intent.html

I tried this broadcast intent for detecting the headset alone:

adb shell am broadcast -a android.intent.action.HEADSET_PLUG

which gets this response whether or not a wired headset is actually plugged in:

Broadcasting: Intent { act=android.intent.action.HEADSET_PLUG }
Broadcast completed: result=0

So I'm not sure where to go from here. I can't even detect if the headset is plugged in, much less if the external microphone is being used. Any help would be greatly appreciated. Thanks!


Solution

  • I found this method works on my device:

    Run the command

    adb shell dumpsys activity broadcasts | grep microphone

    which should produce something like:

    extras: Bundle[{name=h2w, state=1, microphone=1}]
    extras: Bundle[{name=h2w, state=0, microphone=1}]
    extras: Bundle[{name=h2w, state=1, microphone=1}]
    extras: Bundle[{name=h2w, state=0, microphone=1}]
    extras: Bundle[{name=h2w, state=1, microphone=1}]
    extras: Bundle[{name=h2w, state=0, microphone=1}]
    extras: Bundle[{name=h2w, state=1, microphone=1}]
    extras: Bundle[{name=h2w, state=0, microphone=1}]
    extras: Bundle[{name=h2w, state=1, microphone=1}]
    extras: Bundle[{name=h2w, state=0, microphone=1}]
    extras: Bundle[{name=h2w, state=1, microphone=1}]
    extras: Bundle[{name=h2w, state=0, microphone=1}]
    extras: Bundle[{name=h2w, state=1, microphone=1}]
      Bundle[{name=h2w, state=1, microphone=1}]
    

    The last line is inside the sticky broadcasts section of the dump, the broadcasts that remain the same until changed.

    So if we take the last line using tail and dissect it, it contains the current state of the headset:

    adb shell dumpsys activity broadcasts | grep microphone | tail -n 1

    output:

    Bundle[{name=h2w, state=1, microphone=1}]

    The state integer refers to whether something is plugged into the headphone jack, regardless of it contains a microphone. 0 for unplugged and 1 for plugged in.

    The microphone integer refers to if the headset that was last plugged in also included a microphone. 0 for no, 1 for yes.

    Scenarios

    If a normal pair of headphones is currently plugged in, the output will be:

    Bundle[{name=h2w, state=1, microphone=0}]


    If a headset with a microphone is currently plugged in, the output will be:

    Bundle[{name=h2w, state=1, microphone=1}]


    If nothing is plugged in, the output is either:

    Bundle[{name=h2w, state=0, microphone=0}]

    or

    Bundle[{name=h2w, state=0, microphone=1}]