How to connect to a Bluetooth device from a Linux terminal

For those who spend the most time on the terminal, being able to connect to your Bluetooth device (such as speakers, transmitters, and any other electronic devices) from the terminal makes a lot of sense. In this article, we will discuss how to control a Bluetooth device from a Linux terminal (distributions based on Ubuntu, Debian, Fedora, and Arch).

Bluetooth wireless technology is a global specification for a small, low-cost radio solution that provides links between mobile computers, mobile phones, other portable handheld devices, and connectivity to the Internet. The specification was developed, published and promoted by the Bluetooth Special Interest Group (SIG).

In this guide, we will use Blue Z Provides support for core Bluetooth layers and protocols. It is flexible, efficient and implemented using modularity. It has the following main functions.

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

Install BlueZ on Linux

Install the BlueZ package using any of the following commands that match your Linux distribution.

----- Ubuntu / Debian -----
$ sudo apt-get -y install bluetooth bluez bluez-tools rfkill

----- Fedora -----
$ sudo dnf -y install bluez bluez-tools

----- Arch Linux / Manjaro -----
$ sudo pacman -S bluez bluez-utils 

This installation provides bluetoothctl utility. If you want to connect to a Bluetooth tether, you need to add your account to the lp group.

sudo usermod -aG lp $USER
newgrp lp

The Bluetooth device should be up and enabled.

$ systemctl is-enabled bluetooth.service 
enabled

$ systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
   Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-11-30 17:14:12 EAT; 3h 55min ago
     Docs: man:bluetoothd(8)
 Main PID: 1638 (bluetoothd)
    Tasks: 1 (limit: 18590)
   Memory: 2.3M
      CPU: 47ms
   CGroup: /system.slice/bluetooth.service
           └─1638 /usr/libexec/bluetooth/bluetoothd

Nov 30 17:14:12 myfed.example.net systemd[1]: Starting Bluetooth service...
Nov 30 17:14:12 myfed.example.net bluetoothd[1638]: Bluetooth daemon 5.52
Nov 30 17:14:12 myfed.example.net systemd[1]: Started Bluetooth service.
Nov 30 17:14:12 myfed.example.net bluetoothd[1638]: Starting SDP server
Nov 30 17:14:12 myfed.example.net bluetoothd[1638]: Bluetooth management interface 1.14 initialized
Nov 30 21:07:52 myfed.example.net bluetoothd[1638]: Endpoint registered: sender=:1.77 path=/MediaEndpoint/A2DPSink/sbc
Nov 30 21:07:52 myfed.example.net bluetoothd[1638]: Endpoint registered: sender=:1.77 path=/MediaEndpoint/A2DPSource/sbc

How to pair and connect to a Bluetooth device

We will use bluetoothctl CLI tp configure bluez. If you have an alternative front-end tool, such as GNOME Bluetooth, you can use it instead.

First, make sure Bluetooth is not disabled.

$ rfkill 
ID TYPE      DEVICE               SOFT          HARD
0 bluetooth tpacpi_bluetooth_sw   blocked       unblocked
1 wlan      phy0                  unblocked     unblocked

If it is blocked, use the following command to unblock it.

$ rfkill unblock bluetooth

Confirm that it is blocked.

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

Connect to a Bluetooth device using bluetoothctl

This is a general overview of pairing devices using bluetoothctl.

  • Start the bluetoothctl interactive command.
$ bluetoothctl
Agent registered

$ bluetoothctl show 
Controller 20:79:18:5E:4B:64 (public)
 Name: myfed.example.net
 Alias: myfed.example.net
 Class: 0x000c010c
 Powered: yes
 Discoverable: no
 DiscoverableTimeout: 0x00000000
 Pairable: no
 UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
 UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
 UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
 UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
 UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
 UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
 UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
 Modalias: usb:v1D6Bp0246d0534
 Discovering: no
Advertising Features:
 ActiveInstances: 0x00
 SupportedInstances: 0x05
 SupportedIncludes: tx-power
 SupportedIncludes: appearance
 SupportedIncludes: local-name

Entry help Get a list of available commands.

  • Turn on your Bluetooth adapter
[bluetooth]# agent KeyboardOnly
Agent is already registered

[bluetooth]# default-agent
Default agent request successful

[bluetooth]# power on
Changing power on succeeded
  • Scan to detect your Bluetooth device
# scan on
Discovery started
[CHG] Controller 20:79:18:5E:4B:64 Discovering: yes
[NEW] Device 14:AB:C5:CC:C7:AB 14-AB-C5-CC-C7-AB
[CHG] Device 14:AB:C5:CC:C7:AB TxPower: 8
[CHG] Device 14:AB:C5:CC:C7:AB Name: DESKTOP-N9GVMQ2
[CHG] Device 14:AB:C5:CC:C7:AB Alias: DESKTOP-N9GVMQ2
[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[NEW] Device 5C:FB:7C:A4:13:C6 JBL Clip 3
  • Pair with a discovered Bluetooth device
[bluetooth]# pair 5C:FB:7C:A4:13:C6
Attempting to pair with 5C:FB:7C:A4:13:C6
[CHG] Device 5C:FB:7C:A4:13:C6 Connected: yes
[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:FB:7C:A4:13:C6 ServicesResolved: yes
[CHG] Device 5C:FB:7C:A4:13:C6 Paired: yes
Pairing successful

# trust 5C:FB:7C:A4:13:C6
[CHG] Device 5C:FB:7C:A4:13:C6 Trusted: yes
Changing 5C:FB:7C:A4:13:C6 trust succeeded

# paired-devices
Device 5C:FB:7C:A4:13:C6 JBL Clip 3

# devices
Device 5C:FB:7C:A4:13:C6 JBL Clip 3
  • Connect to device after pairing
[bluetooth]# connect 5C:FB:7C:A4:13:C6
Attempting to connect to 5C:FB:7C:A4:13:C6
[CHG] Device 5C:FB:7C:A4:13:C6 Connected: yes
Connection successful

[JBL Clip 3]# info
Device 5C:FB:7C:A4:13:C6 (public)
 Name: JBL Clip 3
 Alias: JBL Clip 3
 Class: 0x00200414
 Icon: audio-card
 Paired: yes
 Trusted: yes
 Blocked: no
 Connected: yes
 LegacyPairing: no
 UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
 UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
 UUID: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
 ManufacturerData Key: 0x0057
 ManufacturerData Value:
  01 00 cb 0e d0 1e 03 01                          ........        
 RSSI: -21
 TxPower: 0

Turn on the Bluetooth adapter when turning on the power

To power on the Bluetooth adapter at system startup, open the configuration file.

$ sudo vim /etc/bluetooth/main.conf

Add line AutoEnable=true

[Policy]
AutoEnable=true

reference:

  • Arch Linux Bluetooth configuration
  • Blue page

More tips:

Install Windows Terminal on Windows 10 / Windows Server

How to do a Google search from a Linux terminal

The best console / terminal file manager for Linux

10 best terminal emulators for Linux

How to share a Linux terminal session in a web browser

Sidebar