Connecting To Bluetooth Devices Via CLI

Linux is a CLI-driven OS. Hence, for hardcore terminal fans, being able to do a wide range of tasks via the terminal is extremely important. In this module, we’ll be learning how we can accomplish one such trivial task, i.e connecting to a Bluetooth device, via the Terminal.

What Is BlueZ?

Before we begin, we need need to install BlueZ, the official Linux Bluetooth protocol stack!

BlueZ provides support for the core Bluetooth layers and protocols. It is flexible, efficient and uses a modular implementation. It has many interesting features:

  • Complete modular implementation
  • Symmetric multi processing safe
  • Multi-threaded data processing
  • Support for multiple Bluetooth devices
  • Real hardware abstraction
  • Standard socket interface to all layers
  • Device and service level security support

Thus it is a necessary requirement if we plan on working with bluetooth!

Installing Bluez

On Debian/Ubuntu you can use the apt command:

$ sudo apt -y install bluetooth bluez bluez-tools rfkill

On Arch Based Distros you can use the pacman command:

$ sudo pacman -S bluez bluez-utils util-linux

On Fedora/CentOS you can use the dnf command:

$ sudo dnf -y install bluez bluez-tools

Adding User To The Required Group

Before we proceed, we need to make sure that our user is a part of the lp group. You can check the groups the current user is a part of with:

$ id
uid=1000(user) gid=1000(user) groups=1000(user),998(wheel)

To add the current user to the lp group, type the usermod command:

$ sudo usermod -aG lp $USER
$ newgrp lp

Checking now, we should see that we have been added to the lp group!

$ id
uid=1000(user) gid=991(lp) groups=991(lp),998(wheel),1000(user)

With this, we can now finally go ahead and connect to bluetooth.

Enabling Bluetooth Service

To enable bluetooth service at start-up, enable it by using:

$ sudo systemctl enable --now bluetooth.service

To check if the service was successfully activated or not, type in:

$ sudo systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
     Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: disabled)
     Active: active (running) since Fri 2021-04-30 09:11:50 IST; 2h 0min ago
       Docs: man:bluetoothd(8)
   Main PID: 443 (bluetoothd)
     Status: "Running"
      Tasks: 1 (limit: 4587)
     Memory: 2.9M
     CGroup: /system.slice/bluetooth.service
             └─443 /usr/lib/bluetooth/bluetoothd

Thus our bluetooth service is up and running! Next up, make sure that it is now blocked with:

$ rfkill
ID TYPE      DEVICE      SOFT      HARD
 0 wlan      phy0   unblocked unblocked
 2 bluetooth hci0   blocked   unblocked

Incase it is blocked, unblock the same with:

$ rfkill unblock bluetooth

Finally confirm that it has been unblocked with:

$ rfkill
ID TYPE      DEVICE      SOFT      HARD
 0 wlan      phy0   unblocked unblocked
 2 bluetooth hci0   unblocked unblocked

Now we are ready to move onto the next step.

Connecting To A Bluetooth Device

To connect to a device, we’ll be using a utility program called bluetoothctl which would allow us to do various things like discovering and pairing to devices! To start bluetoothctl simply type:

$ bluetoothctl

This should drop us into an interactive prompt which allows us to perform various tasks.

Bluetoothctl
Bluetoothctl

Powering On Your Controller

To power on your bluetooth controller, type the following into the interactive prompt :

[bluetooth]# power on

Scanning For Nearby Devices

Before connecting, we need to scan for nearby devices with :

[bluetooth]# scan on

This should give us a list of all nearby bluetooth devices along with their Mac IDs

Scanning For Devices
Scanning For Devices

Now, we’ll be connecting to a bluetooth headset!

Pairing Our Device

To pair the target device, type in:

[bluetooth]# pair [ID]
Pairing With Our Device
Pairing With Our Device

Once successfully paired, we can proceed to the next step.

Trusting Our Device [Optional]

You can also trust the device with:

[bluetooth]# trust [ID]
Trusting The Device
Trusting The Device

Connecting Our Device

Finally connect your device with:

[bluetooth]# connect [ID]

Note that this might fail at times, and you can keep retrying till you are successfully connected to your device.

Connecting To Our Device
Connecting To Our Device

Thus with this we have successfully connected to our device vi the terminal!

Conclusion

Thus we connected to our Bluetooth device via the CLI. This whole procedure can be automated by writing suitable scripts. This method can be used to connect to wireless headphones and other Bluetooth devices as well!