androidbluetooth

Android Speech Recognizer with Bluetooth Microphone


I've been writing a chat app to work with bluetooth headsets/earphones. So far I've been able to record audio files via the mic in a bluetooth headset and I've been able to get Speech-to-text working with the Android device's built in microphone, using RecogniserIntent etc.

But I can't find a way of getting SpeechRecogniser to listen through the Bluetooth mic.Is it even possible to do so, and if so, how?

Current Device: Samsung Galax

Android Version: 4.4.2

Edit: I found some options hidden in my tablets settings for the Speech Recognizer, one of these is a tick box labeled "use bluetooth microphone" but it seems to have no effect.


Solution

  • Found the answer to my own question so I'm posting it for others to use:

    In order to get speak recognition to work with a Bluetooth Mic you first need to get the device as a BluetoothHeadset Object and then call .startVoiceRecognition() on it, this will set the mode to Voice recognition.

    Once finished you need to call .stopVoiceRecognition().

    You get the BluetoothHeadset as such:

    private void SetupBluetooth()
    {
        btAdapter = BluetoothAdapter.getDefaultAdapter();
    
        pairedDevices = btAdapter.getBondedDevices();
    
        BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
            public void onServiceConnected(int profile, BluetoothProfile proxy)
            {
                if (profile == BluetoothProfile.HEADSET)
                {
                    btHeadset = (BluetoothHeadset) proxy;
                }
            }
            public void onServiceDisconnected(int profile)
            {
                if (profile == BluetoothProfile.HEADSET) {
                    btHeadset = null;
                }
            }
        };
        btAdapter.getProfileProxy(SpeechActivity.this, mProfileListener, BluetoothProfile.HEADSET);
    
    }
    

    Then you get call startVoiceRecognition() and send off your voice recognition intent like so:

    private void startVoice()
    {
        if(btAdapter.isEnabled())
        {
            for (BluetoothDevice tryDevice : pairedDevices)
            {
                //This loop tries to start VoiceRecognition mode on every paired device until it finds one that works(which will be the currently in use bluetooth headset)
                if (btHeadset.startVoiceRecognition(tryDevice))
                {
                    break;
                }
            }
        }
        recogIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recogIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    
        recog = SpeechRecognizer.createSpeechRecognizer(SpeechActivity.this);
        recog.setRecognitionListener(new RecognitionListener()
        {
           .........
        });
    
        recog.startListening(recogIntent);
    }