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)
}