title: BluetoothSocket connect failed
body: <p>My demo is just determined to build a connection between two phones.But when my client tries to call <code>bluetoothSocket.connect()</code> it throws an IOException with the message <code>no route to host</code>.I have tried many approaches but it doesn't work.</p>
  
  <p>Here is my code about AcceptThread and ConnectThread (deleted some unrelated code to make it more concise)</p>
  
  <pre><code>class AcceptThread extends Thread {
  
      private final BluetoothServerSocket mmServerSocket;
      BluetoothServerSocket tmp;
  
      public AcceptThread() {
  
          Method listenMethod = null;
          try {
              listenMethod = bluetoothAdapter.getClass().getMethod("listenUsingRfcommOn",new Class[]{int.class});
          } 
          try {
              tmp = ( BluetoothServerSocket) listenMethod.invoke(bluetoothAdapter, new Object[]{30});
          } 
          mmServerSocket = tmp;
      }
  
      public void run() {
          BluetoothSocket socket = null;
          while (isAcceptRun) {
              try {
                  socket = mmServerSocket.accept();
              } catch (IOException e) {
                  Log.i(TAG, "run: AB "+e);
                  break;
              }
              if (socket != null) {
                  try {
                      mmServerSocket.close();
                  }
                  break;
              }
          }
      }
  }
  
  class ConnectThread extends Thread {
  
      private final BluetoothSocket mmSocket;
      private final BluetoothDevice mmDevice;
  
      public ConnectThread(BluetoothDevice device) {
  
          BluetoothSocket tmp = null;
          mmDevice = device;
          Method m = null;
          try {
              m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
          } 
          try {
              tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(30));
          } 
          mmSocket = tmp;
      }
  
      public void run() {
          // Cancel discovery because it will slow down the connection
          bluetoothAdapter.cancelDiscovery();
  
          try {
              mmSocket.connect();
          } catch (IOException connectException) {
              try {
                  mmSocket.close();
              } catch (IOException closeException) {
              }
              return;
          }
      }
  
      /** Will cancel an in-progress connection, and close the socket */
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) { }
      }
  }
  </code></pre>