title: Failed to connect to Bluetooth Device HC-08: "java.io.IOException: read failed, socket might closed or timeout, read ret: -1"
body: <p>I am facing an issue while connecting to a Bluetooth device (HC-08), for my Arduino project.
  I have looked into many solutions, but none of them worked for me,</p>
  <p>this the UUID I have used for the connection.</p>
  <pre><code>    private val myUUID: UUID = UUID.fromString(&quot;0000FFF1-0000-1000-8000-00805F9B34FB&quot;)
  </code></pre>
  <p>this is the method where the Bluetooth socket is being connected</p>
  <pre><code>    override fun onItemClick(parent: AdapterView&lt;*&gt;?, view: View?, position: Int, id: Long) {
      bluetoothAdapter.cancelDiscovery()
      val address = btDevices[position].address
      val remoteDevice = bluetoothAdapter.getRemoteDevice(address)
      bluetoothSocket = remoteDevice.createInsecureRfcommSocketToServiceRecord(myUUID)
      try {
          bluetoothSocket.connect()
      } catch (socketException: IOException){
          toastMessageLong(&quot;Could not close connection:$socketException&quot;)
          Log.i(&quot;Socket Exception&quot;, &quot;Could not close connection:$socketException&quot;)
          try {
              bluetoothSocket.close()
          } catch (closeException: Exception) {
              toastMessageShort(&quot;Could not close connection:$closeException&quot;)
          }
          return
      }
  }
  </code></pre>
  <p>code is too long to put here, so here are the related snippets</p>
  <pre><code>    arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, bluetoothDevices)
      listView.adapter = arrayAdapter
      listView.onItemClickListener = this
  
      val intentFilter = IntentFilter()
      intentFilter.addAction(BluetoothDevice.ACTION_FOUND)
      intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
      intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED)
      intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
      registerReceiver(broadcastReceiver, intentFilter)
  
      val pairIntentFilter = IntentFilter()
      pairIntentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
      registerReceiver(pairBroadcastReceiver, pairIntentFilter)
  </code></pre>
  <p>Here are the <code>BroadcastReceivers</code> :</p>
  <pre><code>    private val broadcastReceiver = object : BroadcastReceiver() {
      override fun onReceive(context: Context?, intent: Intent?) {
          val action = intent?.action
          if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) {
              enableUserInteraction()
              disableProgressView()
              if (bluetoothDevices.size != 0)
                  arrayAdapter.notifyDataSetChanged()
              else
                  toastMessageShort(&quot;No Device Found&quot;)
          } else if (BluetoothDevice.ACTION_FOUND == action) {
              val device: BluetoothDevice? = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
              if (device != null) {
                  val name = device.name
                  val address = device.address
                  if (!addressList.contains(address)) {
                      addressList.add(address)
                      btDevices.add(device)
                      if (name == &quot;&quot; || name == null)
                          bluetoothDevices.add(address)
                      else
                          bluetoothDevices.add(name)
                  }
              }
          }
      }
  }
  </code></pre>
  <p><code>pairBroadcastReceiver</code> :</p>
  <pre><code>    private val pairBroadcastReceiver = object : BroadcastReceiver() {
      override fun onReceive(context: Context?, intent: Intent?) {
          val action = intent?.action
          if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
              val device: BluetoothDevice? =
                      intent?.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
              if (device != null) {
                  if (device.bondState == BluetoothDevice.BOND_BONDED) {
                      toastMessageShort(&quot;Connected successfully&quot;)
                      listView.visibility = View.GONE
                      btnShowAvailableDevices.visibility = View.GONE
                      btnDisconnect.visibility = View.VISIBLE
                  }
                  if (device.bondState == BluetoothDevice.BOND_BONDING) {
                      toastMessageShort(&quot;Connecting...&quot;)
                  }
                  if (device.bondState == BluetoothDevice.BOND_NONE) {
                      toastMessageShort(&quot;Disconnected&quot;)
                  }
              }
          }
      }
  }
  </code></pre>
  <p><code>onDestroy</code> method</p>
  <pre><code>    override fun onDestroy() {
      super.onDestroy()
      unregisterReceiver(broadcastReceiver)
      unregisterReceiver(pairBroadcastReceiver)
  }
  </code></pre>