title: Understanding Bluetooth Headset connection with Android
body: <p>I have a Bluetooth headset (BTC6White). I want to speak into the microphone and an Android device play the sound.</p>
  
  <p>So, How I can do this? First, I can establish a socket connection</p>
  
  <pre><code>socket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString("0000111e-0000-1000-8000-00805f9b34fb"));
  socket.connect();
  </code></pre>
  
  <p>Then, how I get the audio? What is the use this method: startBuetoothSco? To put the audio in the speaker, Should I use Auditrack?</p>
  
  <pre><code>int buffersize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT);
  soundData = new byte [buffersize*5];    
  audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 
     8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT, 
     soundData.length,AudioTrack.MODE_STREAM);
  </code></pre>
  
  <p>But, then, Should I fill the buffer soundData? How? Using the socket (into a Thread)?</p>
  
  <pre><code>  mmInStream = socket.getInputStream();
    public void run() {
    byte[] buffer = new byte[8000*2]; 
        while (true) {
             bytes = mmInStream.read(buffer);
              audioTrack.write(buffer, 0, bytes); //write directly?
        }}
  </code></pre>
  
  <p>And startBuetoothSco() for what? To know sco states? SCO_AUDIO_STATE_CONNECTED... Or to send/receive data? I don't understand how get de audio data from de headset and then how to streaming to the speaker. It's necessary establish a SCO connection (with AudioManager) to get the data of the bluetooth headset?</p>
  
  <p>It's very difficult find information about this problem and the android documentation is very poor (this topic). </p>