Skip to content

Instantly share code, notes, and snippets.

@smonev
Last active October 17, 2025 00:06
Show Gist options
  • Select an option

  • Save smonev/9664467c835ac861684f20ea3c61afdf to your computer and use it in GitHub Desktop.

Select an option

Save smonev/9664467c835ac861684f20ea3c61afdf to your computer and use it in GitHub Desktop.
quick check and fix for common Bluetooth issues (Cline / Claude Sonnet 4.5)
#!/bin/bash
# Bluetooth Diagnostic Script
# Checks common Bluetooth issues and provides quick diagnostics
# Parse command line arguments
VERBOSE=false
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-h|--help)
echo "Usage: $0 [-v|--verbose] [-h|--help]"
echo " -v, --verbose Show detailed command output"
echo " -h, --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use -h or --help for usage information"
exit 1
;;
esac
done
# Verbose output function
log_verbose() {
if [ "$VERBOSE" = true ]; then
echo -e " ${BLUE}[DEBUG]${NC} $1"
fi
}
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Icons
CHECK="✓"
CROSS="✗"
WARN="⚠"
INFO="ℹ"
# Check for required commands
log_verbose "Checking for required commands..."
REQUIRED_COMMANDS=("systemctl" "bluetoothctl" "rfkill" "lsmod" "lsusb" "journalctl")
MISSING_COMMANDS=()
for cmd in "${REQUIRED_COMMANDS[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
MISSING_COMMANDS+=("$cmd")
else
log_verbose "Found command: $cmd"
fi
done
if [ ${#MISSING_COMMANDS[@]} -gt 0 ]; then
echo -e "${RED}${CROSS}${NC} Missing required commands: ${MISSING_COMMANDS[*]}"
echo -e "${YELLOW}${WARN}${NC} Install missing packages and try again"
echo -e "${INFO} For Debian/Ubuntu: sudo apt install bluez rfkill usbutils systemd"
exit 1
fi
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE} Bluetooth Diagnostic Tool${NC}"
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}\n"
# 1. Check Bluetooth Service
echo -e "${BLUE}[1] Bluetooth Service Status${NC}"
log_verbose "Checking Bluetooth service with timeout..."
if timeout 5 systemctl is-active --quiet bluetooth 2>/dev/null; then
echo -e " ${GREEN}${CHECK}${NC} Service is running"
else
if systemctl status bluetooth &>/dev/null; then
echo -e " ${RED}${CROSS}${NC} Service is NOT running or unresponsive"
echo -e " ${YELLOW}${WARN}${NC} Try: sudo systemctl restart bluetooth"
else
echo -e " ${RED}${CROSS}${NC} Service is NOT running"
echo -e " ${YELLOW}${WARN}${NC} Try: sudo systemctl start bluetooth"
fi
fi
echo ""
# 2. Check for Bluetooth Controller
echo -e "${BLUE}[2] Bluetooth Controller${NC}"
log_verbose "Querying bluetoothctl for controller info..."
CONTROLLER_OUTPUT=$(timeout 5 bluetoothctl show 2>&1)
if [ $? -eq 124 ]; then
echo -e " ${RED}${CROSS}${NC} Cannot communicate with Bluetooth daemon (timeout)"
echo -e " ${YELLOW}${WARN}${NC} Try: sudo systemctl restart bluetooth && sleep 2"
else
CONTROLLER=$(echo "$CONTROLLER_OUTPUT" | grep "Controller" | head -1)
if [ -n "$CONTROLLER" ]; then
echo -e " ${GREEN}${CHECK}${NC} Controller detected"
echo -e " ${INFO} $CONTROLLER"
# Check if powered
POWERED=$(echo "$CONTROLLER_OUTPUT" | grep "Powered:" | awk '{print $2}')
if [ "$POWERED" = "yes" ]; then
echo -e " ${GREEN}${CHECK}${NC} Controller is powered on"
else
echo -e " ${YELLOW}${WARN}${NC} Controller is powered off"
echo -e " ${YELLOW}${WARN}${NC} Try: bluetoothctl power on"
fi
else
echo -e " ${RED}${CROSS}${NC} No controller available"
echo -e " ${YELLOW}${WARN}${NC} This usually means hardware is not detected"
echo -e " ${INFO} Check dmesg for hardware detection: dmesg | grep -i bluetooth"
fi
fi
echo ""
# 3. Check rfkill status
echo -e "${BLUE}[3] Radio Kill Switch Status${NC}"
log_verbose "Checking rfkill status..."
if ! command -v rfkill &> /dev/null; then
echo -e " ${YELLOW}${WARN}${NC} rfkill tool not available"
echo -e " ${INFO} Install with: sudo apt install rfkill"
else
RFKILL_OUTPUT=$(rfkill list bluetooth 2>/dev/null)
if [ -z "$RFKILL_OUTPUT" ]; then
echo -e " ${YELLOW}${WARN}${NC} No Bluetooth device found in rfkill"
echo -e " ${INFO} This might indicate a driver or hardware issue"
echo -e " ${INFO} Check dmesg for hardware detection: dmesg | grep -i bluetooth"
else
echo "$RFKILL_OUTPUT" | grep -q "Soft blocked: yes"
if [ $? -eq 0 ]; then
echo -e " ${RED}${CROSS}${NC} Bluetooth is SOFT BLOCKED"
echo -e " ${YELLOW}${WARN}${NC} Try: sudo rfkill unblock bluetooth"
else
echo -e " ${GREEN}${CHECK}${NC} Not soft blocked"
fi
echo "$RFKILL_OUTPUT" | grep -q "Hard blocked: yes"
if [ $? -eq 0 ]; then
echo -e " ${RED}${CROSS}${NC} Bluetooth is HARD BLOCKED (hardware switch)"
echo -e " ${YELLOW}${WARN}${NC} Check physical WiFi/BT switch on laptop"
else
echo -e " ${GREEN}${CHECK}${NC} Not hard blocked"
fi
fi
fi
echo ""
# 4. Check Bluetooth kernel modules
echo -e "${BLUE}[4] Bluetooth Kernel Modules${NC}"
log_verbose "Checking kernel modules..."
MODULES=("bluetooth" "btusb" "btmtk")
MISSING_MODS=()
for mod in "${MODULES[@]}"; do
if lsmod | grep -q "^$mod"; then
echo -e " ${GREEN}${CHECK}${NC} $mod module loaded"
log_verbose "$mod module is active"
else
echo -e " ${YELLOW}${WARN}${NC} $mod module NOT loaded"
MISSING_MODS+=("$mod")
fi
done
if [ ${#MISSING_MODS[@]} -gt 0 ]; then
echo -e "\n ${INFO} Try loading missing modules:"
for mod in "${MISSING_MODS[@]}"; do
echo -e " sudo modprobe $mod"
done
fi
echo ""
# 5. Check USB Bluetooth device
echo -e "${BLUE}[5] USB Bluetooth Hardware${NC}"
USB_BT=$(lsusb | grep -i -E "bluetooth|mediatek.*wireless")
if [ -n "$USB_BT" ]; then
echo -e " ${GREEN}${CHECK}${NC} USB Bluetooth device found:"
echo " $USB_BT"
# Check for MediaTek device specifically
if echo "$USB_BT" | grep -q "0e8d:e616"; then
echo -e "\n ${INFO} MediaTek MT7922 detected"
# Check power management
DEVICE_PATH=$(for device in /sys/bus/usb/devices/*; do
if [ -f "$device/idVendor" ] && [ "$(cat $device/idVendor)" = "0e8d" ] && [ "$(cat $device/idProduct)" = "e616" ]; then
echo "$device"
break
fi
done)
if [ -n "$DEVICE_PATH" ]; then
POWER_CONTROL=$(cat "$DEVICE_PATH/power/control" 2>/dev/null)
if [ "$POWER_CONTROL" = "on" ]; then
echo -e " ${GREEN}${CHECK}${NC} USB autosuspend is DISABLED (good)"
else
echo -e " ${YELLOW}${WARN}${NC} USB autosuspend is ENABLED (may cause issues)"
echo -e " ${YELLOW}${WARN}${NC} Try: echo 'on' | sudo tee $DEVICE_PATH/power/control"
fi
fi
fi
else
echo -e " ${RED}${CROSS}${NC} No USB Bluetooth device found"
echo -e " ${YELLOW}${WARN}${NC} Hardware may be disabled or malfunctioning"
fi
echo ""
# 6. Check recent Bluetooth errors
echo -e "${BLUE}[6] Recent Bluetooth Errors${NC}"
log_verbose "Checking system logs for Bluetooth errors..."
ERRORS=$(journalctl -b -u bluetooth --since "10 minutes ago" 2>&1 | grep -v "permission denied" | grep -i "error\|fail\|unable" | tail -5)
JOURNAL_EXIT=$?
if [ $JOURNAL_EXIT -ne 0 ] || echo "$ERRORS" | grep -q "Failed to query"; then
echo -e " ${YELLOW}${WARN}${NC} Cannot read logs (may need sudo)"
echo -e " ${INFO} Try: sudo journalctl -b -u bluetooth --since '10 minutes ago'"
elif [ -z "$ERRORS" ]; then
echo -e " ${GREEN}${CHECK}${NC} No recent errors in logs"
else
echo -e " ${YELLOW}${WARN}${NC} Recent errors found:"
echo "$ERRORS" | while read line; do
echo " $line"
done
fi
echo ""
# 7. Summary and recommendations
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE} Summary & Quick Fixes${NC}"
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}"
# Determine overall status
if [ -n "$CONTROLLER" ] && systemctl is-active --quiet bluetooth; then
echo -e "${GREEN}Overall Status: Bluetooth appears to be working${NC}\n"
else
echo -e "${RED}Overall Status: Bluetooth has issues${NC}\n"
echo -e "${YELLOW}Quick fix attempts (run in order):${NC}"
echo " 1. Restart Bluetooth service:"
echo " sudo systemctl restart bluetooth"
echo ""
echo " 2. Reload Bluetooth drivers:"
echo " sudo modprobe -r btusb && sudo modprobe btusb"
echo ""
echo " 3. For MediaTek MT7922 (if it disappeared):"
echo " sudo modprobe -r mt7921e && sudo modprobe mt7921e && sleep 2 && sudo modprobe -r btusb && sudo modprobe btusb"
echo ""
echo " 4. Last resort - reboot system"
echo ""
fi
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment