Merge ba69ca9aa58864d88407dfbc9d4dea6c80817e30 into a6ccaf4a4ee664835f476c8ac4742b5077e4a03a

This commit is contained in:
Jeevitha Kannan K S 2025-02-02 04:53:45 +00:00 committed by GitHub
commit ff8b3d00ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 26 deletions

View File

@ -3,13 +3,12 @@
. ../common-script.sh . ../common-script.sh
. ../common-service-script.sh . ../common-service-script.sh
# Function to check Bluez is installed
setupBluetooth() { setupBluetooth() {
printf "%b\n" "${YELLOW}Installing Bluez...${RC}" printf "%b\n" "${YELLOW}Installing Bluez...${RC}"
if ! command_exists bluetoothctl; then if ! command_exists bluetoothctl; then
case "$PACKAGER" in case "$PACKAGER" in
pacman) pacman)
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez-utils "$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez bluez-utils
;; ;;
apk) apk)
"$ESCALATION_TOOL" "$PACKAGER" add bluez "$ESCALATION_TOOL" "$PACKAGER" add bluez
@ -25,7 +24,6 @@ setupBluetooth() {
startService bluetooth startService bluetooth
} }
# Function to display the main menu
main_menu() { main_menu() {
while true; do while true; do
clear clear
@ -52,7 +50,6 @@ main_menu() {
done done
} }
# Function to scan for devices
scan_devices() { scan_devices() {
clear clear
printf "%b\n" "${YELLOW}Scanning for devices...${RC}" printf "%b\n" "${YELLOW}Scanning for devices...${RC}"
@ -68,12 +65,11 @@ scan_devices() {
read -r _ read -r _
} }
# Function to prompt for MAC address using numbers
prompt_for_mac() { prompt_for_mac() {
command=$2 command=$1
prompt_msg=$3 prompt_msg=$2
success_msg=$4 success_msg=$3
failure_msg=$5 failure_msg=$4
while true; do while true; do
clear clear
@ -86,19 +82,20 @@ prompt_for_mac() {
fi fi
# Display devices with numbers # Display devices with numbers
device_list=$(echo "$devices" | tr '\n' '\n')
i=1 i=1
for device in $device_list; do for device in $device_list; do
printf "%d. %s\n" "$i" "$device" printf "%d. %s\n" "$i" "$device"
i=$((i + 1)) i=$((i + 1))
done done
printf "%b\n" "0. Exit to main menu" printf "%b\n" "0. Exit to main menu"
printf "%b\n" "$prompt_msg" printf "%b" "$prompt_msg"
read -r choice read -r choice
# Validate the choice count=$(printf "%b" "$devices" | wc -l)
if echo "$choice" | grep -qE '^[0-9]+$' && [ "$choice" -le "$((i - 1))" ] && [ "$choice" -gt 0 ]; then count=$((count + 1))
device=$(echo "$device_list" | sed -n "${choice}p")
if echo "$choice" | grep -qE '^[0-9]+$' && [ -n "$choice" ] && [ "$choice" -le "$count" ] && [ "$choice" -gt 0 ]; then
device=$(echo "$devices" | sed -n "${choice}p")
mac=$(echo "$device" | awk '{print $2}') mac=$(echo "$device" | awk '{print $2}')
if bluetoothctl info "$mac" > /dev/null 2>&1; then if bluetoothctl info "$mac" > /dev/null 2>&1; then
if bluetoothctl "$command" "$mac"; then if bluetoothctl "$command" "$mac"; then
@ -106,41 +103,37 @@ prompt_for_mac() {
break break
else else
printf "%b\n" "${RED}$failure_msg${RC}" printf "%b\n" "${RED}$failure_msg${RC}"
read -r _
fi fi
else else
printf "%b\n" "${RED}Invalid MAC address. Please try again.${RC}" printf "%b\n" "${RED}Invalid MAC address. Please try again.${RC}"
read -r _
fi fi
elif [ "$choice" -eq 0 ]; then elif [ "$choice" -eq 0 ]; then
return return
else else
printf "%b\n" "${RED}Invalid choice. Please try again.${RC}" printf "%b\n" "${RED}Invalid choice. Please try again.${RC}"
read -r _
fi fi
done done
printf "%b" "Press any key to return to the main menu..."
read -r _
} }
# Function to pair with a device
pair_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." prompt_for_mac "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() { connect_device() {
prompt_for_mac "connect" "connect" "Enter the number of the device to connect: " "Connecting to device completed." "Failed to connect to device." prompt_for_mac "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() { disconnect_device() {
prompt_for_mac "disconnect" "disconnect" "Enter the number of the device to disconnect: " "Disconnecting from device completed." "Failed to disconnect from device." prompt_for_mac "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() { remove_device() {
prompt_for_mac "remove" "remove" "Enter the number of the device to remove: " "Removing device completed." "Failed to remove device." prompt_for_mac "remove" "Enter the number of the device to remove: " "Removing device completed." "Failed to remove device."
} }
# Initialize
checkEnv checkEnv
checkEscalationTool checkEscalationTool
setupBluetooth setupBluetooth