带蓝牙麦克风的Android语音识别器

时间:2015-01-22 10:24:58

标签: android bluetooth

我一直在写一个聊天应用程序来使用蓝牙耳机/耳机。 到目前为止,我已经能够通过蓝牙耳机和麦克风录制音频文件 我已经能够使用Android设备的内置麦克风,使用RecogniserIntent等进行语音转文本处理。

但我找不到让SpeechRecogniser通过蓝牙麦克风收听的方法。它甚至可以这样做,如果是这样,怎么办?

当前设备:三星Galax

Android版本:4.4.2

编辑:我发现语音识别器的平板电脑设置中隐藏了一些选项,其中一个是标有“使用蓝牙麦克风”的复选框,但似乎没有任何效果。

1 个答案:

答案 0 :(得分:4)

找到我自己的问题的答案,以便我发布给其他人使用:

为了让说话识别与蓝牙麦克风配合使用,您首先需要将设备作为BluetoothHeadset对象,然后在其上调用.startVoiceRecognition(),这会将模式设置为语音识别。

完成后,您需要调用.stopVoiceRecognition()。

您可以获得BluetoothHeadset:

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

}

然后你会调用startVoiceRecognition()并发出你的语音识别意图:

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);
}
相关问题