title: Routing phone microphone to Sco Bluetooth headset in Android
body: <p>How those chances upon my posts throw the last year saw how much I struggled with this application.
  Now I have reached to the final phase and I really cannot understand why the code doesn't work. What I'm trying to do is to route the audio stream from the phone microphone to a sco Bluetooth headset.
  The Phone is Samsung Galaxy s2 and the headset is Esco Plastronics. 
  I followed the documentation on android web site and this is what I have done so far:</p>
  
  <pre><code>tools.Log("&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;","");
  tools.Log("&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;","");
  tools.Log("&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;","");
  tools.Log("FBR.SetUpAudioRecoder", "In");
  public static final int SAMPPERSER =  8000;
  public int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
  public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
  public AudioManager AM = (AudioManager) mcontext.getSystemService(Context.AUDIO_SERVICE);
  
  buffersizebytes = AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration, audioEncoding);     buffersizebytes = (short) (buffersizebytes * 20);
  audioRecord = new AudioRecord(
        android.media.MediaRecorder.AudioSource.MIC,SAMPPERSEC,channelConfiguration,audioEncoding,buffersizeb   ytes); // constructor
  //Debug
  boolean tmp = AM.isBluetoothScoAvailableOffCall();//Returns true
  tools.Log("AM.isBluetoothScoAvailableOffCall() is -",tools.BoolToString(tmp));
  try {
      audioRecord.startRecording();
  } catch (IllegalStateException e) {
      tools.Log("FBR.GetAudioFromMic:startRecording:Error ",
                  e.getMessage());
      return "Get Audio From Mic Error";
  }
  AM.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
  AM.setBluetoothScoOn(true);
  AM.startBluetoothSco();// HERE I GET A BEEP ON THE HEADSET
  tmp = AM.isBluetoothScoOn();// Returns true
  tools.Log("AM.isBluetoothScoOn() is - ",    tools.BoolToString(tmp));
  tools.Log("&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; end debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;","");
  tools.Log("&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; end debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;","");
  tools.Log("&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; end debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;","");
  </code></pre>
  
  <p>The permitions on the manifest file are:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO"/&gt;
  &lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.BROADCAST_STICKY"/&gt;
  </code></pre>
  
  <p>The log gives me a lot of information which I cannot understand so I just attached few lines.</p>
  
  <pre><code>20 08:56:02.090:D/SpySitter(9659): &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; debug &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;-
  01-20 08:56:02.090: D/SpySitter(9659): FBR.SetUpAudioRecoder-In
  
  01-20 08:56:02.100: I/AudioHardwareYamaha(1833): getInputBufferSize() rate 8000, size 512 
  
  01-20 08:56:02.110: I/AudioPolicyManager(1833): getDeviceForInputSource()input source 1, device 00100000
  
  01-20 08:56:02.110: I/AudioHardwareYamaha(1833): AudioStreamIn::set(1, 16, 8000)
  
  01-20 08:56:02.110: I/AudioHardwareYamaha(1833): bufferSize() channels 2, bufferRatio 4, size 1024
  
  01-20 08:56:02.115: I/AudioHardwareYamaha(1833): AudioStreamIn::standby()
  
  01-20 08:56:02.120: D/SpySitter(9659): AM.isBluetoothScoAvailableOffCall() is - -true
  
  01-20 08:56:02.125: I/AudioService(1989): isMediaSilentMode false
  
  20 08:56:02.140: I/AudioPolicyManager(1833): getDeviceForStrategy() strategy 4, device 2
  
  01-20 08:56:02.145: I/AudioPolicyManager(1833): setForceUse() usage 2, config 3, mPhoneState 0
  
  01-20 08:56:03.475: E/DataRouter(1828): usb connection is true 
  
  01-20 08:56:03.475: E/DataRouter(1828): DSR is ON. Don't send DTR ON
  
  01-20 08:56:05.010: D/Bluetooth HS/HF(2240): initiateScoUsingVirtualVoiceCall: Received
  
  01-20 08:56:05.010: V/Bluetooth AT sent(2240): +CIEV: 3,2
  
  01-20 08:56:05.010: D/Bluetooth HS/HF(2240): initiateScoUsingVirtualVoiceCall: Sent Call-setup 
  procedure
  
  01-20 08:56:05.010: D/Bluetooth HS/HF(2240): audioOn()
  
  01-20 08:56:05.010: D/Bluetooth HS/HF(2240): setAudioState(11)
  
  01-20 08:56:05.015: D/Bluetooth HSHFP(2240): AudioStateIntent:  State: 11 PrevState: 10
  
  01-20 08:56:05.015: V/BluetoothSocket.cpp(2240): initSocketNative
  
  01-20 08:56:05.015: V/BluetoothSocket.cpp(2240): ...fd 99 created (SCO, lm = 0)
  
  01-20 08:56:05.015: V/BluetoothSocket.cpp(2240): initSocketFromFdNative
  
  01-20 08:56:05.040: D/Bluetooth HS/HF(2240): initiateScoUsingVirtualVoiceCall: Done
  
  01-20 08:56:05.045: I/BluetoothPolicyService(1989): isPairingEnabled 
  
  01-20 08:56:05.055: D/BluetoothUtils(2240): isSocketAllowedBySecurityPolicy start : device null
  
  01-20 08:56:05.055: V/BluetoothSocket.cpp(2240): connectNative
  
  01-20 08:56:05.065: V/Bluetooth AT recv(2240): AT+CLCC
  
  01-20 08:56:05.070: V/Bluetooth AT sent(2240): +CLCC: 1,0
  
  01-20 08:56:05.080: V/BluetoothSocket.cpp(2240): ...connect(99, SCO) = 0 (errno 115)
  
  01-20 08:56:05.080: D/Bluetooth HS/HF(2240): Routing audio for outgoing SCO conection
  
  01-20 08:56:16.455: D/SpySitter(9659): &amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp; end debug&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;
  </code></pre>
  
  <p>alot of thanks for helping</p>