Java 无法从android studio中的蓝牙麦克风获取输入

Java 无法从android studio中的蓝牙麦克风获取输入,java,android-studio,speech-recognition,android-bluetooth,microphone,Java,Android Studio,Speech Recognition,Android Bluetooth,Microphone,在这里,我试图通过蓝牙设备的麦克风而不是语音设备获取输入。我还将接收器广播包含在清单中,但我无法通过蓝牙麦克风获取输入 public class BTReceiver extends BroadcastReceiver { private static final String TAG = "BTReceiver"; int state = 0; AudioManager audioManager; @RequiresApi(api = Bui

在这里,我试图通过蓝牙设备的麦克风而不是语音设备获取输入。我还将接收器广播包含在清单中,但我无法通过蓝牙麦克风获取输入

public class BTReceiver extends BroadcastReceiver {
    private static final String TAG = "BTReceiver";
    int state = 0;
    AudioManager audioManager;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Z", "Received: Bluetooth");
        try {
            Bundle extras = intent.getExtras();
            if (extras != null) { //Do something
                audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

                String action = intent.getAction();
                Toast.makeText(context, action, Toast.LENGTH_LONG).show();
                int state;
                if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
                    state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
                            BluetoothHeadset.STATE_DISCONNECTED);
                    if (state == BluetoothHeadset.STATE_CONNECTED) {
                        setModeBluetooth();
                    } else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
                        // Calling stopVoiceRecognition always returns false here
                        // as it should since the headset is no longer connected.
                        setModeNormal();
                        Log.d(TAG, "Headset disconnected"); //$NON-NLS-1$
                    }
                } else // audio
                {
                    state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
                    Log.d(TAG, "\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
                    if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
                        Log.d(TAG, "\nHeadset audio connected");  //$NON-NLS-1$
                        setModeBluetooth();
                    } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
                        setModeNormal();
                        Log.d(TAG, "Headset audio disconnected"); //$NON-NLS-1$
                    }
                }
            }
        } catch (Exception e) {
            Log.d("Exception", "Exception " + e.toString());
        }
    }


    private void setModeBluetooth() {
        try {
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            audioManager.startBluetoothSco();
            audioManager.setBluetoothScoOn(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setModeNormal() {
        try {
            audioManager.setMode(AudioManager.MODE_NORMAL);
            audioManager.stopBluetoothSco();
            audioManager.setBluetoothScoOn(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 protected void onResume(){
        super.onResume();
        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.
        registerReceiver(mHeadsetBroadcastReceiver,
                new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
    }
    @Override
    protected void  onPause(){
        unregisterReceiver(mHeadsetBroadcastReceiver);
        super.onPause();
    }


我只能从手机麦克风获取输入

我在活动中使用语音识别,我刚刚意识到,如果我连接了耳机,识别器仍然只能通过音素进行监听。有没有办法让它通过耳机收听

public class BTReceiver extends BroadcastReceiver {
    private static final String TAG = "BTReceiver";
    int state = 0;
    AudioManager audioManager;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Z", "Received: Bluetooth");
        try {
            Bundle extras = intent.getExtras();
            if (extras != null) { //Do something
                audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

                String action = intent.getAction();
                Toast.makeText(context, action, Toast.LENGTH_LONG).show();
                int state;
                if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
                    state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
                            BluetoothHeadset.STATE_DISCONNECTED);
                    if (state == BluetoothHeadset.STATE_CONNECTED) {
                        setModeBluetooth();
                    } else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
                        // Calling stopVoiceRecognition always returns false here
                        // as it should since the headset is no longer connected.
                        setModeNormal();
                        Log.d(TAG, "Headset disconnected"); //$NON-NLS-1$
                    }
                } else // audio
                {
                    state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
                    Log.d(TAG, "\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
                    if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
                        Log.d(TAG, "\nHeadset audio connected");  //$NON-NLS-1$
                        setModeBluetooth();
                    } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
                        setModeNormal();
                        Log.d(TAG, "Headset audio disconnected"); //$NON-NLS-1$
                    }
                }
            }
        } catch (Exception e) {
            Log.d("Exception", "Exception " + e.toString());
        }
    }


    private void setModeBluetooth() {
        try {
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            audioManager.startBluetoothSco();
            audioManager.setBluetoothScoOn(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setModeNormal() {
        try {
            audioManager.setMode(AudioManager.MODE_NORMAL);
            audioManager.stopBluetoothSco();
            audioManager.setBluetoothScoOn(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 protected void onResume(){
        super.onResume();
        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.
        registerReceiver(mHeadsetBroadcastReceiver,
                new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
    }
    @Override
    protected void  onPause(){
        unregisterReceiver(mHeadsetBroadcastReceiver);
        super.onPause();
    }


<receiver
         android:name="BTReceiver"
            android:enabled="true"
            android:permission="android.permission.BLUETOOTH">
            <intent-filter>
                <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
            </intent-filter>
        </receiver>