0

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,

this the UUID I have used for the connection.

    private val myUUID: UUID = UUID.fromString("0000FFF1-0000-1000-8000-00805F9B34FB")

this is the method where the Bluetooth socket is being connected

    override fun onItemClick(parent: AdapterView<*>?, 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("Could not close connection:$socketException")
        Log.i("Socket Exception", "Could not close connection:$socketException")
        try {
            bluetoothSocket.close()
        } catch (closeException: Exception) {
            toastMessageShort("Could not close connection:$closeException")
        }
        return
    }
}

code is too long to put here, so here are the related snippets

    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)

Here are the BroadcastReceivers :

    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("No Device Found")
        } 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 == "" || name == null)
                        bluetoothDevices.add(address)
                    else
                        bluetoothDevices.add(name)
                }
            }
        }
    }
}

pairBroadcastReceiver :

    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("Connected successfully")
                    listView.visibility = View.GONE
                    btnShowAvailableDevices.visibility = View.GONE
                    btnDisconnect.visibility = View.VISIBLE
                }
                if (device.bondState == BluetoothDevice.BOND_BONDING) {
                    toastMessageShort("Connecting...")
                }
                if (device.bondState == BluetoothDevice.BOND_NONE) {
                    toastMessageShort("Disconnected")
                }
            }
        }
    }
}

onDestroy method

    override fun onDestroy() {
    super.onDestroy()
    unregisterReceiver(broadcastReceiver)
    unregisterReceiver(pairBroadcastReceiver)
}
Himanshu Bansal
  • 139
  • 1
  • 5
  • The HC-08 is a Bluetooth Low Energy (BLE) device and not a Bluetooth Classic device. There is some more information at: https://stackoverflow.com/a/62938210/7721752 – ukBaz Dec 26 '20 at 23:36
  • @ukBaz I know this is different than Bluetooth classic. I can read and write data with a Bluetooth Terminal App - https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal , But i want to develop my own app to connect to a BLE device. It would be greatful, if you could help me with the same. – Himanshu Bansal Dec 27 '20 at 13:05
  • https://github.com/kai-morich/SimpleBluetoothLeTerminal this is the source code of that app, but I am not enabled to figure it out. how to get help from it. – Himanshu Bansal Dec 27 '20 at 13:35
  • 1
    You are using the [RFComm API](https://developer.android.com/guide/topics/connectivity/bluetooth.html) rather than the [BLE API](https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html). The source you linked to seems very good. Your question is too broad to be able to answer it. You need to post more specific questions. – ukBaz Dec 27 '20 at 16:05
  • @ukBaz Ok, I will try to figure this out. – Himanshu Bansal Dec 27 '20 at 18:19

0 Answers0