-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbt-connect.sh
More file actions
70 lines (57 loc) · 1.6 KB
/
Copy pathbt-connect.sh
File metadata and controls
70 lines (57 loc) · 1.6 KB
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
#!/bin/bash
# bt-connect.sh
# Usage:
# bt-connect.sh -s <shortcut> [profile]
# Example:
# bt-connect.sh -s q20i
# bt-connect.sh -s q20i headset-head-unit
declare -A DEVICES
DEVICES=(
[q20i]="88:0E:85:62:AB:ED"
[earbuds]="XX:XX:XX:XX:XX:XX"
# add more shortcuts here
)
# parse args
while getopts "s:" opt; do
case $opt in
s) SHORTCUT="$OPTARG" ;;
*) echo "Usage: $0 -s <shortcut> [profile]"; exit 1 ;;
esac
done
shift $((OPTIND-1))
PROFILE="${1:-a2dp-sink}"
if [[ -z "$SHORTCUT" || -z "${DEVICES[$SHORTCUT]}" ]]; then
echo "Unknown shortcut: $SHORTCUT"
echo "Available shortcuts: ${!DEVICES[@]}"
exit 1
fi
DEVICE="${DEVICES[$SHORTCUT]}"
echo "Connecting $SHORTCUT ($DEVICE) with profile $PROFILE..."
# --- same connection logic as before ---
RETRIES=5
SLEEP_TIME=2
TIMEOUT=15
for i in $(seq 1 $RETRIES); do
echo "Attempt $i/$RETRIES..."
echo -e "power on\nagent on\ndefault-agent\nconnect $DEVICE" | bluetoothctl
sleep $SLEEP_TIME
SINK=""
ELAPSED=0
while [[ -z "$SINK" && $ELAPSED -lt $TIMEOUT ]]; do
SINK=$(pactl list short sinks | grep -i "$DEVICE" | awk '{print $2}')
[[ -n "$SINK" ]] && break
sleep 1
((ELAPSED++))
done
[[ -n "$SINK" ]] && break
done
if [[ -z "$SINK" ]]; then
echo "Error: Sink for $DEVICE not found."
exit 2
fi
CARD="bluez_card.${DEVICE//:/_}"
pactl set-card-profile "$CARD" "$PROFILE"
pactl suspend-sink "$SINK" 0
pactl set-default-sink "$SINK"
pactl list short sink-inputs | awk '{print $1}' | xargs -r -n1 pactl move-sink-input "$SINK"
echo "Audio switched to $SHORTCUT ($SINK)."