title: Capture Audio through Bluetooth Headset paired with Android Device
body: <p>I am trying to capture audio from a Bluetooth Headset paired with an Android Device.</p>
  
  <p>Following is the relevant code:</p>
  
  <pre><code>Intent in=null;
  final int bufferSize=BufferElements2Rec*BytesPerElement;
  
  final BroadcastReceiver brr=new BroadcastReceiver()
  {
      @Override
      public void onReceive(Context context,Intent intent)
      {
          int state=intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1);
          Log.d(labelApp,"Audio SCO State = "+state);
          if(AudioManager.SCO_AUDIO_STATE_CONNECTED==state)
          {
              Log.d(labelApp,"Entered and Starting Recording");
              //recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
              //        RECORDER_SAMPLERATE, RECORDER_CHANNELS,
              //        RECORDER_AUDIO_ENCODING, bufferSize);
              recorder = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC,
                  RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                  RECORDER_AUDIO_ENCODING, bufferSize);
              if(recorder==null)
              {
                  Log.d(labelApp,"null");
              }
              else
              {
                  Log.d(labelApp,"not null");
              }
              recorder.startRecording();
              recordingThread=new Thread(new Runnable()
              {
                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      writeAudioDataToFile();
                  }
  
              },"AudioRecorder Thread");
              recordingThread.start();
  
              Log.d(labelApp,"Launched Recording Thread");
          }
      }
  };
  
  try
  {
      Log.d(labelApp,"Initializing BT");
      am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      //am.setMode(AudioManager.MODE_IN_CALL);
  
      //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
      //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
      Log.d(labelApp,"Starting Bluetooth");
      am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
      am.setBluetoothScoOn(true);
      am.setMode(AudioManager.MODE_IN_CALL);
      am.startBluetoothSco();
      Log.d(labelApp,"Can BT record from mic? "+am.isBluetoothScoAvailableOffCall());
      //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
      in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
      // The following line makes the audio go to hell
      //am.setMode(AudioManager.MODE_IN_CALL);
  
  
      //am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
      Log.d(labelApp,"Everything initializated");
      Log.d(labelApp,"Recorder is...");
  }
  catch(Exception e)
  {
      Log.e(labelApp,"exception",e);
      writeStack(e);
  }
  
  try
  {
          Log.d(labelApp,"Initializing BT");
          am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
          //am.setMode(AudioManager.MODE_IN_CALL);
  
          //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
          //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
          Log.d(labelApp,"Starting Bluetooth");
          am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
          am.setBluetoothScoOn(true);
          am.setMode(AudioManager.MODE_IN_CALL);
          am.startBluetoothSco();
          Log.d(labelApp,"Can BT record from mic? "+am.isBluetoothScoAvailableOffCall());
          //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
          in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
          // The following line makes the audio go to hell
          //am.setMode(AudioManager.MODE_IN_CALL);
  
  
          //am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
          Log.d(labelApp,"Everything initializated");
          Log.d(labelApp,"Recorder is...");
  }
  catch(Exception e)
  {
          Log.e(labelApp,"exception",e);
          writeStack(e);
  }
  </code></pre>
  
  <p>The Manifest asks permissions for:</p>
  
  <ul>
  <li>WRITE_EXTERNAL_STORAGE</li>
  <li>RECORD_AUDIO</li>
  <li>INTERNET</li>
  <li>MODIFY_AUDIO_SETTINGS</li>
  <li>BROADCAST_STICKY</li>
  <li>BLUETOOTH</li>
  <li>BLUETOOTH_ADMIN</li>
  </ul>
  
  <p>The typical Filtered LogCat output per the app is:</p>
  
  <ol>
  <li>Initializing BT</li>
  <li>Starting Bluetooth</li>
  <li>Can BT record from mic? true</li>
  <li>Everything initialized</li>
  <li>Recorder is...</li>
  <li>Audio SCO State = 2</li>
  <li>Audio SCO State = 1</li>
  <li>Entered and Starting Recording</li>
  <li>not null</li>
  <li>Launched Recording Thread</li>
  </ol>
  
  <p>When the am.startBluetoothSco(); is invoked, I can hear a brief noise on the BT device, but then the app simply gets the audio from the Android Device's mic in place of the BT's one.</p>
  
  <p>Any hint on what am I missing/doing wrong?</p>
  
  <p>Thanks in advance for the attention</p>