{"id":1327,"date":"2021-12-05T21:45:38","date_gmt":"2021-12-05T16:15:38","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1327"},"modified":"2023-03-25T00:50:06","modified_gmt":"2023-03-24T19:20:06","slug":"bluetoothctl-management-of-bluetooth-devices-in-linux","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/bluetoothctl-management-of-bluetooth-devices-in-linux\/","title":{"rendered":"Bluetoothctl: Management of Bluetooth Devices in Linux"},"content":{"rendered":"\n
This article is about the Management of Bluetooth devices in Linux using a command-line tool called bluetoothctl. In this, I will talk about how to connect with a Bluetooth device<\/strong> in Linux. I have also given a little hint in the end about how to automate this whole scanning, connecting, and other processes just with a script<\/strong>.<\/p>\n\n\n\n Alternatively, we can use Graphical Tools like GNOME Bluetooth, Bluedevil, Blueman<\/strong>. But like all other Graphical Tools, they are bloated, they lack the ability of scripting, and they cannot be used without X11 or Wayland. These restrictions are mitigated by using a command-line utility called Manjaro and Archlinux:<\/p>\n\n\n\n Debian and Ubuntu:<\/p>\n\n\n\n Fedora:<\/p>\n\n\n\n Now, enable<\/strong> the bluetooth.service<\/strong> using following systemd<\/strong>‘s command <\/p>\n\n\n\n The above is to enable the Bluetooth service in the background. But it requires restarting your <\/strong>system. If you do not want to restart, execute the following command as well:<\/p>\n\n\n\n Note<\/strong>: Make sure that kernel module btusb <\/strong>is loaded into the system. Kernel modules are equivalent to drivers in Windows OS. If it is not loaded, load it using<\/p>\n\n\n\n Now, I will be describing how to connect to a Bluetooth device using my smartphone as an example in the following steps.<\/p>\n\n\n\n Like nmtui, bluetoothctl’s interactive mode <\/strong>is easier to work with during the first-time connection. And like nmcli, the non-interactive<\/strong> mode is good for scripting.<\/p>\n\n\n\n To get into interactive mode, use<\/p>\n\n\n\n Output<\/strong> may look like this:<\/p>\n\n\n\n Notice, how the prompt is changing from After following all the steps, you can quit using<\/p>\n\n\n\n To turn on the Bluetooth, use power on <\/strong>command:<\/p>\n\n\n\n In my system, I get the following output:<\/p>\n\n\n\n Similarly, to turn it off, you can use the First list available devices using<\/p>\n\n\n\n Output:<\/p>\n\n\n\n If you cannot find your device in the output, use the following command:<\/p>\n\n\n\n Output on my system:<\/p>\n\n\n\n As you turn on your Bluetooth device (in the above example, my new smartphone), bluetoothctl will list it in the output. After you have found your device, copy its address for future use.<\/p>\n\n\n\n If you want your Linux computer to be discoverable by other devices, use the command given below. But for my tutorial, it is not necessary.<\/p>\n\n\n\n We can do it in two ways: Turn on the pairing in your Bluetooth device (ex – smartphone). Then, Turn on the pairing in your Linux computer using<\/p>\n\n\n\n Now connect using<\/p>\n\n\n\n Now, you might get a prompt to accept the connection. Just say yes.<\/p>\n\n\n\n First, you need to trust your device using the following :<\/p>\n\n\n\n Output:<\/p>\n\n\n\n Now, bluetoothctl saves your device’s address on your computer. In the future, you will not be needing to trust the device anymore. This saving is preserved even over system-restart. Now, you can connect with the device using:<\/p>\n\n\n\n Now, you might get a prompt to accept the connection. Just say yes. It might even ask you to match passkey\/pin. For my smartphone, the output is<\/p>\n\n\n\nbluetoothctl<\/a><\/strong><\/code>.<\/p>\n\n\n\n
Table of Contents<\/h2>\n\n\n\n
\n
\n
Installation and Early Preparation<\/h2>\n\n\n\n
~$ sudo pacman -S bluez bluez-utils <\/code><\/pre>\n\n\n\n
~$ sudo apt install bluez*<\/code><\/pre>\n\n\n\n
~$ sudo dnf install bluez bluez-tools<\/code><\/pre>\n\n\n\n
~$ sudo systemctl enable bluetooth.service<\/code><\/pre>\n\n\n\n
~$ sudo systemctl start bluetooth.service<\/code><\/pre>\n\n\n\n
~$ modprobe btusb<\/code><\/pre>\n\n\n\n
Step 1: Get into Bluetoothctl’s Interactive Mode<\/h2>\n\n\n\n
~$ bluetoothctl<\/code><\/pre>\n\n\n\n
Agent registered\n[CHG] Controller F8:89:D6:C8:2E:54 Pairable: yes\n[bluetooth]# <\/code><\/pre>\n\n\n\n
~$<\/code><\/strong> to
[bluetooth]#<\/code><\/strong>. Now, you can execute any command like
help<\/code><\/strong> after the
#<\/code> symbol. Use
help<\/code> <\/strong>to find all existing commands and their one-liner explanations.<\/p>\n\n\n\n
[bluetooth]# help<\/code><\/pre>\n\n\n\n
[bluetooth]# quit<\/code><\/pre>\n\n\n\n
Step 2: Turn on Bluetooth in Linux<\/h2>\n\n\n\n
[bluetooth]# power on<\/code><\/pre>\n\n\n\n
[CHG] Controller F8:89:D2:C8:2E:54 Class: 0x006c010c\nChanging power on succeeded<\/code><\/pre>\n\n\n\n
power off<\/code><\/strong> command.<\/p>\n\n\n\n
Step 3: Scan for Available Bluetooth Devices<\/h2>\n\n\n\n
[bluetooth]# devices<\/code><\/pre>\n\n\n\n
[bluetooth]# devices\nDevice 40:45:DA:B8:AB:BB 34SUPER\nDevice 04:C8:07:12:D0:2D LG Q3<\/code><\/pre>\n\n\n\n
[bluetooth]# Scan on<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
[bluetooth]# discoverable on<\/code><\/pre>\n\n\n\n
Step 4: Connect to Your Bluetooth Device<\/h2>\n\n\n\n
1. Pair<\/strong> and then Connect<\/strong>.
2. Trust<\/strong> and then Connect<\/strong>. I find it simpler, less confusing and it works for me with all kinds of devices. Hence, I will be describing this in detail.<\/p>\n\n\n\n1. Pair and Connect<\/h3>\n\n\n\n
[bluetooth]# pair 90:78:B2:C7:8F:A8<\/code><\/pre>\n\n\n\n
[bluetooth]# connect 90:78:B2:C7:8F:A8<\/code><\/pre>\n\n\n\n
2. Trust and Connect<\/h3>\n\n\n\n
[bluetooth]# trust 90:78:B2:C7:8F:A8<\/code><\/pre>\n\n\n\n
[CHG] Device 90:78:B2:C7:8F:A8 Trusted: yes\nChanging 90:78:B2:C7:8F:A8 trust succeeded<\/code><\/pre>\n\n\n\n
[bluetooth]# connect 90:78:B2:C7:8F:A8<\/code><\/pre>\n\n\n\n
Request confirmation\n[agent] Confirm passkey 989960 (yes\/no): yes\n[CHG] Device 90:78:B2:C7:8F:A8 Paired: yes\nConnection successful<\/code><\/pre>\n\n\n\n
Scripting using bluetoothctl in non-interactive mode<\/h2>\n\n\n\n