[英]Handle bluetooth headset clicks (ACTION_VOICE_COMMAND and ACTION_WEB_SEARCH) on Android
I'm developing an Android
application and I want it to interact with headset
button clicks. 我正在开发一个
Android
应用程序,我希望它与headset
按钮点击进行交互。 I'm testing it in a Nexus 5 with Android KitKat 4.4 . 我正在使用Android KitKat 4.4在Nexus 5中测试它。
I tried first with a simple headset (not wireless). 我首先尝试使用简单的耳机(不是无线耳机)。 The button event received was
KEYCODE_HEADSETHOOK
(79). 收到的按钮事件是
KEYCODE_HEADSETHOOK
(79)。 I created a MEDIA_BUTTON
receiver
to handle its clicks: 我创建了一个
MEDIA_BUTTON
receiver
来处理它的点击:
<receiver android:name="com.example.mytest.SearchActivity$MediaButtonIntentReceiver">
<intent-filter>
<intent-filter android:priority="1000000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</intent-filter>
</receiver>
This is the activity holding the receiver: 这是持有接收者的活动:
public class SearchActivity extends Activity {
private AudioManager mAudioManager;
private ComponentName mAudioReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
mAudioReceiver = new ComponentName(getPackageName(),
MediaButtonIntentReceiver.class.getName());
}
@Override
protected void onResume() {
super.onResume();
mAudioManager.registerMediaButtonEventReceiver(mAudioReceiver);
}
@Override
protected void onPause() {
super.onPause();
mAudioManager.unregisterMediaButtonEventReceiver(mAudioReceiver);
}
public static class MediaButtonIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("SA", "ON RECEIVE");
...
abortBroadcast();
}
}
}
This code works with my wired headset only if the user performs a short click . 仅当用户执行短暂点击时,此代码才能与我的有线耳机配合使用 。 Performing a long click opens
Google Voice Search
. 执行长按可打开
Google Voice Search
。 I'd like to capture long clicks too, but I don't mind if it's not possible. 我也希望获得长时间点击,但我不介意它是否不可能。
After that I tested it with a bluetooth
headset. 之后我用
bluetooth
耳机测试了它。 Specifically, I'm using Moveteck Bluetooth Headset BH119A (you can see an image at the bottom of this post). 具体来说,我使用的是Moveteck蓝牙耳机BH119A(你可以看到这篇文章底部的图片)。 This headset only has one button, and if I press it the following "
activity
" is opened: 此耳机只有一个按钮,如果我按下它,则会打开以下“
activity
”:
I'd like to capture this click event too if my Activity
is opened. 如果我的
Activity
被打开,我也想捕获这个点击事件。 How can I do it? 我该怎么做? I tried adding the following filters to my receiver, but it's not working either:
我尝试将以下过滤器添加到我的接收器中,但它也不起作用:
<action android:name="android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT" />
<action android:name="android.intent.action.VOICE_COMMAND" />
<action android:name="android.intent.action.CALL_BUTTON" />
I also tried overriding onKeyDown
in my Activity, but it's not triggered. 我也尝试在我的Activity中覆盖
onKeyDown
,但它没有被触发。
Anybody knows how can I intercept those events? 有人知道如何拦截这些事件?
This is my bluetooth headset: 这是我的蓝牙耳机:
** EDIT ** **编辑**
Following Toaster's advice, I checked the whole log looking for the events my headset triggers. 按照Toaster的建议,我查看了整个日志,查找我的耳机触发的事件。
Wired headset long click 有线耳机长按一下
This is the log when I long click in my wired headset (it opens Google Voice Search
): 这是我长时间点击有线耳机时的日志(它会打开
Google Voice Search
):
12-10 09:24:36.644: I/MediaFocusControl(740): voice-based interactions: about to use ACTION_WEB_SEARCH
12-10 09:24:36.644: I/ActivityManager(740): START u0 {act=android.speech.action.WEB_SEARCH flg=0x10800000 cmp=com.google.android.googlequicksearchbox/.SearchActivity} from pid 740
12-10 09:24:36.754: I/ActivityManager(740): START u0 {act=android.speech.action.WEB_SEARCH flg=0x10000000 cmp=com.google.android.googlequicksearchbox/com.google.android.launcher.GEL} from pid 10153
12-10 09:24:36.764: I/InputDispatcher(740): Dropping event because there is no focused window or focused application.
12-10 09:24:36.764: I/InputDispatcher(740): Dropping event because there is no focused window or focused application.
12-10 09:24:36.774: I/GEL(1025): handleIntent(Intent { act=android.speech.action.WEB_SEARCH flg=0x10400000 cmp=com.google.android.googlequicksearchbox/com.google.android.launcher.GEL })
12-10 09:24:36.774: V/SearchControllerCache(10153): creating SearchController
12-10 09:24:36.804: I/AudioRouter(10153): ROUTE_NONE->ROUTE_NO_BLUETOOTH
12-10 09:24:36.804: I/MediaFocusControl(740): AudioFocus requestAudioFocus() from android.media.AudioManager@4267ad58com.google.android.voicesearch.audio.AudioRouterImpl$1@42695f60
12-10 09:24:36.804: I/Velvet.SdchManager(10153): Sdch cache load complete.
12-10 09:24:36.814: W/IInputConnectionWrapper(18407): showStatusIcon on inactive InputConnection
12-10 09:24:36.814: I/Icing.InternalIcingCorporaProvider(10153): Updating corpora: A: NONE, C: DELTA
12-10 09:24:36.854: I/VS.G3EngineManager(10153): create_rm: m=GRAMMAR,l=en-US
12-10 09:24:36.854: W/Search.ConcurrentUtils(10153): Executor queue length is now 9. Perhaps some tasks are too long, or the pool is too small. [GrecoExecutor-1]
12-10 09:24:36.854: I/VS.G3EngineManager(10153): Brought up new g3 instance :/system/usr/srec/en-US/grammar.config for: en-USin: 9 ms
12-10 09:24:36.864: D/audio_hw_primary(189): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=4
12-10 09:24:36.864: D/audio_hw_primary(189): select_devices: out_snd_device(4: headphones) in_snd_device(0: )
12-10 09:24:36.874: D/audio_hw_primary(189): select_devices: out_snd_device(0: ) in_snd_device(18: headset-mic)
12-10 09:24:36.874: D/(189): Failed to fetch the lookup information of the device 00000008
12-10 09:24:36.874: E/ACDB-LOADER(189): Error: ACDB AudProc vol returned = -19
12-10 09:24:38.864: I/LATENCY(10153): 0-4,45-2064,
12-10 09:24:38.874: I/AudioRouter(10153): ROUTE_NO_BLUETOOTH->ROUTE_NONE
12-10 09:24:38.874: I/MediaFocusControl(740): AudioFocus abandonAudioFocus() from android.media.AudioManager@4267ad58com.google.android.voicesearch.audio.AudioRouterImpl$1@42695f60
12-10 09:24:38.874: I/MicrophoneInputStream(10153): mic_close
It seems it triggers an ACTION_WEB_SEARCH
event, so I tried to add it to the filter. 它似乎触发了
ACTION_WEB_SEARCH
事件,因此我尝试将其添加到过滤器中。 I tried it two ways: 我尝试了两种方法:
Declaring the filter in the manifest: 在清单中声明过滤器:
<action android:name="android.intent.action.WEB_SEARCH" />
Declaring the filter programmatically: 以编程方式声明过滤器:
protected void onResume() { IntentFilter f = new IntentFilter(Intent.ACTION_WEB_SEARCH); registerReceiver(myReceiver, f); } private BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("AA", "ON RECEIVE"); } };
None of this options work. 这些选项都不起作用。 As I said, this scenario isn't of much importance, I can deal with it.
正如我所说,这种情况并不重要,我可以处理它。
Wireless headset simple click 无线耳机简单点击
The Wireless headset simple click is the one opening the Voice Dialer and it's the event I really need to capture . 无线耳机简单点击是打开语音拨号器的那个 , 这是我真正需要捕捉的事件 。 This is the log output:
这是日志输出:
12-10 10:41:22.014: E/bt-rfcomm(21800): PORT_DataInd, p_port:0x7507a7e8, p_data_co_callback is null
12-10 10:41:22.014: D/HeadsetStateMachine(21800): processVrEvent: state=1 mVoiceRecognitionStarted: false mWaitingforVoiceRecognition: false isInCall: false
12-10 10:41:22.014: I/ActivityManager(740): START u0 {act=android.intent.action.VOICE_COMMAND flg=0x10000000 cmp=com.google.android.googlequicksearchbox/com.google.android.voicesearch.handsfree.HandsFreeIntentActivity} from pid 21800
12-10 10:41:22.154: V/Avrcp(21800): New genId = 440, clearing = 1
12-10 10:41:22.154: D/HandsFreeIntentActivity(10153): #onStart(Intent { act=android.intent.action.VOICE_COMMAND flg=0x10800000 cmp=com.google.android.googlequicksearchbox/com.google.android.voicesearch.handsfree.HandsFreeIntentActivity })
12-10 10:41:22.154: D/HandsFreeIntentActivity(10153): Starting activity: Intent { act=android.intent.action.VOICE_COMMAND flg=0x10000000 cmp=com.google.android.googlequicksearchbox/com.google.android.voicesearch.handsfree.HandsFreeActivity }
12-10 10:41:22.154: I/ActivityManager(740): START u0 {act=android.intent.action.VOICE_COMMAND flg=0x10000000 cmp=com.google.android.googlequicksearchbox/com.google.android.voicesearch.handsfree.HandsFreeActivity} from pid 10153
12-10 10:41:22.204: D/OpenGLRenderer(10153): Enabling debug mode 0
12-10 10:41:22.214: W/IInputConnectionWrapper(18895): showStatusIcon on inactive InputConnection
12-10 10:41:22.244: I/ActivityManager(740): Displayed com.google.android.googlequicksearchbox/com.google.android.voicesearch.handsfree.HandsFreeActivity: +80ms (total +89ms)
12-10 10:41:22.374: I/AudioRouter(10153): ROUTE_NONE->ROUTE_BLUETOOTH_WANTED
12-10 10:41:22.384: I/MediaFocusControl(740): AudioFocus requestAudioFocus() from android.media.AudioManager@4267ad58com.google.android.voicesearch.audio.AudioRouterImpl$1@42695f60
12-10 10:41:22.384: V/Avrcp(21800): New genId = 441, clearing = 1
12-10 10:41:22.384: D/BluetoothManagerService(740): Message: 30
12-10 10:41:22.384: D/BluetoothHeadset(10153): Proxy object connected
12-10 10:41:22.384: I/BluetoothController(10153): BT device connected
12-10 10:41:22.394: I/AudioRouter(10153): BT required, starting SCO
12-10 10:41:22.394: I/BluetoothController(10153): Starting VR
12-10 10:41:22.394: D/BluetoothHeadset(10153): startVoiceRecognition()
12-10 10:41:22.394: D/HeadsetStateMachine(21800): Voice recognition started successfully
12-10 10:41:22.394: D/HeadsetStateMachine(21800): Initiating audio connection for Voice Recognition
12-10 10:41:22.394: W/bt-btm(21800): BTM Remote does not support 3-EDR eSCO
12-10 10:41:22.434: I/TextToSpeech(10153): Sucessfully bound to com.google.android.tts
12-10 10:41:22.454: I/TextToSpeech(10153): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
12-10 10:41:22.454: I/TextToSpeech(10153): Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
12-10 10:41:22.484: D/dalvikvm(21966): GC_CONCURRENT freed 346K, 3% free 16647K/17064K, paused 2ms+3ms, total 13ms
12-10 10:41:22.764: D/audio_hw_primary(189): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=32
12-10 10:41:22.774: D/audio_hw_primary(189): select_devices: out_snd_device(11: bt-sco-headset) in_snd_device(0: )
12-10 10:41:24.874: I/EventLogService(1148): Aggregate from 1386666683008 (log), 1386666683008 (data)
12-10 10:41:24.994: I/ServiceDumpSys(1148): dumping service [account]
12-10 10:41:25.994: D/dalvikvm(10153): GC_CONCURRENT freed 1582K, 15% free 23868K/27920K, paused 5ms+7ms, total 60ms
12-10 10:41:26.014: I/VS.G3EngineManager(10153): create_rm: m=GRAMMAR,l=en-US
12-10 10:41:26.024: I/VS.G3EngineManager(10153): Brought up new g3 instance :/system/usr/srec/en-US/grammar.config for: en-USin: 4 ms
12-10 10:41:26.024: D/audio_hw_primary(189): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=32
12-10 10:41:26.034: D/audio_hw_primary(189): select_devices: out_snd_device(0: ) in_snd_device(25: bt-sco-mic)
12-10 10:41:26.034: D/(189): Failed to fetch the lookup information of the device 00000015
12-10 10:41:26.034: E/ACDB-LOADER(189): Error: ACDB AudProc vol returned = -19
This time it seems it's sending an ACTION_VOICE_COMMAND
, so I tried to add it to the filter. 这次它似乎正在发送一个
ACTION_VOICE_COMMAND
,所以我试着将它添加到过滤器中。 I tried it two ways: 我尝试了两种方法:
Declaring the filter in the manifest: 在清单中声明过滤器:
<action android:name="android.intent.action.VOICE_COMMAND" />
Declaring the filter programmatically: 以编程方式声明过滤器:
protected void onResume() { IntentFilter f = new IntentFilter(Intent.ACTION_VOICE_COMMAND); registerReceiver(myReceiver, f); } private BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("AA", "ON RECEIVE"); } };
Again, I'm not receiving these events, I don't know why. 再说一次,我没有收到这些事件,我不知道为什么。
I finally managed to detect the events. 我终于设法检测到了这些事件。 I didn't know about this class:
我不知道这堂课:
http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html
Using the classes BluetoothAdapter, BluetoothHeadset and BluetoothDevice I can register a receiver using IntentFilter BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED and I'm able to detect clicks on my headset. 使用BluetoothAdapter,BluetoothHeadset和BluetoothDevice类我可以使用IntentFilter BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED注册一个接收器,我能够检测到我的耳机上的点击。
The problem with this is that the broadcast is non-ordered, so I can't abort it. 这个问题是广播是无序的,所以我不能中止它。 I can close the VoiceDialer activity right after it opens, but that's not what I want.
我可以在它打开后立即关闭VoiceDialer活动,但这不是我想要的。
I'll keep struggling with this. 我会一直在努力解决这个问题。
Thank you @Toaster for your efforts :) 谢谢@Toaster的努力:)
EDIT: 编辑:
Code used to detect the events: 用于检测事件的代码:
protected BluetoothAdapter mBluetoothAdapter;
protected BluetoothHeadset mBluetoothHeadset;
protected BluetoothDevice mConnectedHeadset;
protected AudioManager mAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null)
{
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isBluetoothScoAvailableOffCall())
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
mBluetoothAdapter.getProfileProxy(this, mHeadsetProfileListener, BluetoothProfile.HEADSET);
}
}
}
}
protected BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener()
{
/**
* This method is never called, even when we closeProfileProxy on onPause.
* When or will it ever be called???
*/
@Override
public void onServiceDisconnected(int profile)
{
mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
unregisterReceiver(mHeadsetBroadcastReceiver);
mBluetoothHeadset = null;
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
// mBluetoothHeadset is just a head set profile,
// it does not represent a head set device.
mBluetoothHeadset = (BluetoothHeadset) proxy;
// If a head set is connected before this application starts,
// ACTION_CONNECTION_STATE_CHANGED will not be broadcast.
// So we need to check for already connected head set.
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
if (devices.size() > 0)
{
// Only one head set can be connected at a time,
// so the connected head set is at index 0.
mConnectedHeadset = devices.get(0);
String log;
// The audio should not yet be connected at this stage.
// But just to make sure we check.
if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset))
{
log = "Profile listener audio already connected"; //$NON-NLS-1$
}
else
{
// The if statement is just for debug. So far startVoiceRecognition always
// returns true here. What can we do if it returns false? Perhaps the only
// sensible thing is to inform the user.
// Well actually, it only returns true if a call to stopVoiceRecognition is
// call somewhere after a call to startVoiceRecognition. Otherwise, if
// stopVoiceRecognition is never called, then when the application is restarted
// startVoiceRecognition always returns false whenever it is called.
if (mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset))
{
log = "Profile listener startVoiceRecognition returns true"; //$NON-NLS-1$
}
else
{
log = "Profile listener startVoiceRecognition returns false"; //$NON-NLS-1$
}
}
Log.d(TAG, log);
}
// During the active life time of the app, a user may turn on and off the head set.
// So register for broadcast of connection states.
registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
// Calling startVoiceRecognition does not result in immediate audio connection.
// So register for broadcast of audio connection states. This broadcast will
// only be sent if startVoiceRecognition returns true.
IntentFilter f = new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
f.setPriority(Integer.MAX_VALUE);
registerReceiver(mHeadsetBroadcastReceiver, f);
}
};
protected BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
int state;
int previousState = intent.getIntExtra(BluetoothHeadset.EXTRA_PREVIOUS_STATE, BluetoothHeadset.STATE_DISCONNECTED);
String log = "";
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED))
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
if (state == BluetoothHeadset.STATE_CONNECTED)
{
mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Audio should not be connected yet but just to make sure.
if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset))
{
log = "Headset connected audio already connected";
}
else
{
// Calling startVoiceRecognition always returns false here,
// that why a count down timer is implemented to call
// startVoiceRecognition in the onTick and onFinish.
if (mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset))
{
log = "Headset connected startVoiceRecognition returns true"; $NON-NLS-1$
}
else
{
log = "Headset connected startVoiceRecognition returns false";
}
}
}
else if (state == BluetoothHeadset.STATE_DISCONNECTED)
{
// Calling stopVoiceRecognition always returns false here
// as it should since the headset is no longer connected.
mConnectedHeadset = null;
}
}
else // audio
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED)
{
log = "Head set audio connected, cancel countdown timer";
}
else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED)
{
// The headset audio is disconnected, but calling
// stopVoiceRecognition always returns true here.
boolean returnValue = mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
log = "Audio disconnected stopVoiceRecognition return " + returnValue;
}
}
log += "\nAction = " + action + "\nState = " + state
+ " previous state = " + previousState;
Log.d(TAG, log);
}
};
Like I said, I can detect the events, but I can't aboard the broadcast. 就像我说的那样,我可以检测到这些事件,但我无法登上广播。
For the Voice Dialer action, add the following to your manifest: 对于Voice Dialer操作,请将以下内容添加到清单中:
<action android:name="android.intent.action.VOICE_COMMAND" />
<category android:name="android.intent.category.DEFAULT" />
As you deduced from your debug logs, ACTION_VOICE_COMMAND is the triggered action, but without CATEGORY_DEFAULT , your app will not be considered. 从调试日志中推断出, ACTION_VOICE_COMMAND是触发的操作,但如果没有CATEGORY_DEFAULT ,则不会考虑您的应用。 (I tested this with my own bluetooth headset, and it worked for me!)
(我用我自己的蓝牙耳机进行了测试,它对我有用!)
Since you have a method that receives broadcasts, can't you try to print what intent you receive when performing a long click on your headset, so you would know what event your phone receives in this case and handle it? 由于您有一种接收广播的方法,因此您不能尝试打印在长按耳机时收到的意图,这样您就可以知道手机在这种情况下接收的是什么事件并处理它?
@Override
public void onReceive(Context context, Intent intent) {
Log.d("SA", "ON RECEIVE" + intent.getAction()); // Print the received event
...
abortBroadcast();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.