2024-08-15 03:41:41 +01:00
|
|
|
#!/bin/sh -e
|
2024-08-05 20:24:05 +01:00
|
|
|
|
2024-08-21 08:18:34 +01:00
|
|
|
. ../common-script.sh
|
2024-11-07 19:03:21 +00:00
|
|
|
. ../common-service-script.sh
|
2024-08-15 06:15:12 +01:00
|
|
|
|
|
|
|
setupBluetooth() {
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${YELLOW}Installing Bluez...${RC}"
|
2024-08-15 06:15:12 +01:00
|
|
|
if ! command_exists bluetoothctl; then
|
2024-09-17 13:23:23 +01:00
|
|
|
case "$PACKAGER" in
|
2024-08-15 06:15:12 +01:00
|
|
|
pacman)
|
2024-11-20 06:10:24 +00:00
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez bluez-utils
|
2024-08-15 06:15:12 +01:00
|
|
|
;;
|
2024-11-07 19:03:21 +00:00
|
|
|
apk)
|
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" add bluez
|
|
|
|
;;
|
2024-08-15 06:15:12 +01:00
|
|
|
*)
|
2024-09-19 01:03:32 +01:00
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" install -y bluez
|
2024-08-15 06:15:12 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${GREEN}Bluez is already installed.${RC}"
|
2024-08-15 06:15:12 +01:00
|
|
|
fi
|
|
|
|
|
2024-11-07 19:03:21 +00:00
|
|
|
startService bluetooth
|
2024-08-15 06:15:12 +01:00
|
|
|
}
|
|
|
|
|
2024-08-05 20:24:05 +01:00
|
|
|
main_menu() {
|
|
|
|
while true; do
|
|
|
|
clear
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${YELLOW}Bluetooth Manager${RC}"
|
|
|
|
printf "%b\n" "${YELLOW}=================${RC}"
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b\n" "1. Scan for devices"
|
|
|
|
printf "%b\n" "2. Pair with a device"
|
|
|
|
printf "%b\n" "3. Connect to a device"
|
|
|
|
printf "%b\n" "4. Disconnect from a device"
|
|
|
|
printf "%b\n" "5. Remove a device"
|
|
|
|
printf "%b\n" "0. Exit"
|
|
|
|
printf "%b" "Choose an option: "
|
2024-09-19 20:08:41 +01:00
|
|
|
read -r choice
|
2024-08-05 20:24:05 +01:00
|
|
|
|
|
|
|
case $choice in
|
|
|
|
1) scan_devices ;;
|
|
|
|
2) pair_device ;;
|
|
|
|
3) connect_device ;;
|
|
|
|
4) disconnect_device ;;
|
|
|
|
5) remove_device ;;
|
|
|
|
0) exit 0 ;;
|
2024-09-12 21:14:50 +01:00
|
|
|
*) printf "%b\n" "${RED}Invalid option. Please try again.${RC}" ;;
|
2024-08-05 20:24:05 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
scan_devices() {
|
|
|
|
clear
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${YELLOW}Scanning for devices...${RC}"
|
2024-08-05 20:24:05 +01:00
|
|
|
bluetoothctl --timeout 10 scan on
|
|
|
|
devices=$(bluetoothctl devices)
|
|
|
|
if [ -z "$devices" ]; then
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${RED}No devices found.${RC}"
|
2024-08-05 20:24:05 +01:00
|
|
|
else
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${GREEN}Devices found:${RC}"
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b\n" "$devices"
|
2024-08-05 20:24:05 +01:00
|
|
|
fi
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Press any key to return to the main menu..."
|
2024-11-19 18:59:19 +00:00
|
|
|
read -r _
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prompt_for_mac() {
|
2024-11-19 18:59:19 +00:00
|
|
|
command=$1
|
|
|
|
prompt_msg=$2
|
|
|
|
success_msg=$3
|
|
|
|
failure_msg=$4
|
2024-08-05 20:24:05 +01:00
|
|
|
|
|
|
|
while true; do
|
|
|
|
clear
|
|
|
|
devices=$(bluetoothctl devices)
|
|
|
|
if [ -z "$devices" ]; then
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${RED}No devices available. Please scan for devices first.${RC}"
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Press any key to return to the main menu..."
|
2024-11-19 18:59:19 +00:00
|
|
|
read -r _
|
2024-08-05 20:24:05 +01:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Display devices with numbers
|
2024-09-12 21:14:50 +01:00
|
|
|
i=1
|
2024-11-20 06:14:38 +00:00
|
|
|
echo "$devices" | while IFS= read -r device; do
|
2024-09-19 01:13:35 +01:00
|
|
|
printf "%d. %s\n" "$i" "$device"
|
2024-09-12 21:14:50 +01:00
|
|
|
i=$((i + 1))
|
2024-08-05 20:24:05 +01:00
|
|
|
done
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b\n" "0. Exit to main menu"
|
2024-11-19 18:59:19 +00:00
|
|
|
printf "%b" "$prompt_msg"
|
2024-09-19 22:47:08 +01:00
|
|
|
read -r choice
|
2024-08-05 20:24:05 +01:00
|
|
|
|
2024-11-20 06:14:38 +00:00
|
|
|
count=$(printf "%b" "$devices" | wc -l)
|
2024-11-19 18:59:19 +00:00
|
|
|
count=$((count + 1))
|
|
|
|
|
|
|
|
if echo "$choice" | grep -qE '^[0-9]+$' && [ -n "$choice" ] && [ "$choice" -le "$count" ] && [ "$choice" -gt 0 ]; then
|
2024-11-20 06:14:38 +00:00
|
|
|
device=$(echo "$devices" | sed -n "${choice}p")
|
2024-08-05 20:24:05 +01:00
|
|
|
mac=$(echo "$device" | awk '{print $2}')
|
2024-11-20 06:10:24 +00:00
|
|
|
if bluetoothctl info "$mac" >/dev/null 2>&1; then
|
2024-11-19 18:59:19 +00:00
|
|
|
if bluetoothctl "$command" "$mac"; then
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${GREEN}$success_msg${RC}"
|
2024-08-05 20:24:05 +01:00
|
|
|
break
|
2024-11-19 18:59:19 +00:00
|
|
|
else
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${RED}$failure_msg${RC}"
|
2024-11-19 18:59:19 +00:00
|
|
|
read -r _
|
|
|
|
fi
|
2024-08-05 20:24:05 +01:00
|
|
|
else
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${RED}Invalid MAC address. Please try again.${RC}"
|
2024-11-19 18:59:19 +00:00
|
|
|
read -r _
|
2024-08-05 20:24:05 +01:00
|
|
|
fi
|
|
|
|
elif [ "$choice" -eq 0 ]; then
|
|
|
|
return
|
|
|
|
else
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "${RED}Invalid choice. Please try again.${RC}"
|
2024-11-19 18:59:19 +00:00
|
|
|
read -r _
|
2024-08-05 20:24:05 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
pair_device() {
|
2024-11-19 18:59:19 +00:00
|
|
|
prompt_for_mac "pair" "Enter the number of the device to pair: " "Pairing with device completed." "Failed to pair with device."
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
connect_device() {
|
2024-11-19 18:59:19 +00:00
|
|
|
prompt_for_mac "connect" "Enter the number of the device to connect: " "Connecting to device completed." "Failed to connect to device."
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect_device() {
|
2024-11-19 18:59:19 +00:00
|
|
|
prompt_for_mac "disconnect" "Enter the number of the device to disconnect: " "Disconnecting from device completed." "Failed to disconnect from device."
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
remove_device() {
|
2024-11-19 18:59:19 +00:00
|
|
|
prompt_for_mac "remove" "Enter the number of the device to remove: " "Removing device completed." "Failed to remove device."
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
2024-08-15 06:15:12 +01:00
|
|
|
checkEnv
|
2024-09-17 13:23:23 +01:00
|
|
|
checkEscalationTool
|
2024-08-15 06:15:12 +01:00
|
|
|
setupBluetooth
|
2024-08-15 03:41:41 +01:00
|
|
|
main_menu
|