-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_audio_patch.sh
executable file
·213 lines (192 loc) · 5.33 KB
/
bluetooth_audio_patch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
# Description: this patch is used to connect bluetooth headset that can support both external mic and external audio
# Author: An Pham
# Date: Sun 19 Jun 2022 10:15:08 PM PDT
source ./device_change.sh
# Add or change bluetooth device name here
blue_headset=AC:12:2F:C8:56:FC
#blue_headset='E8:EE:CC:23:BD:5A'
#
display_devices () {
bluetoothctl -- devices | cut -d " " -f 1 --complement
}
select_device () {
pass
}
sudo_checker () {
pass
}
string_convert () {
local input_string="$1"
# convert the '3B:64:D2:73:B4:C3' string to 3B_64_D2_73_B4_C3
echo "$input_string" | sed -e 's/:/_/g'
}
connect_bluetooth () {
hcitool con | grep $blue_headset &> /dev/null
if [ $? == 1 ]
then
while ! bluetoothctl -- connect "$blue_headset"
do
echo "Waiting for bluetooth connection"
sleep 1
done
fi
}
setting_audio_profile () {
pactl set-sink-volume 0 20%
local blue_device="$(string_convert $blue_headset)"
#check audio profile
pactl set-default-sink "bluez_output.$blue_device.a2dp-sink" && notify-send "Audio connected!"
}
disconnect_bluetooth () {
local blue_device="$(echo "$blue_headset" | sed "s/_/:/g") "
bluetoothctl -- disconnect $blue_headset; pactl set-sink-volume 0 0%
pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo
}
restart_bluetooth () {
disconnect_bluetooth
systemctl --user daemon-reload
connect_bluetooth
}
install_dependency () {
# Installing pipewire audio library (on top of pulseaudio)
add-apt-repository ppa:pipewire-debian/pipewire-upstream
apt-get update
apt install pipewire pipewire-audio-client-libraries
apt install gstreamer1.0-pipewire libpipewire-0.3-{0,dev,modules} libspa-0.2-{bluetooth,dev,jack,modules} pipewire{,-{audio-client-libraries,pulse,media-session,bin,locales,tests}}
# Installing notifier
apt install dunst
}
repatch_bluetooth () {
apt install libspa-0.2-bluetooth
systemctl --user --now disable pulseaudio.service pulseaudio.socket
systemctl --user --now disable pulseaudio.service
systemctl --user --now disable pulseaudio.socket
systemctl --user mask pulseaudio
systemctl --user --now enable pipewire-media-session.service
systemctl --user restart pipewire
}
set_audio_only () {
# add profile to the card
local bluetooth_dev="$(string_convert $blue_headset)"
pactl set-card-profile "bluez_card.$bluetooth_dev" a2dp-sink && notify-send "Bluetooth headset has been switched to audio mode"
if [ $? == 0 ]
then
pactl set-sink-volume 0 70%
pactl set-default-sink "bluez_output.$bluetooth_dev.a2dp-sink"
else
notify-send "Error: can't find audio source"
fi
}
increase_volume () {
pactl set-sink-volume 0 +10%
notify-send "Volume has been increased"
}
decrease_volume () {
pactl set-sink-volume 0 -10%
notify-send "Volume has been decreased"
}
check_if_device_available () {
local device_name="$(bluetoothctl -- devices) | grep $blue_headset)";
[ "$?" = 0 ] && echo "Deviced Found"
grep -i [$bluetooth_headset]
# WIP
}
set_mic_only () {
# add mic and audion
local bluetooth_dev="$(string_convert $blue_headset)"
pactl set-card-profile "bluez_card.$bluetooth_dev" headset-head-unit
pactl set-sink-volume 0 40%
echo "Volume has been switched to audio and mic mode"
notify-send "Volume has been switched to audio and mic mode"
}
mute_audio () {
pactl set-sink-volume 0 0%
}
unmute_audio () {
pactl set-sink-volume 0 40%
}
main () {
while [ -n "$1" ]
do
case "$1" in
--reset | -r)
restart_bluetooth
shift
;;
--install-dependency )
install_dependency
shift
;;
--version | -v)
echo '1.0'
;;
--con | --connect)
connect_bluetooth
shift
;;
--dis | --disconnect)
disconnect_bluetooth
shift
;;
--audio | -a)
connect_bluetooth && sleep 3s && set_audio_only || echo "Fail: Unable to connect the bluetooth device"
shift
;;
-m)
mute_audio
shift
;;
-um)
unmute_audio
shift
;;
--mic | -ac)
connect_bluetooth && set_mic_only || echo "Fail: Unable to connect the bluetooth device"
shift
;;
-i | --inc)
increase_volume
shift
;;
--dec)
decrease_volume
shift
;;
--display | -l)
display_devices
shift
;;
--change-bluetooth)
ask_devices
connect_bluetooth && sleep 3s && set_audio_only || echo "Fail: Unable to connect the bluetooth device"
shift
;;
* | -*)
echo 'Invalid argument'
help
exit 1
break
;;
esac
done
}
help () {
echo "Usuage: $(basename $0) <--con|--dis|-a|-ac>. First, edit the variable to match your bluetooth interface MAC address. "
echo ''
echo 'Where:'
echo ' --con,--connect connect to bluetooth device from the script'
echo ' -r,--restart restart the bluetooth headset connection'
echo ' -l,--list show current bluetooth devices'
echo ' --dis,--disconnect disconnect bluetooth headset'
echo ' -a,--audio connect with audio only'
echo ' -ac,--audio-mic connect with both audio and mic'
echo ' -m,--mute mute the audio'
echo ' -um,--unmute unmute the audio'
echo ' -i,--install install dependency'
echo ' --change-bluetooth change bluetooth device'
echo ''
echo 'Example:'
echo " $(basename $0) -c | --check Check if the device is connected"
}
main "$@"