• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps BluetoothDevice.createrfcommsockettoservicerecord is Null

BrainTrance

Lurker
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.

This is my code:

//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 i try:

val arduinoSocket = arduino?.createRfcommSocketToServiceRecord(uuid)

which is null.

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

Any insights? Thanks
 
Back
Top Bottom