title: BluetoothSocket not connecting to target device
body: <p>I'm trying to connect my device to another one via Bluetooth, but when I select the device I want to connect with, I get an <code>IOException</code> saying </p>
  
  <blockquote>
    <p>read failed, socket might closed or timeout, read ret: -1 </p>
  </blockquote>
  
  <p>Just to illustrate how my app works, I have a <code>RecyclerView</code> populated with the devices my Bluetooth scan has found, then when I click an item the app is supposed to connect with that device.</p>
  
  <p>Below is my the code for my connection thread:</p>
  
  <pre><code>private val MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb")
  
  private lateinit var device: BluetoothDevice
  private lateinit var onDeviceActionListener: OnDeviceActionListener
  private lateinit var socket: BluetoothSocket
  
  fun init(device: BluetoothDevice,
           onDeviceActionListener: OnDeviceActionListener): ConnectionThread {
      this.device = device
      this.onDeviceActionListener = onDeviceActionListener
      try {
          socket = device.createRfcommSocketToServiceRecord(MY_UUID)
      } catch (e: IOException) {
          Log.e(TAG, "Error creating socket", e)
      }
      return this
  }
  
  override fun run() {
      try {
          socket.connect()
      } catch (openException: IOException) {
          Log.e(TAG, "Error opening connection. Trying to close...", openException)
          try {
              socket.close()
          } catch (closeException: IOException) {
              Log.e(TAG, "Error closing socket", closeException)
          }
          return
      }
      onDeviceActionListener.onDeviceConnect(device)
  }
  </code></pre>
  
  <p>My guess is there is something wrong with my UUID. I've tried some other values but still didn't work.</p>
  
  <p>Any help will be much appreciated.</p>