0

I have successfully recognised the bluetooth device (HC-05), I got its name, MAC address, and bond state, but when I'm trying to create a client socket in order to initiate a bluetooth connection with it I got a null socket.

//initializing bluetooth adapter
    val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
    if (bluetoothAdapter?.isEnabled == false) {
        val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
    }

    //creating a list of PAIRED DEVICES
    val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices

    //define a BluetoothDevice (the first on the list)
    pairedDevices?.forEach { device ->
        val deviceName = device.name
        val deviceHardwareAddress = device.address // MAC address
        val arduino: BluetoothDevice = pairedDevices.first()

Then:

val arduinoSocket = arduino?.createRfcommSocketToServiceRecord(uuid)

which is Null.

My UUID is:

private val uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")

Any insights?

BrainTrance
  • 79
  • 11

1 Answers1

1

Your code seems fine for the most part. Instead of using the default uuid, you can try using the code below to try and get the device's actual uuid.

BluetoothSocket arduinoSocket = arduino.createRfcommSocketToServiceRecord(arduino.getUuids()[0].getUuid());