3

I am developing an app that uses voice recognition and I want to disable internal microphone while I use a Bluetooth headset. The problem is that the internal android microphone keep listening and the recognition engine recognize words that I don't want (other peoples speak near phone, or ambiental noise make recognition useful). Thank you!


public class BluetoothHelper extends BluetoothHeadsetUtils {

    private Context _mContext;
    private AudioManager audioManager;
    private int audioModeBackup;

    public BluetoothHelper(Context context) {
        super(context);
        this._mContext = context;
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public void onScoAudioDisconnected() {
        // Cancel speech recognizer if desired
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamSolo(AudioManager.USE_DEFAULT_STREAM_TYPE, true);


        Log.d(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                + audioManager.isBluetoothScoAvailableOffCall());
        Toast.makeText(_mContext, "SCO Audio disconnected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onScoAudioConnected() {
        // Should start speech recognition here if not already started
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
        Log.d(BluetoothHelper.class.getSimpleName(), "Is bluetooth sco on: "+audioManager.isBluetoothScoOn());

        Log.d(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                + audioManager.isBluetoothScoAvailableOffCall()+" SCO ON: ");
        Toast.makeText(_mContext, "SCO Audio connected.  Audio is on headset SCO: " + this.isOnHeadsetSco(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHeadsetDisconnected() {
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);

        Log.i(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                + audioManager.isBluetoothScoAvailableOffCall());
        Toast.makeText(_mContext, "Bluetooth Headset Off", Toast.LENGTH_SHORT).show();
        /* Unmute the external microphone */
        setInternalMicMute(false);
    }

    @Override
    public void onHeadsetConnected() {
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);

        if(!audioManager.isBluetoothA2dpOn()){
            Log.d("BluetoothHelper", "Reset connection with bluetooth");
            this.setStarted(false);
            this.start();
        }
        else{
            this.mIsCountDownOn = true;
            Log.i(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                    + audioManager.isBluetoothScoAvailableOffCall());
            Toast.makeText(_mContext, "Bluetooth Headset On.", Toast.LENGTH_SHORT).show();
            /* Mute the external microphone */
            setInternalMicMute(true);
            this.mCountDown11.start();
        }

    }


    private void setInternalMicMute(boolean mute) {

    }
}

Updated question! I am using these helper class to detect when a Bluetooth headset is connected, and after that I call startVoiceRecognition() from BluetoothHeadset class.

Floern
  • 33,559
  • 24
  • 104
  • 119
cortex
  • 31
  • 1
  • 3

1 Answers1

0

Android should manage to use headset mic when available does it work using:

setAudioSource(AudioSource.DEFAULT);

More info here.

Hope this helps.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • But I am not using media recorder at all – cortex Sep 28 '15 at 11:11
  • Then what are you using, we need more info to help you. – Nanoc Sep 28 '15 at 11:37
  • updated the question. Thank you ! What MediaRecorder has to do with voice recognition ? Do I have to create an instance and set the audio source before starting the recognition engine ? – cortex Sep 28 '15 at 13:01