0

I've been scouring the internet on this and can't quite find a solution. I want to use a hardware button accessory (bluetooth, nfc, or even simple 1/8th-inch mic jack) to do the equivalent of pressing the "microphone icon" on GBoard and start voice dictation.

That is, assume we are in an app with a text input field highlighted. The user would press the hardware button and GBoard would start listening for dictation as if the user had tapped on the microphone icon.

Any ideas? There are plenty of button solutions out there, including Android's built-in accessibility switch functionality but I can't come up with a way to map the button press specifically to the "start voice recognition" software button.

1 Answers1

0

This buttons is used to control playback with mediaplayer during media session or control phone call. This click is intercepted by android os. So, the way for you to intercept this process is to create dummy media session and handle hardware button clicks with MediaButtonReceiver. Nevertheless, any app (for example, running YouTube or Google Play Music) may steal your media button focus.

EDIT: To start voice recognition

public Intent getRecognizeIntent(String promptToUse, int maxResultsToReturn)
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResultsToReturn);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, promptToUse);
    return intent;
}

startActivityForResult(recognizeIntent, SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE);

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("Speech", "GOT SPEECH RESULT " + resultCode + " req: "
        + requestCode);
    if (requestCode == SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE)
    {
        if (resultCode == RESULT_OK)
        {
            ArrayList<String> matches = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            Log.d(D_LOG, "matches: ");
            for (String match : matches)
            {
                Log.d(D_LOG, match);
            }
        }
    }
}
Romadro
  • 626
  • 4
  • 10
  • I think what you are suggesting is that I build an app that uses the MediaButtonReceiver to receive button presses from a hardware button. That's fine, but I'm still not sure how I would start speech recognition on GBoard once I've captured the hardware button press. – Nathaniel Granor Sep 18 '19 at 16:44