Toggling Your Bluetooth Connection using bluetoothctl on the Command-Line

Featured image for sharing metadata for article

Since getting a pair of Bluetooth headphones, I've been thoroughly enjoying being able to be hands-free.

However, one thing that's annoyed me is that to switch between my desktop (where I play my music on) and my work laptop (for Zoom calls), which requires either clicking around in UIs, or running a couple of invocations of command-line, none of which is useful if I need to quickly switch between them.

Unfortunately within bluetoothctl, there's no way to determine if the controller is connected to any device, but given we know the MAC address for the device, we can use the info subcommand and determine if it's connected:

#!/usr/bin/env bash
device="38:18:4C:BF:46:FE"

if bluetoothctl info "$device" | grep 'Connected: yes' -q; then
  bluetoothctl disconnect "$device"
else
  bluetoothctl connect "$device"
fi

This will now toggle the connection of the device.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #command-line #bluetoothctl #bluetooth.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.