title: How to programmatically connect to a Bluetooth device after it's already paired (bonded) in Android
body: <p>I have been working on trying to get a Bluetooth device like a keyboard or a remote to connect to an android device. More specifically when this program runs for the first time it would scan for Bluetooth devices and attempt to pair and connect with one that it finds. I have tried seemingly every possible way to accomplish this but I am only able to pair the device, not connect it completely.
  I have tried the examples in the Android Bluetooth guide and many others. One consistency is the javi.io error I get when the BluetoothSocket is calling connect. </p>
  
  <pre><code>java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
  at BTConnectThread.run(BTConnectThread.java:61)
  </code></pre>
  
  <p>I have tried different UUIDs. Some I generated myself others I pulled from the devices. I also tried writing code assuming both are acting as servers that mirrors mostly what I am doing here and what is in the Android Bluetooth guide.  have tried all variations of calling createBond() on the device.  All attempts leave the device paired/bonded but not connected. Any help is greatly appreciated.
  `    public BTConnectThread(BluetoothDevice bluetoothDevice) {</p>
  
  <pre><code>    BluetoothSocket tempSocket = null;
  
      try {
         // tempSocket =        bluetoothDevice.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
        //  tempSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
  
          //Magic?
          Method method = bluetoothDevice.getClass().getMethod("createRfcommSocket",
              new Class[]{int.class});
          tempSocket = (BluetoothSocket) method.invoke(bluetoothDevice, 1);
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      m_bluetoothSocket = tempSocket;
  
  }
  
  public void run() {
  
      //cancel discovery
      BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      if (bluetoothAdapter != null)
          bluetoothAdapter.cancelDiscovery();
  
      //TODO: Try brute force approach. Loop until it connects.
      //TODO: Try a fallback socket.
      try {
          m_bluetoothSocket.connect();
          Log.d(TAG, "Connection Established");
  
      } catch (IOException connectException) {
          // Unable to connect; close the socket and get out
          Log.d(TAG, "Fail to connect!", connectException);
          try {
              m_bluetoothSocket.close();
          } catch (IOException closeException) {
              Log.d(TAG, "Fail to close connection", closeException);
          }
          return;
      }
  }
  
  public void cancel() {
  
      try {
          m_bluetoothSocket.close();
      } catch (IOException e) {
  
      }
  }`
  </code></pre>