-
-
Save OndraZizka/2724d353f695dacd73a50883dfdf0fc6 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
#### Restart Bluetooth | |
if [ "$1" == "resetBT" ] ; then | |
sudo rfkill block bluetooth && sleep 0.1 && sudo rfkill unblock bluetooth; | |
exit; | |
fi; | |
#### Toggle listen/speak | |
if [ "$1" == "" -o "$1" == "toggle" ] ; then | |
LINE=`pacmd list-sinks | grep '\(name:\|alias\)' | grep -B1 F5A | head -1` | |
if [ "$LINE" == "" ] ; then echo "F5A headset not found."; $0 reset; sleep 2; exit; fi | |
SINK_NAME="bluez_sink.00_19_5D_25_6F_6C.a2dp_sink" | |
if $(echo "$LINE" | grep $SINK_NAME &> /dev/null) ; then | |
echo "Detected quality sound output, that means we can't speak; switch that." | |
$0 speak; | |
else | |
echo "Quality sound not found, switch to the good sound." | |
$0 listen; | |
fi | |
fi | |
#### Change the output to F5A | |
if [ "$1" == "listen" ] ; then | |
LINE=`pacmd list-sinks | grep '\(name:\|alias\)' | grep -B1 F5A | head -1` | |
if [ "$LINE" == "" ] ; then echo "F5A phones not found."; $0 reset; sleep 2; exit; fi | |
# name: <bluez_sink.00_19_5D_25_6F_6C.headset_head_unit> | |
## Get what's between <...> | |
SINK_NAME=`echo "$LINE" | tr '>' '<' | cut -d'<' -f2`; | |
## The above gives an ID according to the active profile. | |
## To set manually: | |
#SINK_NAME="bluez_sink.00_19_5D_25_6F_6C.headset_head_unit" | |
#SINK_NAME="bluez_sink.00_19_5D_25_6F_6C.a2dp_sink" | |
## Switch the output to that. | |
echo "Switching audio output to $SINK_NAME"; | |
pacmd set-default-sink "$SINK_NAME" | |
#### Change profile to quality output + no mic. From `pacmd list-cards`: | |
CARD="bluez_card.00_19_5D_25_6F_6C" | |
PROFILE="a2dp_sink" | |
echo "Switching audio profile to $PROFILE"; | |
pacmd set-card-profile $CARD $PROFILE | |
exit; | |
fi; | |
#### Input | |
if [ "$1" == "speak" ] ; then | |
## Change profile to crappy output + mic. From `pacmd list-cards`: | |
CARD="bluez_card.00_19_5D_25_6F_6C" | |
pacmd set-card-profile $CARD headset_head_unit | |
LINE=`pacmd list-sources | grep '\(name:\|alias\)' | grep -B1 F5A | head -1` | |
if [ "$LINE" == "" ] ; then echo "F5A mic not found."; $0 reset; sleep 2; exit; fi | |
SOURCE_NAME=`echo "$LINE" | tr '>' '<' | cut -d'<' -f2`; | |
#SOURCE_NAME="bluez_source.00_19_5D_25_6F_6C.headset_head_unit" | |
#SOURCE_NAME="bluez_sink.00_19_5D_25_6F_6C.a2dp_sink.monitor" | |
echo "Switching audio input to $SOURCE_NAME"; | |
pacmd set-default-source "$SOURCE_NAME" || echo 'Try `pacmd list-sources`.'; | |
fi; | |
#### Resources: | |
## Why this is needed | |
# https://jimshaver.net/2015/03/31/going-a2dp-only-on-linux/ | |
## My original question | |
# https://askubuntu.com/questions/1004712/audio-profile-changes-automatically-to-hsp-bad-quality-when-i-change-input-to/1009156#1009156 | |
## Script to monitor plugged earphones and switch when unplugged (Ubuntu does that, but nice script): | |
# https://github.com/freundTech/linux-helper-scripts/blob/master/padevswitch/padevswitch |
Hi all, I am glad that there has been quite a lot of activity around my humble script :)
I personally moved to a device with BlueTooth 5, so I can't work on this anymore.
@tvaillantdeguelis wrote a good summary - BlueTooth 4 was not capable of supporting a microphone at a decent quality, unless there were workarounds supported by both the drivers and the device, like setting up 2 BT connections. I could not make that work on Linux.
@Fuzzphorescent I ran into the same problem today.
You might have found a solution for your problem by now, but I hope someone else with similar problem finds this useful
Original script searches ( greps) for F5A keyword from list of connected devices (sinks to be accurate) , well your device might have different name like mine. So, that's why you got that error.
The script given by artem328 worked like a charm for me along with toggle button. So try it out
@JustinFrost47 I never did figure it out but now having read your answer, it seems so obvious what the problem was. Looks like I hadn't skimmed the code back then to find out what prompted the error message.
I've since moved to a newer machine running Windows 10 (which is posing its own set of BT-related challenges). I'm now inspired to dust off the old laptop and give it another go for an undeserved sense of accomplishment. Thanks for the help, cheers.
I am using the script from @artem328
Maybe someone can help me find the variables I need for this script to work, and where to add them to the script -- for instance, the name of the headset.
I get this error:
headset not found
It is a BOSE QC35 II
On line 4 I have:
BLUETOOTH_DEVICE="BOSE QC35 II"
but I get the above error message, and the headphones disconnect.
EDIT: The correct name is "Bose QC35 II" and then it works like a charm.
Thanks for script!!
details from blueman:
60:AB:D2:45:5D:47 public Bose QC35 II Bose QC35 II 0x240418 0x0000 audio-card yes yes no no yes 00000000-deca-fade-deca-deafdecacaff Proprietary 00001101-0000-1000-8000-00805f9b34fb Serial Port 00001108-0000-1000-8000-00805f9b34fb Headset 0000110b-0000-1000-8000-00805f9b34fb Audio Sink 0000110c-0000-1000-8000-00805f9b34fb Remote Control Target 0000110d-0000-1000-8000-00805f9b34fb Advanced Audio 0000110e-0000-1000-8000-00805f9b34fb Remote Control 0000111e-0000-1000-8000-00805f9b34fb Handsfree 0000112f-0000-1000-8000-00805f9b34fb Phonebook Access (PBAP) - PSE 00001200-0000-1000-8000-00805f9b34fb PnP Information 81c2e72a-0591-443e-a1ff-05f988593351 Proprietary 931c7e8a-540f-4686-b798-e8df0a2ad9f7 Proprietary f8d1fbe4-7966-4334-8024-ff96c9330e15 Proprietary bluetooth:v009Ep4020d0436 /org/bluez/hci0
Kubuntu 20.04
EDIT: The name of device is case-sensitive. The correct name is "Bose QC35 II" and then it works like a charm.
Thanks for script!!