94

I'm using Mint 15 w/ Cinnamon.

I bought a set of bluetooth speakers and I'm trying to connect to them via terminal. Via the GUI I can see them normally and I am connected to them. I want to make a small script so every time they are visible I would connect to them automatically.

I am trying to scan them with:

hcitool scan

But I get

Scanning...

and after a few seconds the process dies.

The same thing with hidd --search.

If I run hciconfig scan I get:

hci0:   Type: BR/EDR  Bus: USB
    BD Address: 40:2C:F4:78:E8:69  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING PSCAN ISCAN 
    RX bytes:130700 acl:22 sco:0 events:18527 errors:0
    TX bytes:31875398 acl:36784 sco:0 commands:75 errors:0

I suppose that is just saying my bluetooth address and that it is turned on.

As I said already, via the normal User Interface, I can see the speakers and I am connected to them, but through terminal I get nothing.

Actually it is quite funny that hcitool scan isn't finding anything since my speakers are connected and every time I run the command the sound from the speakers breaks for a couple of seconds.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Zippie
  • 1,879

8 Answers8

70

I managed to do so via bluez-tools:

sudo apt-get install bluez-tools

List of devices to get the MAC address of my device:

bt-device -l

and successfully connect to it:

bt-device -c 01:02:03:04:05:06
Toby Speight
  • 8,678
Zippie
  • 1,879
  • 1
    For update follow the issue https://github.com/khvzak/bluez-tools/issues/13 – Édouard Lopez Oct 03 '16 at 17:09
  • On Linux Mint 17 (Cinnamon) I successfully used bluez-test-audio connect MAC_ADDRESS for an already paired audio device. – Suzana Jan 28 '17 at 01:23
  • 9
    I successfully used bt-device -c mac_address on Debian. 9. – jbrock Jan 22 '19 at 20:18
  • 6
    On Ubuntu 18.04, bt-audio is not available. Judging from bluez-audio issue #13, this has been the case for years. Running bt-device -c <device-mac> doesn't help ("Error: GDBus.Error:org.bluez.Error.AlreadyExists: Already Exists") and I'm reluctant to go into hacks suggested under issue #13 because I have to do them on each machine, I expect they'll be deprecated easily etc. Can it really be that (after navigating to the right part of the settings GUI) it's a single click on GUI to redirect sound to a speaker, but that there's still no way to do it in a single, simple command from the CLI? – Tomislav Nakic-Alfirevic May 04 '20 at 10:14
  • 2
    @TomislavNakic-Alfirevic There is using bluetoothctl... as Aldrin Jenson mentioned in his comment below. There are a lot of online tutorials about connecting to bluetooth via CLI mentioning bluetoothctl, but most (if not all) only talk about using it in interactive mode. Yet, it seems all the commands available via bluetoothctl's interactive mode can be run non-interactively, as well. You could bundle up bluetoothctl commands into scripts that could make device management even easier. – nckbrzg Jun 18 '21 at 04:20
55

To do it with the built-in utils, you can follow this slightly more manual process using bluetoothctl.

hcitool scan  # to get the MAC address of your device
bluetoothctl
agent on
scan on  # wait for your device's address to show up here
scan off
trust MAC_ADDRESS
pair MAC_ADDRRESS
connect MAC_ADDRESS

The posts in this Github issue suggest a way to script it, but that did not work for me since I needed to manually wait for the scan to yield results.

Max
  • 651
  • this is the only way I made it work. Thanks! – Boson Bear Jul 15 '20 at 18:36
  • Yep, this worked like a charm. Now I can connect to my active speaker with Super+G – koem Nov 18 '20 at 21:47
  • I get a never-ending list when I start bluetoothctl (scan off does nothing), but I was able to connect using separate bluetooth commands: bluetoothctl trust MAC_ADDRESS, bluetoothctl pair MAC_ADDRESS etc... – ropeladder Dec 12 '20 at 15:42
  • On Xubuntu 22.04 this says my device is not available for me. It finds (but does not connect to) some devices, but does not find my Jelly Comb keyboard (whose MAC addresses I know). – Brōtsyorfuzthrāx Sep 30 '22 at 22:19
  • I'm kinda confused from your description: why would you need hcitool scan if scan on will give you the address anyway. hcitool is also deprecated and no longer included in bluez-tools package at least on Arch. – Hi-Angel Nov 13 '22 at 12:01
  • @Hi-Angel at least for me, scan on sometimes returns the mac addresses without the device names, while hcitool scan gave the complete list. You're right that it shouldn't theoretically be needed and scan on should suffice. – Max Nov 14 '22 at 02:56
11

I did to sudo apt-get install bluez-tools, because I did use it at some point.

After being able to find the device from hcitool scan, I ran bluez-simple-agent hci0 X where X is the mac address of the device I was adding. This created the connection to the device.

Then I ran hciconfig scan and it seems to have automatically started the connection.

user62474
  • 111
10

Adding on to Max's answer, to make your life easier, you just need to find the MAC address of your device once by using

bluetoothctl devices

Use the following command to connect to the bluetooth device.

bluetoothctl connect <The Mac Address of your device>

You can create an alias for this in your shell or even create a keyboard short cut in your Desktop environment.

This is not automatic, you manually have to trigger the command, but this is less overhead than continuously scanning in a script and it doesn't introduce an un-neccesary delay after powering up your bluetooth device, before it becomes discovered. You just trigger the script either in a terminal or via the keyboard shortcut.

X Tian
  • 10,463
2

I had a similar situation with some headphones I own that I constantly swap between my computer and phone. I created this bash script and placed it in a folder on my path so I can connect / disconnect from these headphones via the command line (and via a launcher I built). Here is the script that uses bluetoothctl to connect / disconnect. I used the MAC address in the question:

#!/bin/bash
MAC="${1:-40:2C:F4:78:E8:69}"
DEVICE=$(bt-device -l | grep $MAC  | cut -d  " " -f1-2)

if [[ $(bt-device -i $MAC | awk '/Connected/ {print $2}') == 1 ]] then echo "Device was connected, now disconnecting $DEVICE" echo -e "disconnect $MAC" | bluetoothctl > /dev/null else echo "Device was disconnected, now connecting $DEVICE" echo -e "connect $MAC" | bluetoothctl > /dev/null fi

To execute this script successfully you will need to set your MAC address or provide it when running the script. You must also have bluez-tools installed.

To install bluez-tools using aptitude you run

sudo apt-get install bluez-tools

To discover the MAC address of the hardware, I used the following command

bt-device -l

Which lists all of the added Bluetooth devices. I got a good start on this by following the directions from @Zippie's answer - bt-audio appears to have broken since this question was answered. But bt-device -l seems like a good solution. None of this will work if the device has not been added to your bluetooth devices. I just used the blueman applet to set that up.

artu-hnrq
  • 297
nephiw
  • 123
  • 1
    It seems that getting device name is better with bluetoothctl devices | grep $MAC | cut -d " " -f3- for devices with spaces in name, like "KOSS Porta Pro Wireless" – smido Sep 14 '22 at 10:52
1

I know this is kinda late but I was playing with this command hcitool and I'd like to point out something that I noted which solves your issue of doing a scan and the process "just dying". The thing is the scan only works if the devices are unpaired. In your case, you say you can see the speakers are connected on the GUI which means they've already been paired...hence the scan returns nothing.

I'm working on how to pair/connect devices with the same tool although I can see you've been given other options. I'll update my answer as soon as I manage.

0

I have the following installed in Debian 9.

blueman/stable,now 2.0.4-1 amd64 [installed]
bluez/stable,stable,now 5.43-2+deb9u1 amd64 [installed]
bluez-obexd/stable,stable,now 5.43-2+deb9u1 amd64 [installed,automatic]
bluez-tools/stable,now 0.2.0~20140808-5+b2 amd64 [installed]
pulseaudio-module-bluetooth/stable,now 10.0-1+deb9u1 amd64 [installed]

I have already paired my Bluetooth headphones to a USB Bluetooth adapter on my desktop using the GUI application Bluetooth Manager. I have a keyboard shortcut that runs the following two commands: bt-device -c 00:02:5B:01:3C:3B; pacmd set-default-sink bluez_sink.00_02_5B_01_3C_3B.a2dp_sink

  1. Connect headphones to the Bluetooth USB adapter. (Use bt-device -l to get the MAC address.)
  2. Set Bluetooth audio as the default output. (Use pacmd list-sinks | awk '/index:/ {print $0}; /name:/ {print $0}; /device\.description/ {print $0}' to get the correct name.)
jbrock
  • 1,141
-1

This might be a bit late, but I had the exact same problem, with the same output from hciconfig.

However, sudo hcitool lescan did the job for me - maybe it can still help you!

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255