Sample Header Ad - 728x90

Why is the scanned bluetooth devices not showing on `Rofi` bluetooth menu using `bluetoothctl`?

0 votes
1 answer
326 views
Why is the rofi menu not showing available the bluetooth devices when it's enabled and scanned via bluetoothctl? Here is the script that is used to make a shortcut to open a rofi styled bluetooth menu:
#!/usr/bin/env bash

# Function to enable Bluetooth
enable_bluetooth() {
    sudo rfkill unblock bluetooth
    sudo bluetoothctl power on
    sleep 2  # Short delay to ensure Bluetooth is powered on
}

# Function to disable Bluetooth
disable_bluetooth() {
    sudo bluetoothctl power off
    sleep 2  # Short delay to ensure Bluetooth is powered off
}

# Function to scan for Bluetooth devices
scan_bluetooth_devices() {
    sudo bluetoothctl scan on &
    sleep 10  # Allow some time for scanning

    sudo bluetoothctl devices | while read -r line; do
        device_mac=$(echo "$line" | awk '{print $2}')
        device_name=$(echo "$line" | cut -d' ' -f3-)
        if sudo bluetoothctl info "$device_mac" | grep -q "Connected: yes"; then
            echo "  $device_name (connected)"
        else
            echo "  $device_name"
        fi
    done
}

# Ensure the bluetooth service is running
sudo systemctl start bluetooth

# Check the status of the bluetooth service
service_status=$(sudo systemctl is-active bluetooth)
if [[ "$service_status" != "active" ]]; then
    notify-send "Bluetooth Service" "Bluetooth service is not active. Please check the systemctl status."
    exit 1
fi

# Main menu loop
while true; do
    # Get Bluetooth power status
    bluetooth_status=$(sudo bluetoothctl show | grep "Powered:" | awk '{print $2}')
    if [[ "$bluetooth_status" == "yes" ]]; then
        toggle="󰂯  Disable Bluetooth"
    else
        toggle="󰂮  Enable Bluetooth"
    fi

    # Prompt user to select an option
    chosen_option=$(echo -e "$toggle\n󰍾  Scan for devices\n󰍃  Exit" | rofi -dmenu -i -selected-row 1 -p "Bluetooth Menu")

    # Handle user selection
    if [ -z "$chosen_option" ]; then
        exit
    elif [ "$chosen_option" = "󰂮  Enable Bluetooth" ]; then
        enable_bluetooth
    elif [ "$chosen_option" = "󰂯  Disable Bluetooth" ]; then
        disable_bluetooth
    elif [ "$chosen_option" = "󰍾  Scan for devices" ]; then
        device_list=$(scan_bluetooth_devices)
        chosen_device=$(echo -e "$device_list\n󰍃  Back" | rofi -dmenu -i -selected-row 1 -p "Available Devices: ")

        if [ "$chosen_device" = "󰍃  Back" ] || [ -z "$chosen_device" ]; then
            continue
        else
            # Extract MAC address and name of connection
            chosen_mac=$(echo "$chosen_device" | awk '{print $2}')
            chosen_name=$(echo "$chosen_device" | cut -d' ' -f3- | sed 's/ (connected)//')

            # Message to show when connection is activated successfully
            success_message="You are now connected to the Bluetooth device \"$chosen_name\"."

            # Check if the device is already connected
            if sudo bluetoothctl info "$chosen_mac" | grep -q "Connected: yes"; then
                sudo bluetoothctl disconnect "$chosen_mac" && notify-send "Bluetooth Disconnected" "You are now disconnected from the Bluetooth device \"$chosen_name\"."
            else
                sudo bluetoothctl connect "$chosen_mac" && notify-send "Bluetooth Connected" "$success_message"
            fi
        fi
    elif [ "$chosen_option" = "󰍃  Exit" ]; then
        exit
    fi
done
Asked by DarKnightz (61 rep)
Jun 23, 2024, 12:50 PM
Last activity: Jul 12, 2024, 08:43 PM