How to set up bluetooth (the hackerman way)

Last edited: 23rd February 2024

A very basic problem: I have some wireless headphones, which also can be optionally wired, however, I want to connect them via bluetooth instead. When using a desktop environment, this is not an obstacle, but I don't use one, and if you use a window manager like me, you have to resort to an external application, and when especially using a tiling window manager, it's gonna be a terminal application, and the best way of learning something rather complicated like a terminal application is by teaching it to others, so this is a very simple guide on how to set up bluetooth via the command line (or the spooky hackerman stuff).

First of all, installing the package. Install the bluez and maybe the bluez-utils package via the system's package manager. As for audio, this requires also to either install bluez-alsa for ALSA users, pulseaudio-bluetooth for PulseAudio users (sometimes this is not needed), or libspa-bluetooth (sometimes this is not needed) or pipewire-audio for PipeWire users.

Next, enable the bluetooth daemon, and add your local account to the bluetooth group (if needed; sometimes you don't have to be in this group)

# usermod -aG bluetooth USER

Sometimes, the Linux kernel soft blocks the bluetooth card, so make sure that the output of the command rfkill list does not display that bluetooth is soft blocked. If it does display that it is blocked, run the command

# rfkill unblock bluetooth

Now reboot your system

After rebooting, type in the command

$ bluetoothctl

This is just a front-end for bluez, which is invoked in a command line user interface shell, noticeable by the shell's prompt normally looking like this

[bluetoothctl]#

So now start scanning for devices with

[bluetoothctl]# scan on

This will display all the junk devices running bluetooth near you. What you see printed out is the MAC address and the device name. Pair with the device that you want to connect.

[bluetoothctl]# pair MAC_ADDRESS

Sadly, you can only connect via typing the MAC address. A quick way of doing that is by pressing a few characters of the MAC address and then pressing tab to autocomplete the MAC address.

IMPORTANT: Some bluetooth devices may also need to connect after pairing:

[bluetoothctl]# connect MAC_ADDRESS

Now the device should be connected. Because it is just a daemon that is running in the background, you can close the entire terminal window, and your bluetooth device will be still connected. You can also leave the terminal window open if you are a weirdo. If you do that, you probably don't need the scanning to still go on, so turn it off:

[bluetoothctl]# scan off

Another thing you might want to do is to automatically connect the device again later on, which requires you to trust the bluetooth device:

[bluetoothctl]# trust MAC_ADDRESS

When shutting down the bluetooth device and then power it on again, it'll automatically connect without manually reconnecting the device. This might not work well enough when not restarting the system.

As for shutting down the connected bluetooth device, type:

[DEVICE_NAME]# disconnect MAC_ADDRESS

If you also want to forget the device, type the command:

[bluetoothctl]# remove MAC_ADDRESS

Lastly, to never pair it again, type:

[bluetoothctl]# untrust MAC_ADDRESS

That's it! That was quite simple, wasn't it?