Last active
January 26, 2026 04:58
-
-
Save alvaro-jmp/0f1936c1f1d4786c72eefaa006812e5b to your computer and use it in GitHub Desktop.
Script related for connect a Bluetooth Headset/Speaker/Similar and posible fix to "Failed to connect: org.bluez.Error.Failed br-connection-aborted-by-local"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Script related for connect a Bluetooth Headset/Speaker/Similar and posible fix to "Failed to connect: org.bluez.Error.Failed br-connection-aborted-by-local", "Failed to connect: org.bluez.Error.Failed br-connection-timeout", "org.bluez.Error.Failed br-connection-profile-unavailable" | |
| # Related to the error, I observed that if I disconnected and reconnected the usb bluetooth dongle and redo the connection steps, it was solved. To simulate disconnected and reconnected the usb, i used the file unbind and bind related to the usb port | |
| # Currently work if the usb bluetooth device are not the same, i.e. that are not two or more exact usb bluetooth dongle model | |
| # It is possible that two USB Bluetooth dongle devices have the same MAC address. This has already happened to me. | |
| function end() { | |
| printf "\n\nend... :)\n\n" | |
| disconn_bth_device | |
| remove_bth_device | |
| sudo systemctl stop bluetooth | |
| exit 1 | |
| } | |
| # macAddressBthHeadset=AA:BB:CC:DD:EE:FF | |
| macAddressBthHeadset=$BTH_HEADSET_TEST | |
| #ctrl-c | |
| trap end SIGINT | |
| function press_any_key_to_continue() { | |
| read -n 1 -srp "Press any key to continue" | |
| } | |
| function scan_bth_device() { | |
| expect -c "set timeout 20 | |
| spawn bluetoothctl --timeout 19 scan on | |
| expect \"$macAddressBthHeadset\" { | |
| exit 0 | |
| } | |
| exit 1 | |
| " | |
| return $? | |
| } | |
| function conn_bth_device() { | |
| expect -c "set timeout 20 | |
| spawn bluetoothctl --timeout 19 connect $macAddressBthHeadset | |
| expect \"Connection successful\" { | |
| exit 0 | |
| } | |
| exit 1 | |
| " | |
| return $? | |
| } | |
| function disconn_bth_device() { | |
| if [[ $(sudo systemctl status bluetooth | sed '3!d') == *"Active: active"* ]]; then | |
| expect -c "set timeout 5 | |
| spawn bluetoothctl --timeout 4 disconnect $macAddressBthHeadset | |
| expect \"Successful disconnected\" { | |
| exit 0 | |
| } | |
| exit 1 | |
| " | |
| return $? | |
| fi | |
| return 1 | |
| } | |
| function remove_bth_device() { | |
| timeout 4s bash -c " | |
| bluetoothctl --timeout 3 remove $macAddressBthHeadset | |
| " | |
| } | |
| function verify_pactl_list_sinks() { | |
| pactl list sinks | |
| if [ $? -eq 0 ]; then | |
| # Get the text part of where the sink name audio device of bluetooth appear | |
| macAddressBthHeadsetFormat2=$(echo $macAddressBthHeadset | sed -r 's/:/./g') | |
| # xargs is for trim the text | |
| bth_spk_headset_etc_sink_name=$(pactl list sinks | grep -E "^.*Name:.*$macAddressBthHeadsetFormat2.*$" | sed -r 's/Name: //' | xargs) | |
| printf "\n\nbth_spk_headset_etc_sink_name=%s\n\n" "$bth_spk_headset_etc_sink_name" | |
| if [[ -z "$bth_spk_headset_etc_sink_name" ]]; then | |
| return 1 | |
| fi | |
| # Set default audio device | |
| pactl set-default-sink "$bth_spk_headset_etc_sink_name" | |
| # Set volume to %5. You can change the volume to any other value | |
| pactl set-sink-volume "$bth_spk_headset_etc_sink_name" 5% | |
| bluetoothctl pairable off # Optional, security related | |
| bluetoothctl discoverable off # Optional, security related | |
| return 0 | |
| else | |
| return 1 | |
| fi | |
| } | |
| function task() { | |
| disconn_bth_device | |
| BthStatusConnection=$? | |
| if [ "$BthStatusConnection" -gt 0 ]; then | |
| echo "The Bluetooth headset/speaker/etc device could not be disconnected. The following steps will be continued. " | |
| else | |
| echo "Bluetooth headset/speaker/etc was disconnected" | |
| fi | |
| remove_bth_device | |
| # Stop bluetooth service | |
| sudo systemctl stop bluetooth | |
| echo "Bluetooth service is off" | |
| busNport=$(timeout 5s bash -c " | |
| # Search for bluetooth device in usb port | |
| _busnport=\"\" | |
| for device in /sys/bus/usb/devices/* | |
| do | |
| if [[ \$(cat \$device/uevent) =~ .*DRIVER=btusb ]]; then | |
| _busnport=\$(echo \$device | cut -d'/' -f6 | grep -oEi '[0-9]+-[0-9]+') | |
| fi | |
| done | |
| echo \$_busnport | |
| ") | |
| printf "\nbusNport: $busNport\n" | |
| if [[ -z "$busNport" ]]; then | |
| echo "Bluetooth usb dongle is not connected" | |
| press_any_key_to_continue | |
| exit 1 | |
| fi | |
| # Unbind usb port. On unbind state, usb device can't operate | |
| echo $busNport | sudo tee /sys/bus/usb/drivers/usb/unbind | |
| # Sleep 2 second | |
| sleep 2s | |
| # Bind usb port. | |
| echo $busNport | sudo tee /sys/bus/usb/drivers/usb/bind | |
| # Info related if bluetooth usb dongle is connected to usb port | |
| result=$? | |
| if [ "$result" -eq 0 ]; then | |
| echo "Bluetooth usb dongle is connected" | |
| elif [ "$result" -eq 1 ]; then | |
| echo "Bluetooth usb dongle is not connected" | |
| press_any_key_to_continue | |
| exit 1 | |
| else | |
| echo "Stuck in \"for device in /sys/bus/usb/devices/*\" or something another problem. Disconnect or connect the bluetooth usb dongle is a posible fix" | |
| press_any_key_to_continue | |
| exit $? | |
| fi | |
| # Start bluetooth service | |
| sudo systemctl start bluetooth | |
| echo "Bluetooth service is on" | |
| # Sleep 2 second | |
| sleep 2s | |
| # Posible fix: PART org.bluez.Error.Failed br-connection-profile-unavailable | |
| if [ -f /usr/bin/pulseaudio ]; then | |
| pulseaudio --kill | |
| pulseaudio --start | |
| fi | |
| # In ArchLinux, if you use pipewire and no sound plays after starting openbox with startx, it does not connect to Bluetooth headphones/speaker/etc. | |
| # Solution to the issue when connecting to a headphone/speaker/etc without playing any sound using Pipewire as the multimedia server after starting a Window Manager. The connection is not established, so I use speaker-test to play a sound before connecting. | |
| speaker-test -l 1 | |
| # Specific USB Bluetooth Dongle as default controller and verify if exist MAC Address of USB bth Dongle | |
| #bluetoothctl select $macAddressUSBbthDongle | |
| #if [ $? -gt 0 ]; then | |
| # echo "Bluetooth usb dongle does not match with the mac address" | |
| # press_any_key_to_continue | |
| # exit 1 | |
| #fi | |
| # Detect if speaker/headset/etc bluetooth is actived | |
| scan_bth_device | |
| checkBTHdevice=$? | |
| echo checkBTHdevice $checkBTHdevice | |
| # If bluetooth headset/speaker/etc is active, start to connect | |
| if [ "$checkBTHdevice" -eq 0 ]; then | |
| second_test=1 | |
| # Quick mode with pipewire | |
| if [ -f /usr/bin/pipewire ]; then | |
| printf "\n\nSHORT TEST\n\n" | |
| echo "Mode pipewire and connect, without paired and other things" | |
| conn_bth_device | |
| if [ "$?" -eq 0 ]; then | |
| verify_pactl_list_sinks | |
| if [ "$?" -eq 0 ]; then | |
| second_test=0 | |
| return 0 | |
| fi | |
| fi | |
| fi | |
| if [ "$?" -gt 0 ] || [ "$second_test" -eq 1 ]; then | |
| # Posible fix: org.bluez.Error.Failed br-connection-unknown | |
| # https://askubuntu.com/a/1429906/1572826 | |
| printf "\n\nLONG TEST\n\n" | |
| disconn_bth_device | |
| remove_bth_device | |
| scan_bth_device | |
| bluetoothctl --timeout 3 trust $macAddressBthHeadset | |
| # Pairing | |
| expect -c "set timeout 20 | |
| spawn bluetoothctl pair $macAddressBthHeadset | |
| expect \"Pairing successful\" { | |
| exit 0 | |
| } | |
| exit 1 | |
| " | |
| # Connection | |
| BthStatusConnection=$? | |
| echo BthStatusConnection $BthStatusConnection | |
| if [ "$BthStatusConnection" -gt 0 ]; then | |
| echo "Could not pair to the Bluetooth headset/speaker/etc" | |
| return 1 | |
| else | |
| echo "Bluetooth headset/speaker/etc was paired" | |
| fi | |
| sleep 10s | |
| conn_bth_device | |
| BthStatusConnection=$? | |
| echo BthStatusConnection $BthStatusConnection | |
| if [ "$BthStatusConnection" -gt 0 ]; then | |
| echo "Could not connect to the Bluetooth headset/speaker/etc" | |
| return 1 | |
| else | |
| verify_pactl_list_sinks | |
| return $? | |
| fi | |
| fi | |
| else | |
| echo "Bluetooth headset/speaker/etc is not active" | |
| return 1 | |
| fi | |
| } | |
| ########### | |
| ### INIT #### | |
| #################### | |
| for (( cont=0; cont<6 ; cont++ )) | |
| do | |
| echo | |
| echo "ATTEMPT NUMBER $((cont+1))" | |
| echo | |
| task | |
| if [ $? -eq 0 ]; then | |
| break | |
| fi | |
| done | |
| press_any_key_to_continue | |
| # Sources: | |
| # https://www.heelpbook.net/2020/linux-disabling-a-webcam-or-usb-ports/ | |
| # https://help.electronic.us/support/solutions/articles/44001312589-how-to-get-the-list-of-usb-devices-without-gui-and-kernel-module-on-linux- | |
| # https://wiki.archlinux.org/title/Bluetooth_headset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment