Open In App

Connecting To Bluetooth Devices Via CLI

The Bluetooth wireless technology is a worldwide specification for a small-form-factor, low-cost radio solution that provides links between mobile computers, mobile phones, and other portable handheld devices. You’ll find Bluetooth connectivity in smartphones, smartwatches, wireless earphones, etc. 
In this article, we’ll be learning how to connect to a Bluetooth Device via CLI on Linux. 

What Is BlueZ?

BlueZ is the official Bluetooth protocol stack for Linux distributed under GNU General Public License (GPL) that provides support for the core Bluetooth layers and protocols. It is flexible, efficient, and uses a modular implementation. It has many interesting features:



BlueZ Installation

Depending on your distro type, run one of the below commands to install BlueZ and other related tools. 

$ sudo apt -y install bluetooth bluez bluez-tools rfkill # for Debian/Ubuntu based distros



$ sudo pacman -S bluez bluez-utils util-linux # for Arch based distros

$ sudo dnf -y install bluez bluez-tools # for Fedora/CentOS 

 all tools already installed, you may be asked for some confirmation. 

Adding User to Required Group

By default, the Bluetooth daemon will only give out Bluetooth devices to users that are a member of the lp group. We need to make sure to add our current user to the lp group to connect to a Bluetooth tether. 

Firstly, we check the allocated groups to the user.
id prints information about the given user, or the process running it if no user is specified.

$ id   

 

If lp is not present in the output list, we need to add the current user to the lp group

$ sudo usermod -aG lp $USER
$ newgrp lp
$ id       # to check newly added group

 

Enabling Bluetooth Service

To enable any service at start-up, we execute the systemctl enable command. 

$ sudo systemctl enable --now bluetooth.service

 

Now we can check the status of the service using the systemctl status command. It returns the activity status of the mentioned service.

$ sudo systemctl status bluetooth.service

 

Sometimes, the Bluetooth service may be blocked and to unblock it we need to execute the following command.

rfkill is a tool for enabling and disabling wireless devices

$ rfkill

 

In case it is soft-blocked, execute the following command to unblock

$ rfkill unblock bluetooth

You can run the rfkill command to check the status of Bluetooth.

 

Triggering the utility tool

We will be using a utility tool called bluetoothctl that allows various tasks like pairing and connecting a Bluetooth device. 
To start this utility, just type bluetoothctl in CLI.

$ bluetoothctl

 

Scan for nearby devices

Before connecting to any device, we need to scan for all nearby available devices using the command below.
scan on command will actively scan all discoverable Bluetooth devices around the user.

[bluetooth]# scan on

The output will be similar to the image below

 

Pairing a device

In order to form a connection, a Bluetooth device needs to be paired with another Bluetooth device. 
Enter pair [ID] where [ID]  is the unique Bluetooth device identifier shown in the above command. 

[bluetooth]# pair [ID]

For example, if we want to connect to the first device in the above screenshot, the command will be

[bluetooth]# pair D4:8A:39:3E:3C:F5

 

 

Thus we have successfully paired and connected to a Bluetooth device on Linux via CLI. Some of the trivial commands you may need are listed below.

Command Used to 
list  List available controllers
devices List available devices
paired-devices  List paired devices
cancel-pairing [dev]      Cancel pairing with the device
discoverable <on/off>  Set controller discoverable mode
disconnect [dev]    Disconnect device
exit   Quit program
Article Tags :