如何通过ADB检查是否正在使用外部麦克风

时间:2016-04-04 23:00:40

标签: android android-intent adb android-broadcastreceiver

我试图通过ADB检查是否使用了外置有线耳机麦克风。当我插入有线耳机时会自动检测到这个麦克风,但出于外部脚本的目的,检测此操作会非常有用。

我无法找到麦克风的意图,但在此处查找了耳机的意图: http://developer.android.com/reference/android/content/Intent.html

我试过这个广播意图来单独检测耳机:

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

无论是否实际插入有线耳机,都能获得此响应:

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

所以我不知道从哪里开始。我甚至无法检测耳机是否已插入,更不用说如果使用外接麦克风了。任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

我发现此方法适用于我的设备:

运行命令

adb shell dumpsys activity broadcasts | grep microphone

应该产生类似的东西:

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}]

最后一行位于转储的粘性广播部分内,广播在更改之前保持不变。

因此,如果我们使用tail取最后一行并对其进行剖析,则它包含耳机的当前状态:

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

输出:

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

state整数表示是否有东西插入耳机插孔,无论它是否包含麦克风。 0表示未插入,1表示已插入。

microphone整数是指最后插入的耳机是否还包含麦克风。 0表示否,1表示是。

方案

如果当前插入了一副正常的耳机,输出将为:

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

如果当前插入了带麦克风的耳机,输出将为:

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

如果没有插入任何内容,则输出为:

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

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

相关问题