title: There was an error while establishing Bluetooth connection. Falling back
body: <p>I am getting following error whenever my device try to connected with device which is out of range or which is can't connect. if connection is establish then app working fine. </p>
  
  <p>First of all let me tell you ,this question is asked many time before in this site. I go through all that solution but none of that working for me.</p>
  
  <p>Error Occurs at when try to connect in my case at sock.connect. you will see that in my bluetooth manager. </p>
  
  <p>code of Bluetooth manager:</p>
  
  <pre><code>public class BluetoothManager {
  
  private static final String TAG = BluetoothManager.class.getName();
  
  private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  
  public static BluetoothSocket connect(BluetoothDevice dev) throws IOException {
      BluetoothSocket sock = null;
      BluetoothSocket sockFallback = null;
  
      Log.d(TAG, "Starting Bluetooth connection..");
      try {
          sock = dev.createRfcommSocketToServiceRecord(MY_UUID);
          sock.connect();
      } catch (Exception e1) {
          Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back..", e1);
          Class&lt;?&gt; clazz = sock.getRemoteDevice().getClass();
          Class&lt;?&gt;[] paramTypes = new Class&lt;?&gt;[]{Integer.TYPE};
          try {
              Method m = clazz.getMethod("createRfcommSocket", paramTypes);
              Object[] params = new Object[]{Integer.valueOf(1)};
              sockFallback = (BluetoothSocket) m.invoke(sock.getRemoteDevice(), params);
              sockFallback.connect();
              sock = sockFallback;
          } catch (Exception e) {
              Log.e(TAG, "Couldn't fallback while establishing Bluetooth connection.");
  
          }
      }
      return sock;
  }
  }
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>E/com.quad14.obdnewtry.io.BluetoothManager: There was an error while establishing Bluetooth connection. Falling back..
      java.io.IOException: read failed, socket might closed or timeout, read ret: -1
          at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:738)
          at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:750)
          at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:375)
          at com.quad14.obdnewtry.io.BluetoothManager.connect(BluetoothManager.java:24)
          at com.quad14.obdnewtry.io.ObdGatewayService.startObdConnection(ObdGatewayService.java:116)
          at com.quad14.obdnewtry.io.ObdGatewayService.startService(ObdGatewayService.java:89)
          at com.quad14.obdnewtry.activity.MainActivity$16.onServiceConnected(MainActivity.java:1043)
          at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1453)
          at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1481)
          at android.os.Handler.handleCallback(Handler.java:751)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6126)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>