0

我打算使用Android语音识别,但Android只使用设备麦克风,而不是蓝牙。我能做些什么?

bluetooth 和 bluetooth_admin 的权限已设置。

蓝牙耳机已连接并在通话或其他任何情况下都能正常工作。

这是代码:

package com.example.test10;

import java.util.ArrayList;
import java.util.List;

import android.media.AudioManager;

import android.os.Bundle;
import android.app.Activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;

import android.view.View;

import android.widget.Toast;

public class MainActivity extends Activity {
 private static final int REQUEST_CODE = 1234;

protected BluetoothAdapter mBluetoothAdapter;
protected BluetoothHeadset mBluetoothHeadset;
protected BluetoothDevice mConnectedHeadset;

protected AudioManager mAudioManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);






}

public void test(View view)
{
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter != null)
    {
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.startBluetoothSco();
        mBluetoothAdapter.getProfileProxy(this,      mHeadsetProfileListener,BluetoothProfile.HEADSET);


    }
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(
            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "JArvis.");
    startActivityForResult(intent, REQUEST_CODE);
}

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
        {

            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);

            Toast.makeText(this, matches.toString(), 0).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

protected BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener()
{


    @Override
    public void onServiceDisconnected(int profile)
    {

        mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);

        mBluetoothHeadset = null;
    }

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy)
    {


        mBluetoothHeadset = (BluetoothHeadset) proxy;


        List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
        if (devices.size() > 0)
        {

            mConnectedHeadset = devices.get(0);

            mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);


        }

    }
};

}
4

1 回答 1

0

尝试使用 AudioManager.setMode(AudioManager.MODE_IN_CALL);

于 2013-04-17T12:28:37.420 回答