title: Java android connect to device by Bluetooth
body: <p>I try to connect to the device used by  Bluetooth. When I try to connect I get :</p>
  
  <pre><code>java.io.IOException: read failed, socket might be closed or timeout, read ret: -1
  </code></pre>
  
  <p><strong>I do this:</strong></p>
  
  <pre><code>BluetoothSocket mmSocket;
  
   public ConnectThread(BluetoothDevice device) {
              mmDevice = device;
              BluetoothSocket tmp = null;
              // Get a BluetoothSocket for a connection with the
              // given BluetoothDevice
              try {
                  tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
              } catch (IOException e) {
              }
              mmSocket = tmp;
          }
  </code></pre>
  
  <p><strong>And when I do this :</strong></p>
  
  <pre><code>mmSocket.connect();
  </code></pre>
  
  <p>I get a exceptions</p>
  
  <p>This is my connect thread :</p>
  
  <pre><code>private class ConnectThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final BluetoothDevice mmDevice;
  
      public ConnectThread(BluetoothDevice device) {
          mmDevice = device;
          BluetoothSocket tmp = null;
          // Get a BluetoothSocket for a connection with the
          // given BluetoothDevice
          try {
              tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
          } catch (IOException e) {
          }
          mmSocket = tmp;
      }
  
      public void run() {
          setName("ConnectThread");
          // Always cancel discovery because it will slow down a connection
          mAdapter.cancelDiscovery();
          // Make a connection to the BluetoothSocket
          try {
              // This is a blocking call and will only return on a
              // successful connection or an exception
              mmSocket.connect();
          } catch (IOException e) {
              e.printStackTrace();
              connectionFailed();
              // Close the socket
              try {
                  mmSocket.close();
              } catch (IOException e2) {
                  e2.printStackTrace();
              }
              // Start the service over to restart listening mode
              BluetoothChatService.this.start();
              return;
          }
          // Reset the ConnectThread because we're done
          synchronized (BluetoothChatService.this) {
              mConnectThread = null;
          }
          // Start the connected thread
          connected(mmSocket, mmDevice);
      }
  
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>