title: Android - Function to route audio between smartphone and bluetooth module
body: <p>This is my first post on Stackoverflow.
  I try to route audio on android smartphone for the past few days but I don't find function to do that properly.</p>
  
  <p>It is for making a Bluetooth Babyphone.</p>
  
  <p>I need to make a routing between smartphone and Bluetooth module. The Bluetooth module is initially used to make Handset, cars Handfree, wireless speaker...</p>
  
  <p>I communicate with A2DP and HFP(Handfree)  profiles for the audio side of the project. </p>
  
  <p>I can establish a “sco” connection(connection used with Audio Bluetooth exchanges ) between devices and get back audio from Bluetooth module. But when a connection “sco” is working, I can no more use the speaker and microphone on my smartphone.</p>
  
  <p>I hope to find a solution to use audio on my smartphone and in the same time the audio on my Bluetooth module. </p>
  
  <p>I searched on <a href="http://developer.android.com/index.html">http://developer.android.com/index.html</a> for a function to route audio.</p>
  
  <p>The AudioManager class has some functions to route audio like setRouting or setParameters but I have any result yet.
  <a href="http://developer.android.com/reference/android/media/AudioManager.html">http://developer.android.com/reference/android/media/AudioManager.html</a></p>
  
  <p>You can see below the code I use to get audio from babyphone side(Bluetooth module side):</p>
  
  <pre><code>boolean isRecording=true;
  int buffersize = 8000;
  byte[] buffer = new byte[buffersize];
  
  //audio configuration and SCO Bluetooth connection. 
  AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  android.os.Process.setThreadPriority(
              android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  aManager.startBluetoothSco();
  aManager.setBluetoothScoOn(true);
  aManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  
  //AudioRecord configuation for recording audio from babyphone.
  AudioRecord  arec = new AudioRecord(
              MediaRecorder.AudioSource.VOICE_COMMUNICATION,
              8000,
              AudioFormat.CHANNEL_IN_MONO,
              AudioFormat.ENCODING_PCM_16BIT,
              buffersize); 
  
  //AudioTrack configuation for sending audio to smartphone speaker.                
  AudioTrack  atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                  8000,
                  AudioFormat.CHANNEL_OUT_MONO,
                  AudioFormat.ENCODING_PCM_16BIT,
                  buffersize,
                  AudioTrack.MODE_STREAM);
  
  atrack.setPlaybackRate(8000);
  
  //start audio recording and playing.
  arec.startRecording();
  atrack.play();
  
  while(isRecording) {
      arec.read(buffer, 0, buffersize);
  
      atrack.write(buffer, 0, buffer.length);
  }
  
  arec.stop();
  atrack.stop()
  </code></pre>
  
  <p>If I can not route audio to smartphone speaker and use “sco” connection in the same time to get back audio from microphone on the babyphone side, I need to know it as soon as possible for changing the direction of my project.</p>
  
  <p>The babyphone program on the smartphone side needs to run on all recent smartphones  if possible.</p>
  
  <p>English is not my first language so some sentence may not be correct. </p>
  
  <p>I’m open to any solution.</p>