Skip to content

Instantly share code, notes, and snippets.

@henri
Last active February 19, 2024 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henri/5c4d83b4c377b125411dde42e06189c6 to your computer and use it in GitHub Desktop.
Save henri/5c4d83b4c377b125411dde42e06189c6 to your computer and use it in GitHub Desktop.
bluetooth disable auto connect
#!/usr/bin/env bash
#
# toggle auto aconnect
# Henri Shustak 2023
# Released under under MIT Licence
# version 1.1 - initial release
# version 1.2 - will work no matter the state of the lock files
toggle_file=/tmp/bluetooth_connect.toogle
my_headset="0C:C4:13:12:34:56"
default_sink="bluez_output.51_98_75_F3_14_QW.a2dp-sink"
if ! [[ -e ${toggle_file} ]] && ! [[ -e ${toggle_file}-error ]] ; then
# Block Connecting
bluetoothctl unblock ${my_headset} && bluetoothctl connect ${my_headset}
if [[ $? == 0 ]] ; then
touch ${toggle_file}
pactl set-default-sink $my_headset_sink
else
touch ${toggle_file}-error
fi
else
# Block Connecting
bluetoothctl disconnect ${my_headset} && bluetoothctl block ${my_headset}
rm -f ${toggle_file}
rm -f ${toggle_file}-error
fi
# NOTES :
#
# Find bluetooth device MAC address :
# $ bluetoothctl devices
#
# Manually block the MAC address :
# $ bluetoothctl block 0C:C4:13:12:34:56
#
# List pulse audio sinks :
# $ pactl list sinks
#
# check the systemd unit status for bluetooth
systemctl is-enabled bluetooth.service
systemctl status bluetooth.service
# show bluetooth devices
bluetoothctl show
# enter interactive bluetooth mode
bluetoothctl
# scan for devices
scan on
# connect / pair with device - device-id is found with the 'scan on' command above
connect <device-id>
# paired devices
paired-devices
# cancel pairing
cancel-pairing <device-id>
# remove device
remove <device-id>
#!/usr/bin/env bash
#
# toggle profile
# Henri Shustak 2023
# Released under under MIT Licence
toggle_file=/tmp/bluetooth_latency_quaility.toogle
my_headset="0C:C4:13:12:34:56"
bluez_dev=$(pactl list cards short | awk '/bluez/{print $2}')
high_quaility_profile=a2dp-sink
low_latency_profile=headset-head-unit-msbc
if ! [[ -e ${toggle_file} ]] ; then
echo ""
echo "bluetooth high quaility / high latency : \"$high_quaility_profile\" output profile selected"
echo ""
pactl set-card-profile "$bluez_dev" $high_quaility_profile
touch ${toggle_file}
else
echo ""
echo "bluetooth low latency / low quaility : \"$low_latency_profile\" output profile selected"
echo ""
pactl set-card-profile "$bluez_dev" $low_latency_profile
rm ${toggle_file}
fi
# NOTES :
#
# Find bluetooth device MAC address :
# $ bluetoothctl devices
#
# Manually set profile :
# $ BLUEZ_DEV=$(pactl list cards short | awk '/bluez/{print $2}')
# $ pactl set-card-profile "$BLUEZ_DEV" <profile-name>
#
# Show Bluetooth Information (which profils are supported as you may want to mess around to get something good).
# $ pactl list | grep -Pzo '.*bluez_card(.*\n)*'
# Alternatively just get the one line you are interested in "Part of profiles(s) :
# $ pactl list | grep -Pzo '.*bluez_card(.*\n)*' | tail -n 2
#!/usr/bin/env bash
#
# resink - set the output
# Henri Shustak 2023
# Released under under MIT Licence
# note you may need to edit the 'headset-head-unit' option to be something your device supports see the tips below
# https://gist.github.com/henri/5c4d83b4c377b125411dde42e06189c6/edit
BLUEZ_DEV=$(pactl list cards short | awk '/bluez/{print $2}')
pactl set-card-profile "$BLUEZ_DEV" headset-head-unit
pactl set-card-profile "$BLUEZ_DEV" off
pactl set-card-profile "$BLUEZ_DEV" headset-head-unit
#!/usr/bin/env bash
#
# resync to solve bluetooth latency
# Henri Shustak 2023
# Released under under MIT Licence
#
BLUEZ_DEV=$(pactl list cards short | awk '/bluez/{print $2}')
pactl set-card-profile "$BLUEZ_DEV" a2dp-sink-sbc
pactl set-card-profile "$BLUEZ_DEV" a2dp-sink-sbc_xq
pactl set-card-profile "$BLUEZ_DEV" a2dp-sink-sbc
# Notes :
# Find the available profiles for your device just switch from one to another this should sync it up.
# - pactl set-card-profile "$bluezcard"
#
# It is not perfect but it improves things.
# Maybe spending time messing around with more profiles
# would sort this out.
# Handy tips for sorting out latency issues with Bluetooth
# ---------------------------------------------------------
# If you are having issues with out of sync playback on a linux system
# then hopefully, some of these commands and the bluetooth_resink.bash
# script will help you get it going well.
# different headsets have different options
# depending on your system you may able to manually configure the latency
# of your output device. However, sometimes this will not work
# depending on your settings. Sometimes, you can get the headset to sort out
# the latency if it has support for such an option.
# list the bluetooth device information
pactl list | grep -Pzo '.*bluez_card(.*\n)*'
# within the output of the command above you will see something like the following
<snip>
headset-output: Headset (type: Headset, priority: 0, latency offset: 0 usec, available)
<snip>
Part of profile(s): a2dp-sink, headset-head-unit, a2dp-sink-sbc, a2dp-sink-sbc_xq, headset-head-unit-cvsd, headset-head-unit-msbc
# In the script below I hvae opted to use headset-head-unit
# just try some of the options (profiles) which your headset supports.
# hopefully one of them will get the audio in sync - even if the quaility is not as good.
# It is possible to manually specify the latancey.
# However, on some systems and with some settings you will get issues or dropouts.
pactl set-port-latency-offset <your-device-name> <your-output-port> <latency-ammount-microseconds>
- example : pactl set-port-latency-offset bluez_card.30_02_35_F1_14_FB headset-output 0
# quickly restart bluetooth deamon (you may need admin rights to start it up again for GUI sessions)
sudo service bluetooth restart
# you could also try the following option into the end of the file : /etc/modprobe.d/alsa-base.conf
# this will requires bluetooth restart see above
options snd-hda-intel model=generic
# if you get super despriate maybe take a look at some of the links below (your miliage may vary)
- Setup PipeWire on Ubuntu (or a direvitive) : https://gist.github.com/shakthizen/c943a07d3ef99f8f5b4004d77b358d2d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment