title: Android Bluetooth Headset Speech Recognition
body: <p>I plan to use Android speech recognition, but Android only uses the device microphone, not the bluetooth one. What can I do?</p>
  
  <p>Permissions of bluetooth and bluetooth_admin are set.</p>
  
  <p>Bluetooth headset is connected and work good in call or whatever.</p>
  
  <p>Here is the Code:</p>
  
  <pre><code>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&lt;ResolveInfo&gt; 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 &amp;&amp; resultCode == RESULT_OK)
          {
  
              ArrayList&lt;String&gt; 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&lt;BluetoothDevice&gt; devices = mBluetoothHeadset.getConnectedDevices();
          if (devices.size() &gt; 0)
          {
  
              mConnectedHeadset = devices.get(0);
  
              mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);
  
  
          }
  
      }
  };
  
  }
  </code></pre>