title: Programmatically connect to paired Bluetooth speaker and play audio
body: <p>In our app, I'd like to connect to a previously paired A2DP Bluetooth Speaker and direct audio playback to it, using Android v4.2 or later.</p>
  
  <p>I can successfully create an A2DP profile object using this code to start the process:</p>
  
  <hr>
  
  <pre><code>/* Manifest permissions */
  &lt;uses-permission android:name="android.permission.BLUETOOTH"/&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>// Get the default adapter
  BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  
  // Establish connection to the proxy.
  mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.A2DP)
  </code></pre>
  
  <p>And the following listener to respond to the connection:</p>
  
  <pre><code>private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
  public void onServiceConnected(int profile, BluetoothProfile proxy) {
  
      if (profile == BluetoothProfile.A2DP) {
  
          mBluetoothSpeaker = (BluetoothA2dp) proxy;
  
          // no devices are connected         
          List&lt;BluetoothDevice&gt; connectedDevices = mBluetoothSpeaker.getConnectedDevices();
  
          //the one paired (and disconnected) speaker is returned here
          int[] statesToCheck = {BluetoothA2dp.STATE_DISCONNECTED};           
          List&lt;BluetoothDevice&gt; disconnectedDevices = mBluetoothSpeaker.getDevicesMatchingConnectionStates(statesToCheck);
  
  
  
          BluetoothDevice btSpeaker = disconnectedDevices.get(0); 
  
          //WHAT NOW?
  
      }
  }
  public void onServiceDisconnected(int profile) {
      if (profile == BluetoothProfile.A2DP) {
          mBluetoothSpeaker = null;
      }
  }
  };
  </code></pre>
  
  <p>I'm just a little lost as to what to do now, to connect the device, and direct the audio output to it. I've tried connecting to the device, <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient" rel="noreferrer">as detailed in the Android docs</a>, with the following code, but the final <code>BluetoothSpeaker.getConnectedDevices()</code> call returns no connected devices.</p>
  
  <pre><code>    BluetoothSocket tmp = null;
      UUID MY_UUID = UUID.fromString("00001108-0000-1000-8000-00805f9b34fb");
      try {           
          tmp = btSpeaker.createInsecureRfcommSocketToServiceRecord(MY_UUID);
  
      } catch (IOException e1) {
          // TODO Auto-generated catch block
          Log.d("createRfcommSocketToServiceRecord ERROR", e1.getMessage());
      }
      mmSocket = tmp;
  
      try {
          // Connect the device through the socket. This will block
          // until it succeeds or throws an exception
          mmSocket.connect();
      } catch (IOException connectException) {
          // Unable to connect; close the socket and get out
          try {
              Log.d("connectException", connectException.getMessage());
              mmSocket.close();
          } catch (IOException closeException) { }
          return;
      }
  
      connectedDevices = mBluetoothSpeaker.getConnectedDevices();
  </code></pre>
  
  <p>The code does seem to connect to the device in some way though, as when I stop execution, the Bluetooth speaker annouces that it is ready to pair (as it always does when it disconnects from an audio source).</p>
  
  <p>Older versions of the <code>BluetoothA2dp</code> seem to have a <code>connect(BluetoothDevice device)</code> method, but that has now been removed (as of 4.2) and I'm struggling to find any clear examples of how to programmatically connect to an A2DP device, and to direct audio output to it. Any help on how to approach either would be gratefully received.</p>
  
  <p>Any advice on how to approach this would be hugely appreciated.</p>