Skip to content
Related Articles

Related Articles

Connecting To Bluetooth Devices Via CLI

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Jul, 2022
View Discussion
Improve Article
Save Article

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:

  • Complete modular implementation
  • Real hardware abstraction
  • Multiple Bluetooth devices support
  • Symmetric multi-processing safe
  • Standard socket interface to all layers
     

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
  • sudo allows the user to run commands with the privileges of a superuser.
  • usermod command (Modify User) is used to change the properties of a user.
  • In the -aG flag, -a appends the user to the mentioned groups, and -G gives a list of supplementary groups of which the user is also a member.
  • The usermod -G command replaces the groups that were there before unless you add the -a (for append) flag.
  • $USER returns the current username.

 

Enabling Bluetooth Service

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

$ sudo systemctl enable --now bluetooth.service
  • sudo allows the user to run commands with the privileges of a superuser.
  • enable command enables the mentioned service at boot.
  • — now flag starts the mentioned service right away.

 

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!