android – BluetoothDevice.setPin not connecting

I cannot seem to get my app to connect to a Bluetooth device and set the PIN programatically.

I’ve tried without aborting the broadcast and it didn’t help.
If I don’t implement the BroadcastReceiver, it prompts the user to enter the PIN and it pairs successfully. But if I try setting the PIN directly, it never works. And I’ve even tried specifying the byteArray charset to UTF-8 with no effect. I’ve even tried manually setting the byteArray and still nothing.

What am I doing wrong?

class MainActivity: AppCompatActivity() {

    private var broadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(p0: Context?, p1: Intent?) {
            var device: BluetoothDevice? = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
           device?.setPin("1234".toByteArray())
            this.abortBroadcast()
        }}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.devicelist)
        ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),1)
        registerReceiver(broadcastReceiver, IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))

        findViewById<Button>(R.id.button3).setOnClickListener{
        var ble = BluetoothAdapter.getDefaultAdapter()
        var dev = ble.getRemoteDevice("00:20:04:32:8A:EE")
        dev.createBond()
        }

}}