How to Manage Bluetooth in Ubuntu via CLI?

Managing Bluetooth devices from the command line in Ubuntu is a powerful way to troubleshoot connectivity issues, automate pairings, or manage headless server environments. This article provides a comprehensive guide to using bluetoothctl, the built-in interactive utility for controlling Bluetooth on Linux. You will learn how to turn on your Bluetooth controller, scan for nearby devices, trust and pair with them, and connect or disconnect devices seamlessly using simple terminal commands.

Getting Started with bluetoothctl

Ubuntu includes a built-in command-line tool called bluetoothctl which communicates directly with the BlueZ Bluetooth protocol stack. To start managing your devices, open your terminal and launch the interactive shell by typing:

bluetoothctl

Once inside, your terminal prompt will change to [bluetooth]#. This indicates you are ready to issue Bluetooth commands. To exit this interactive mode at any time, simply type exit.

Powering On and Scanning for Devices

Before you can connect to a device, you need to ensure your local Bluetooth adapter is turned on and visible.

  1. Turn on the Bluetooth controller:
power on
  1. Enable the agent: The agent manages the pairing process, including PIN codes and passkeys.
agent on
default-agent
  1. Start scanning: Turn on discovery mode to find nearby Bluetooth devices.
scan on

As the scan runs, you will see a list of discovered devices scrolling in your terminal. Each device will display its unique MAC address (formatted like AA:BB:CC:DD:EE:FF) and its name. Note down the MAC address of the device you want to connect to.

Pairing and Connecting a Device

Once you have identified the MAC address of your target device, you can proceed with the pairing process. Replace XX:XX:XX:XX:XX:XX with your actual device’s MAC address in the following steps:

  1. Pair the device: This establishes a secure bond.
pair XX:XX:XX:XX:XX:XX

Note: If prompted for a PIN code on your terminal or your device, enter it to complete the pairing. 2. Trust the device: Trusting a device allows it to reconnect automatically in the future without asking for permission again.

trust XX:XX:XX:XX:XX:XX
  1. Connect to the device: Finalize the connection to start using your device.
connect XX:XX:XX:XX:XX:XX

Disconnecting and Removing Devices

If you need to sever a connection or completely clear a device from your Ubuntu system’s memory, you can do so with these straightforward commands:

disconnect XX:XX:XX:XX:XX:XX
remove XX:XX:XX:XX:XX:XX
power off