title: How to record audio via bluetooth mic
body: <p>I am trying to record audio via bluetooth headset mic, but I can able to hear only the audio, but not able to record audio via bluetooth mic instead of I can record voice from mobile mic. </p>
  
  <p>I am using below code</p>
  
  <pre><code>IntentFilter newintent = new IntentFilter();
  newintent.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
  mContext.registerReceiver(mSCOHeadsetAudioState, newintent);
  
  AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  mAudioManager.setBluetoothScoOn(true);
  mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  mAudioManager.startBluetoothSco();
  
  private final BroadcastReceiver mSCOHeadsetAudioState = new BroadcastReceiver() {
  
      public void onReceive(Context context, Intent intent) {
  
          int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
  
          if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
              DisplayToast("BT Recording is Ready");
  
              onRecord();
  
          } else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
              DisplayToast("BT Recording Disabled");              
  
          }
      }
  };
  
  public void onRecord(){     
  
      String mFilename = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
      String mBTFileName = mFilename + "/BTRecord.3gp";
  
      final RecordThread BT = new RecordThread(false,mBTFileName);
  
  
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  
      // set title
      alertDialogBuilder.setTitle("Alert");
  
      // set dialog message
      alertDialogBuilder
          .setMessage("Recording...")
          .setCancelable(false)
          .setNeutralButton("Stop",new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,int id) {
                  // if this button is clicked, stop recording
                  BT.stopRecording();
                  mAudioManager.stopBluetoothSco();
                  // To do: start separation and play back here
  
                  dialog.dismiss();
              }
          });
  
      try{
          BT.prepareRecording();
      }catch(Exception e){
          Toast.makeText(this, "Recording prepare failed", Toast.LENGTH_SHORT).show();
          BT.stopRecording();
          this.finish();
      }
      try{
          mAudioManager.stopBluetoothSco();
          BT.start(); //Try to minimize delay here some how...
      }catch(Exception e){
          Toast.makeText(this, "Recording failed", Toast.LENGTH_SHORT).show();
          Log.e(LOG_TAG, "Record failed");
          BT.stopRecording();
          mAudioManager.stopBluetoothSco();
          this.finish();
      }           
  
      // create alert dialog
      alertDialog = alertDialogBuilder.create();
  
      // show it
      alertDialog.show();
  
  }
  
  final class RecordThread extends Thread implements Runnable{
  
      public String mFile;
      protected boolean isBT;
  
      private Camera access;
      private MediaRecorder mRecorder;
      private static final String LOG_TAG = "Recording thread";
  
      public RecordThread(boolean isBT, String file){
          this.isBT = isBT;
          this.mFile = file;
      }
      @Override
      public void run() throws RuntimeException{
  
          try{
              this.mRecorder.start();
          }catch(Exception e){
              Log.e(LOG_TAG,"Run Method Exception");
              RuntimeException re = new RuntimeException();
              throw re;
          }
  
      }
      public void prepareRecording() throws Exception{
          this.mRecorder = new MediaRecorder();
          this.mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          this.mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          this.mRecorder.setOutputFile(mFile);
          this.mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
  
          try {
              this.mRecorder.prepare();
          } catch (IOException e) {
              Log.e(LOG_TAG, "prepare() failed");
              if(access != null){
                  access.release();
              }
              throw e;
          }        
      }   
  
      public void stopRecording() {
          if(access != null){
              access.release();
          }
          try{
              mRecorder.stop();
              mRecorder.release();
              mRecorder = null;
          }catch(Exception e){        
              mRecorder = null;
          }
      }
  }
  </code></pre>