如何通过 ADB 检查是否正在使用外部麦克风
How to check if external microphone is being used via ADB
我正在尝试通过 ADB 检查是否使用了外部有线耳机麦克风。当我插入有线耳机时会自动检测到此麦克风,但出于外部脚本目的,检测此操作会非常有帮助。
我找不到麦克风的 intent,但在这里查找了耳机的 intent:
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
所以我不确定从这里到哪里去。我什至无法检测到耳机是否已插入,更不用说是否正在使用外接麦克风了。任何帮助将不胜感激。谢谢!
我发现此方法适用于我的设备:
运行命令
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}]
我正在尝试通过 ADB 检查是否使用了外部有线耳机麦克风。当我插入有线耳机时会自动检测到此麦克风,但出于外部脚本目的,检测此操作会非常有帮助。
我找不到麦克风的 intent,但在这里查找了耳机的 intent: 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
所以我不确定从这里到哪里去。我什至无法检测到耳机是否已插入,更不用说是否正在使用外接麦克风了。任何帮助将不胜感激。谢谢!
我发现此方法适用于我的设备:
运行命令
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}]