Bluetooth keyboard pairing does not ask PIN code on Ubuntu 20.04, how to fix

I just had this problem where I could not pair a Microsoft Designer Keyboard on Ubuntu 20.04 because I was supposed to enter a PIN / Passkey code but it was never displayed to me by Ubuntu.

To quickly check if that's your case too, when you try to pair the keyboard does it hangs for a couple seconds before failing ? And when you press ENTER right after initiating pairing, does it fail right away ? If yes, thats probably because the pairing expect you to enter a PIN code (but you can't see it).

Step 1 : finding the device

I fixed that problem by using the command line interface tool named bluetoothctl.

Simply type bluetoothctl in a terminal and press enter, this will initiate interactive mode.

Then from there, type in scan on to start scanning for bluetooth device, once you see your keyboard, just copy its mac address (it should look like that : 2F:6B:12:49:2C:8E).

If you missed it, you can also list found devices with the devices command.

Then stop scanning with the scan off command.

Step 2 : bluetooth agent

Now, the part that fixed the problem for me was the bluetooth agent. A bluetooth agent is what is responsible for managing pairing code exchanges between devices.

To make sure an agent is active, type agent on AND also type the default-agent command. That's what fixed the problem for me ! For some reasons, the active agent was not handling my keyboard pairing pin...

Step 3 : pair the device

Now type pair 2F:6B:12:49:2C:8E (while replacing the MAC address with the one you copied earlier).

This should now print something like this :

[CHG] Device 2F:6B:12:49:2C:8E Connected: yes
[agent] Passkey: 123486
[CHG] Device 2F:6B:12:49:2C:8E Paired: yes

It basically tells you the passkey you are supposed to type on the bluetooth keyboard, finally ! Type it in and press ENTER.

At this point it should work !

You can also type trust 2F:6B:12:49:2C:8E (while replacing the MAC address again) to make sure the pairing persist after reboot.

After that, type exit to exit the interactive shell and you are good to go !