Repository navigation
Expand file tree
/
Copy pathbt-menu
More file actions
executable file
·518 lines (446 loc) · 15.3 KB
/
Copy pathbt-menu
File metadata and controls
executable file
·518 lines (446 loc) · 15.3 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/bin/bash
# bt-menu — Interactive Bluetooth manager with fzf
set -uo pipefail
# Cleanup function
cleanup() {
if [[ -n "${SCAN_PID:-}" ]] && kill -0 "$SCAN_PID" 2>/dev/null; then
kill "$SCAN_PID" 2>/dev/null || true
fi
bluetoothctl scan off >/dev/null 2>&1 || true
}
trap cleanup EXIT INT TERM
# Check dependencies
check_deps() {
local missing=()
local cmd
for cmd in "$@"; do
if ! command -v "$cmd" &>/dev/null; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo "Error: Missing required dependencies: ${missing[*]}" >&2
echo "Install with: sudo apt install bluez fzf gawk" >&2
exit 1
fi
}
# Show help message
show_help() {
cat << 'EOF'
bt-menu - Interactive Bluetooth manager
USAGE:
bt-menu [COMMAND] [OPTIONS]
COMMANDS:
help Show this help message (default if no command)
pair Scan for and pair with NEW Bluetooth devices
manage Interactively manage paired devices (connect/disconnect/forget)
ls List devices (with optional flags)
OPTIONS:
pair [SECONDS] Optional scan duration in seconds (default: 5)
ls -a, --all Show all paired devices (including disconnected)
EXAMPLES:
bt-menu # Show help
bt-menu pair # Scan for 5 seconds and pair with a new device
bt-menu pair 10 # Scan for 10 seconds and pair
bt-menu manage # Manage already-paired devices (loops)
bt-menu ls # Quick list of connected devices only
bt-menu ls --all # List all paired devices with status
REQUIREMENTS:
- bluetoothctl (bluez package)
- fzf (fuzzy finder) - for pair/manage commands
- awk (text processing)
EOF
}
# Check if Bluetooth is powered on
check_bluetooth() {
if ! bluetoothctl show | grep -q "Powered: yes"; then
echo "Bluetooth is powered off. Attempting to power on..." >&2
bluetoothctl power on >/dev/null 2>&1 || {
echo "Error: Failed to power on Bluetooth. Try: bluetoothctl power on" >&2
exit 1
}
sleep 1
fi
}
# Function to scan for new devices briefly
scan_devices() {
local wait_time="${1:-5}"
echo "🔍 Scanning for devices (${wait_time}s)..."
bluetoothctl scan on >/dev/null 2>&1 &
SCAN_PID=$!
sleep "$wait_time"
if kill -0 "$SCAN_PID" 2>/dev/null; then
kill "$SCAN_PID" 2>/dev/null || true
fi
bluetoothctl scan off >/dev/null 2>&1
unset SCAN_PID
}
# Get list of all Bluetooth controllers
get_controllers() {
bluetoothctl list 2>/dev/null | awk '{print $2}'
}
# Get list of paired devices from all controllers
get_paired() {
local controllers
controllers=$(get_controllers)
local all_devices=""
local controller
# Query each controller
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
local devices
devices=$(bluetoothctl paired-devices 2>/dev/null | \
awk '{mac=$2; $1=$2=""; name=substr($0,3); if(name) print mac " [PAIRED] " name}')
if [[ -n "$devices" ]]; then
all_devices+="$devices"$'\n'
fi
done <<< "$controllers"
# Remove duplicates and print
echo "$all_devices" | grep -v '^$' | sort -u
}
# Get list of unpaired devices (exclude already paired) from all controllers
get_new() {
local controllers
controllers=$(get_controllers)
# Get all paired MACs from all controllers
local paired_macs=""
local controller
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
local macs
macs=$(bluetoothctl paired-devices 2>/dev/null | awk '{print $2}')
if [[ -n "$macs" ]]; then
paired_macs+="$macs"$'\n'
fi
done <<< "$controllers"
# Get all devices from all controllers and filter out paired ones
local all_new=""
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
local devices
devices=$(bluetoothctl devices 2>/dev/null | \
awk -v paired="$paired_macs" '
BEGIN { split(paired, p_arr, "\n"); for(i in p_arr) paired_map[p_arr[i]]=1 }
{
mac=$2
$1=$2=""
name=substr($0,3)
if(name && !(mac in paired_map)) print mac " " name
}')
if [[ -n "$devices" ]]; then
all_new+="$devices"$'\n'
fi
done <<< "$controllers"
# Remove duplicates and print
echo "$all_new" | grep -v '^$' | sort -u
}
# Execute Bluetooth action with error handling
execute_action() {
local action="$1"
local mac="$2"
local output
local status
case "$action" in
connect)
echo "⏳ Connecting..."
output=$(bluetoothctl connect "$mac" 2>&1)
status=$?
;;
pair)
echo "⏳ Pairing..."
output=$(bluetoothctl pair "$mac" 2>&1)
status=$?
;;
trust)
echo "⏳ Trusting..."
output=$(bluetoothctl trust "$mac" 2>&1)
status=$?
;;
disconnect)
echo "⏳ Disconnecting..."
output=$(bluetoothctl disconnect "$mac" 2>&1)
status=$?
;;
remove)
echo "⏳ Removing..."
output=$(bluetoothctl remove "$mac" 2>&1)
status=$?
;;
esac
if [[ $status -eq 0 ]]; then
echo "✓ ${action^} successful"
return 0
else
echo "✗ ${action^} failed: $output" >&2
return 1
fi
}
# List connected devices
cmd_list() {
check_deps bluetoothctl awk
local show_all=false
# Check for -a or --all flag
if [[ "${1:-}" == "-a" || "${1:-}" == "--all" ]]; then
show_all=true
fi
if [[ "$show_all" == "true" ]]; then
echo "All Paired Bluetooth Devices:"
else
echo "Connected Bluetooth Devices:"
fi
echo "----------------------------"
echo ""
local found_any=false
local controllers
controllers=$(get_controllers)
# Query all controllers for paired devices
local all_paired_macs=""
local controller
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
local macs
macs=$(bluetoothctl paired-devices 2>/dev/null | awk '{print $2}')
if [[ -n "$macs" ]]; then
all_paired_macs+="$macs"$'\n'
fi
done <<< "$controllers"
# Process each unique paired device
local processed_macs=""
while IFS= read -r mac; do
[[ -z "$mac" ]] && continue
# Skip if already processed (avoid duplicates)
if echo "$processed_macs" | grep -q "^${mac}$"; then
continue
fi
processed_macs+="$mac"$'\n'
# Try to get info from any controller
local info=""
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
info=$(bluetoothctl info "$mac" 2>/dev/null)
if [[ -n "$info" ]] && echo "$info" | grep -q "Name:"; then
break
fi
done <<< "$controllers"
[[ -z "$info" ]] && continue
local is_connected
is_connected=$(echo "$info" | grep -q "Connected: yes" && echo "yes" || echo "no")
# Skip disconnected devices unless --all flag is set
if [[ "$is_connected" == "no" && "$show_all" == "false" ]]; then
continue
fi
found_any=true
# Extract device details
local name battery icon status_icon
name=$(echo "$info" | grep "Name:" | head -1 | awk '{$1=""; print substr($0,2)}')
battery=$(echo "$info" | grep "Battery Percentage" | awk -F'[()]' '{print $2}')
icon=$(echo "$info" | grep "Icon:" | awk '{print $2}')
# Status indicator
if [[ "$is_connected" == "yes" ]]; then
status_icon="🟢"
else
status_icon="⚪"
fi
echo -n " $status_icon $name"
[[ -n "$battery" ]] && echo -n " (🔋 $battery%)"
echo ""
echo " MAC: $mac"
[[ -n "$icon" ]] && echo " Type: $icon"
if [[ "$show_all" == "true" ]]; then
if [[ "$is_connected" == "yes" ]]; then
echo " Status: Connected"
else
echo " Status: Disconnected"
fi
fi
echo ""
done <<< "$all_paired_macs"
if [[ "$found_any" == "false" ]]; then
if [[ "$show_all" == "true" ]]; then
echo "No paired devices found."
else
echo "No devices currently connected."
echo "Use 'bt-menu ls --all' to see all paired devices."
fi
fi
}
# Interactive device manager (all paired devices)
cmd_manage() {
check_deps bluetoothctl fzf awk
check_bluetooth
while true; do
# Get all paired devices from all controllers
local menu=""
local controllers
controllers=$(get_controllers)
local all_paired_macs=""
local controller
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
local macs
macs=$(bluetoothctl paired-devices 2>/dev/null | awk '{print $2}')
if [[ -n "$macs" ]]; then
all_paired_macs+="$macs"$'\n'
fi
done <<< "$controllers"
if [[ -z "$all_paired_macs" ]]; then
echo "No paired devices found." >&2
echo "Use 'bt-menu pair' to pair with new devices." >&2
exit 0
fi
# Build menu with connection status
local processed_macs=""
while IFS= read -r mac; do
[[ -z "$mac" ]] && continue
# Skip if already processed (avoid duplicates)
if echo "$processed_macs" | grep -q "^${mac}$"; then
continue
fi
processed_macs+="$mac"$'\n'
# Try to get info from any controller
local info=""
while IFS= read -r controller; do
[[ -z "$controller" ]] && continue
bluetoothctl select "$controller" >/dev/null 2>&1
info=$(bluetoothctl info "$mac" 2>/dev/null)
if [[ -n "$info" ]] && echo "$info" | grep -q "Name:"; then
break
fi
done <<< "$controllers"
[[ -z "$info" ]] && continue
local name status_tag
name=$(echo "$info" | grep "Name:" | head -1 | awk '{$1=""; print substr($0,2)}')
if echo "$info" | grep -q "Connected: yes"; then
status_tag="[CONNECTED]"
else
status_tag="[DISCONNECTED]"
fi
menu+="$mac $status_tag $name"$'\n'
done <<< "$all_paired_macs"
# Device selection
local choice
choice=$(echo "$menu" | grep -v '^$' | fzf --prompt="📱 Select device to manage (ESC to quit): " \
--header="Paired Devices" \
--border --height=50%) || exit 0
local mac name is_connected
mac=$(echo "$choice" | awk '{print $1}')
is_connected=$(echo "$choice" | grep -q "\[CONNECTED\]" && echo "yes" || echo "no")
name=$(echo "$choice" | awk '{$1=$2=""; print substr($0,2)}')
# Conditional action menu based on connection status
local actions
if [[ "$is_connected" == "yes" ]]; then
actions="Disconnect\nForget (Remove)"
else
actions="Connect\nForget (Remove)"
fi
local action
action=$(echo -e "$actions" | fzf --prompt="⚡ Action for '$name': " \
--header="Choose action" \
--border --height=40%) || continue
echo ""
case "$action" in
"Connect")
execute_action connect "$mac" || sleep 2
;;
"Disconnect")
execute_action disconnect "$mac" || sleep 2
;;
"Forget (Remove)")
echo "⚠️ This will remove the device. Continue? (y/N)"
read -r -n 1 confirm
echo ""
if [[ "$confirm" =~ ^[Yy]$ ]]; then
execute_action remove "$mac" || sleep 2
else
echo "Cancelled."
fi
;;
esac
echo ""
echo "Press Enter to continue..."
read -r
done
}
# Main pairing function (only for unpaired devices)
cmd_pair() {
check_deps bluetoothctl fzf awk
check_bluetooth
local wait_time=5
# Parse optional wait time flag
if [[ -n "${1:-}" ]]; then
if [[ "$1" =~ ^[0-9]+$ ]]; then
wait_time="$1"
else
echo "Error: Invalid wait time '$1'. Must be a positive integer." >&2
exit 1
fi
fi
scan_devices "$wait_time"
# Build menu: only show NEW/unpaired devices
local menu
menu=$(get_new | sort -u)
if [[ -z "$menu" ]]; then
echo "No unpaired devices found." >&2
echo "" >&2
echo "Tips:" >&2
echo " • Put your Bluetooth device in pairing/discovery mode" >&2
echo " • Make sure the device is charged and nearby" >&2
echo " • Use 'bt-menu manage' to work with already-paired devices" >&2
exit 1
fi
# Device selection
local choice
choice=$(echo "$menu" | fzf --prompt="🔍 Select NEW device to pair (ESC to quit): " \
--header="New/Unpaired Devices" \
--border --height=50%) || exit 0
local mac name
mac=$(echo "$choice" | awk '{print $1}')
name=$(echo "$choice" | awk '{$1=""; print substr($0,2)}')
# Pair directly without action menu
echo ""
echo "Pairing with '$name'..."
if execute_action pair "$mac"; then
execute_action trust "$mac"
sleep 1
execute_action connect "$mac"
echo ""
echo "✓ Successfully paired and connected to '$name'"
else
echo ""
echo "✗ Failed to pair with '$name'"
exit 1
fi
}
# Main entry point
main() {
local command="${1:-}"
case "$command" in
"")
show_help
;;
help|--help|-h)
show_help
;;
pair)
cmd_pair "${2:-}"
;;
ls|list)
cmd_list "${2:-}"
;;
manage|m)
cmd_manage
;;
*)
echo "Error: Unknown command '$command'" >&2
echo "Run 'bt-menu help' for usage information" >&2
exit 1
;;
esac
}
main "$@"