title: Android: Playing sound over Sco Bluetooth headset
body: <p>For the past few days I have been trying to play any sound over my sco bluetooth headset from my android phone. My final goal with this project is to eventually make a garage door opener, but first I need to be able to play sound over the headset.</p>
  
  <p>Here is the basis of the current code I'm using:</p>
  
  <pre><code>==Manifest==
  &lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /&gt;
  
  
  ==Code==
      audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  
      audioManager.setMode(AudioManager.MODE_IN_CALL);
      audioManager.startBluetoothSco();
      audioManager.setBluetoothScoOn(true);
  
      short[] soundData = new short [8000*20];
      for (int iii = 0; iii &lt; 20*8000; iii++) {
          soundData[iii] = 32767;
          iii++;
          soundData[iii] = -32768;
      }
  
      audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
              8000, AudioFormat.CHANNEL_OUT_MONO,
              AudioFormat.ENCODING_PCM_16BIT, soundData.length
              * Short.SIZE, AudioTrack.MODE_STATIC);
      audioTrack.write(soundData, 0, soundData.length);
      audioTrack.play();
  </code></pre>
  
  <p>Before I run this, I pair my bluetooth headset to my phone and have it connected. I have verified it works by calling my voicemail. When I run my code however, no sound comes from anywhere.</p>
  
  <p>Here are the effects of the different lines of code:</p>
  
  <p>When I'm just running my app:</p>
  
  <pre><code>    audioManager.setMode(AudioManager.MODE_IN_CALL);
  </code></pre>
  
  <p>This line makes all of my sound stop working no matter what I do, so it is typically commented out.</p>
  
  <pre><code>    audioManager.startBluetoothSco();
      audioManager.setBluetoothScoOn(true);
  </code></pre>
  
  <p>These two lines make the sound stop coming out of the front speaker and make my headset click and hiss like it's turned on but there's no output.</p>
  
  <pre><code>AudioManager.STREAM_VOICE_CALL
  </code></pre>
  
  <p>This is part of my call to the AudioTrack constructor, but it makes quite a difference. Since this is set to STREAM_VOICE_CALL the sound comes out of the front speaker, if I set this to STREAM_MUSIC, the sound comes out the back speaker instead.</p>
  
  <p>When I open my app during a call:</p>
  
  <pre><code>    audioManager.setMode(AudioManager.MODE_IN_CALL);
  </code></pre>
  
  <p>During a call, this line has no effect because MODE_IN_CALL was already set. But what's different however is that my sound is mixed with the phone call, whereas normally it doesn't play at all.</p>
  
  <pre><code>    audioManager.startBluetoothSco();
      audioManager.setBluetoothScoOn(true);
  </code></pre>
  
  <p>These, with their counterpart off halves, control where the audio is coming from. If I turn these off, my sound and the telephone call come from the front speaker, with these on, the telephone call comes from my headset and my sound is lost.</p>
  
  <p>As to why my code is not working, I honestly have no idea. I believe I have fulfilled the checklist for using <a href="http://developer.android.com/reference/android/media/AudioManager.html#startBluetoothSco%28%29">startBluetoothSco()</a>.</p>
  
  <pre><code>Even if a SCO connection is established, the following restrictions 
  apply on audio output streams so that they can be routed to SCO headset: 
  - the stream type must be STREAM_VOICE_CALL - the format must be mono - 
  the sampling must be 16kHz or 8kHz
  </code></pre>
  
  <p>So, does anyone have an idea of what I am doing wrong? There was one time that I managed to get my sound to play through the headset, but it was only a short tone when I forgot to stop() my AudioTrack so I have to assume it was a glitch.</p>