#!/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 |
This comment has been minimized.
This comment has been minimized.
Thats great! It Works perfectly with My PLT Focus! Thx! |
This comment has been minimized.
This comment has been minimized.
Thank you very much for this effort. I wonder how can it work on Windows? when I use my bluetooth headphones there I can talk and listen with good quality. |
This comment has been minimized.
This comment has been minimized.
really great, thanks. Bose headsets were giving headache with this. Regards! |
This comment has been minimized.
This comment has been minimized.
worked like a charm |
This comment has been minimized.
This comment has been minimized.
this looks like exactly what I need for my Mpow M5 headset. It works a treat in Ubuntu, but am now trying to get it to work in Manjaro KDE. However, I'm getting "line 30: command not found" Am a 'cut and paster' with code, so very limited understanding. My guess is that I've misunderstood the blanks I need to fill for my device. My only initial change to the code was BLUETOOTH_DEVICE="Mpow M5" Do I need to make other changes? |
This comment has been minimized.
This comment has been minimized.
Awesome, thank you. |
This comment has been minimized.
This comment has been minimized.
I don't think so, that's it. |
This comment has been minimized.
This comment has been minimized.
Hey I am new here. Can anyone please tell me what to do with the script? How to use it? |
This comment has been minimized.
This comment has been minimized.
Thank you for this script. It works nicely as expected! I suggest to use it together with a toggle button, e.g.,
|
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
I think this should become a repo by it's own. There're nice improvements to be done to be more generic and w/ toggle functionality @OndraZizka what do you think? |
This comment has been minimized.
This comment has been minimized.
I don't know how to run this, I'm new to linux. Can someone please help me out ? |
This comment has been minimized.
This comment has been minimized.
Usage:
|
This comment has been minimized.
This comment has been minimized.
@weslleyspereira, i tried running the above command of your's on ubuntu terminal , the |
This comment has been minimized.
This comment has been minimized.
Hi!
|
This comment has been minimized.
This comment has been minimized.
@thedopepirate you shouldn't rename the .sh to .py |
This comment has been minimized.
This comment has been minimized.
Thanks ! |
This comment has been minimized.
Thanks for script, i've modified it a little bit to have more universal usage.