title: Android bluetoothSocket connection error
body: <p>I am trying to connect to a HC-06 via Bluetooth from an android device. I managed to get it to work before using:</p>
  
  <pre><code>bluetoothSocket = (BluetoothSocket) bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(this.bluetoothDevice,1);
  </code></pre>
  
  <p>the problem is that it only works once. when I disconnect and close the input and output streams and then the socket, and then try to reconnect it gives me this error:</p>
  
  <pre><code>D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[55]}
  D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@2d5fa8b, channel: 1, state: INIT
  D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@2d5fa8b, channel: 1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@39f43768, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@184d7781mSocket: android.net.LocalSocket@222b6026 impl:android.net.LocalSocketImpl@727b467 fd:FileDescriptor[55]
  D/BluetoothSocket: Closing mSocket: android.net.LocalSocket@222b6026 impl:android.net.LocalSocketImpl@727b467 fd:FileDescriptor[55]
  W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:739)
  W/System.err:     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:716)
  W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:482)
  W/System.err:     at com.example.work.dmm.clientBluetoothConnection.run(clientBluetoothConnection.java:65)
  </code></pre>
  
  <p>I tried to search for the problem and found various posts saying that createRfcommSocketToServiceRecord() should be used instead. I tried it:</p>
  
  <pre><code>ParcelUuid[] uuids = bluetoothDevice.getUuids();
  UUID uuid = uuids[0].getUuid();
  bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
  </code></pre>
  
  <p>but this does not work at all. I am not sure where to go from here so any help is appreciated.</p>