Obsługa kliknięć zestawu słuchawkowego bluetooth (polecenie głosowe akcji i wyszukiwanie w Internecie akcji) na Androidzie

Pracuję nad aplikacją Android i chcę, aby działała ona za pomocą kliknięć przycisków headset. Testuję go w Nexus 5 z Androidem KitKat 4.4.

[22]}próbowałem najpierw z prostym zestawem słuchawkowym (nie bezprzewodowym). Odebrane Zdarzenie przycisku to KEYCODE_HEADSETHOOK (79). Stworzyłem MEDIA_BUTTON receiver aby obsłużyć jego kliknięcia:
<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>

Jest to czynność trzymająca odbiornik:

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();
        }
    }
}

Ten kod działa z moim przewodowym zestawem słuchawkowym tylko wtedy, gdy użytkownik wykona krótkie kliknięcie. Wykonanie długiego kliknięcia otwiera Google Voice Search. Chciałbym też uchwycić długie kliknięcia, ale nie mam nic przeciwko, jeśli nie jest to możliwe.

Potem Przetestowałem go z bluetooth zestawem słuchawkowym. W szczególności używam zestawu słuchawkowego Moveteck Bluetooth BH119A (możesz zobaczyć obraz na dole tego posta). Ten zestaw słuchawkowy ma tylko jeden przycisk, a jeśli go nacisnę, otworzy się następujący "activity":

Tutaj wpisz opis obrazka

Chciałbym również uchwycić to zdarzenie kliknięcia, jeśli mój Activity jest otwarty. Jak mogę to zrobić? Próbowałem dodać następujące filtry do mojego odbiornika, ale też nie działa:

<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" />

Próbowałem też nadpisać onKeyDown w mojej aktywności, ale nie została uruchomiona.

Ktoś wie, jak Mogę przechwycić te wydarzenia?

To jest mój zestaw słuchawkowy bluetooth:

Tutaj wpisz opis obrazka


* * EDIT * *

Za radą tostera, sprawdziłem cały dziennik, szukając zdarzeń, które wyzwalają mój zestaw słuchawkowy.

Przewodowy zestaw słuchawkowy long click

To jest Zaloguj się po długim kliknięciu w mój przewodowy zestaw słuchawkowy (otwiera się 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

Wydaje się, że wywołuje ACTION_WEB_SEARCH Zdarzenie, więc próbowałem dodać je do filtra. Próbowałem na dwa sposoby:

  1. Deklarowanie filtra w manifeście:

    <action android:name="android.intent.action.WEB_SEARCH" />
    
  2. Deklarowanie filtra programowo:

    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");
        }
    };
    

Żadna z tych opcji nie działa. Jak powiedziałem, ten scenariusz nie ma większego znaczenia, poradzę sobie z tym.


Bezprzewodowy Zestaw Słuchawkowy prosty kliknij

Bezprzewodowy Zestaw Słuchawkowy simple click to ten, który otwiera Dialer głosowy i jest to wydarzenie, które naprawdę muszę uchwycić. To jest wyjście dziennika:

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

Tym razem wydaje się, że wysyła ACTION_VOICE_COMMAND, więc próbowałem dodać go do filtra. Próbowałem na dwa sposoby:

  1. Deklarowanie filtra w manifeście:

    <action android:name="android.intent.action.VOICE_COMMAND" />
    
  2. Deklarowanie filtra programowo:

    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");
        }
    };
    

Ponownie, nie otrzymuję tych wydarzenia, Nie wiem dlaczego.

Author: Palec, 2013-12-05

3 answers

W końcu udało mi się wykryć zdarzenia. Nie wiedziałem o tej klasie:

Http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

Używając klas BluetoothAdapter, BluetoothHeadset i BluetoothDevice mogę zarejestrować odbiornik używając IntentFilter BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED i jestem w stanie wykryć kliknięcia na moim zestawie słuchawkowym.

Problem w tym, że transmisja nie jest zamówiona, więc nie mogę jej przerwać. Mogę Zamknij aktywność VoiceDialer zaraz po jej otwarciu, ale nie tego chcę. Będę się z tym zmagał.

Dziękuję @ toster za starania :)

EDIT:

Kod używany do wykrywania zdarzeń:

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);

    }
};
Jak mówiłem, mogę wykryć wydarzenia, ale nie mogę wejść na transmisję.
 10
Author: PX Developer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-20 08:50:40

Dla akcji Dialer głosowy, dodaj następujące do manifestu:

<action android:name="android.intent.action.VOICE_COMMAND" />
<category android:name="android.intent.category.DEFAULT" />

Jak wywnioskowałeś z dzienników debugowania, ACTION_VOICE_COMMANDjest aktywowaną akcją, ale bez CATEGORY_DEFAULT, Twoja aplikacja nie będzie brana pod uwagę. (Testowałem to z własnym zestawem słuchawkowym bluetooth i zadziałało dla mnie!)

 4
Author: Alice Purcell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-27 01:57:23

Ponieważ masz metodę, która odbiera transmisje, nie możesz spróbować wydrukować, jakie intencje otrzymujesz, wykonując długie kliknięcie na zestawie słuchawkowym, aby wiedzieć, jakie zdarzenie odbiera telefon w tym przypadku i obsłużyć go?

@Override
    public void onReceive(Context context, Intent intent) {

        Log.d("SA", "ON RECEIVE" + intent.getAction()); // Print the received event

        ...

        abortBroadcast();
    }
 1
Author: Basile Perrenoud,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-09 15:34:29