linutil/tabs/utils/bluetooth-control.sh

153 lines
4.7 KiB
Bash
Raw Normal View History

2024-08-15 03:41:41 +01:00
#!/bin/sh -e
2024-08-21 08:18:34 +01:00
. ../common-script.sh
2024-09-12 21:14:50 +01:00
# Function to check Bluez is installed
setupBluetooth() {
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}Installing Bluez...${RC}"
if ! command_exists bluetoothctl; then
2024-09-17 13:23:23 +01:00
case "$PACKAGER" in
pacman)
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez-utils
;;
*)
"$ESCALATION_TOOL" "$PACKAGER" install -y bluez
;;
esac
else
2024-09-12 21:14:50 +01:00
printf "%b\n" "${GREEN}Bluez is already installed.${RC}"
fi
# Check if bluetooth service is running
if ! systemctl is-active --quiet bluetooth; then
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}Bluetooth service is not running. Starting it now...${RC}"
"$ESCALATION_TOOL" systemctl start bluetooth
if systemctl is-active --quiet bluetooth; then
2024-09-12 21:14:50 +01:00
printf "%b\n" "${GREEN}Bluetooth service started successfully.${RC}"
fi
fi
}
# Function to display the main menu
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}"
printf "1. Scan for devices\n"
printf "2. Pair with a device\n"
printf "3. Connect to a device\n"
printf "4. Disconnect from a device\n"
printf "5. Remove a device\n"
printf "0. Exit\n"
printf -n "Choose an option: "
2024-09-12 21:14:50 +01:00
read choice
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}" ;;
esac
done
}
# Function to scan for devices
scan_devices() {
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}Scanning for devices...${RC}"
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}"
else
2024-09-12 21:14:50 +01:00
printf "%b\n" "${GREEN}Devices found:${RC}"
printf "%s\n" "$devices"
fi
printf "Press any key to return to the main menu..."
read -r dummy
}
# Function to prompt for MAC address using numbers
prompt_for_mac() {
2024-09-12 21:14:50 +01:00
action=$1
command=$2
prompt_msg=$3
success_msg=$4
failure_msg=$5
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}"
printf "Press any key to return to the main menu..."
read -r dummy
return
fi
# Display devices with numbers
2024-09-12 21:14:50 +01:00
device_list=$(echo "$devices" | tr '\n' '\n')
i=1
echo "$device_list" | while IFS= read -r device; do
printf "%d. %s\n" "$i" "$device"
2024-09-12 21:14:50 +01:00
i=$((i + 1))
done
printf "0. Exit to main menu\n"
printf "%s\n" "$prompt_msg"
2024-09-12 21:14:50 +01:00
read choice
# Validate the choice
2024-09-12 21:14:50 +01:00
if echo "$choice" | grep -qE '^[0-9]+$' && [ "$choice" -le "$((i - 1))" ] && [ "$choice" -gt 0 ]; then
device=$(echo "$device_list" | sed -n "${choice}p")
mac=$(echo "$device" | awk '{print $2}')
if bluetoothctl info "$mac" > /dev/null 2>&1; then
bluetoothctl $command "$mac" && {
2024-09-12 21:14:50 +01:00
printf "%b\n" "${GREEN}$success_msg${RC}"
break
} || {
2024-09-12 21:14:50 +01:00
printf "%b\n" "${RED}$failure_msg${RC}"
}
else
2024-09-12 21:14:50 +01:00
printf "%b\n" "${RED}Invalid MAC address. Please try again.${RC}"
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}"
fi
done
printf "Press any key to return to the main menu..."
read -r dummy
}
# Function to pair with a device
pair_device() {
prompt_for_mac "pair" "pair" "Enter the number of the device to pair: " "Pairing with device completed." "Failed to pair with device."
}
# Function to connect to a device
connect_device() {
prompt_for_mac "connect" "connect" "Enter the number of the device to connect: " "Connecting to device completed." "Failed to connect to device."
}
# Function to disconnect from a device
disconnect_device() {
prompt_for_mac "disconnect" "disconnect" "Enter the number of the device to disconnect: " "Disconnecting from device completed." "Failed to disconnect from device."
}
# Function to remove a device
remove_device() {
prompt_for_mac "remove" "remove" "Enter the number of the device to remove: " "Removing device completed." "Failed to remove device."
}
# Initialize
checkEnv
2024-09-17 13:23:23 +01:00
checkEscalationTool
setupBluetooth
2024-08-15 03:41:41 +01:00
main_menu