summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar/plugins/bluetooth.sh
blob: d8dd1b6ad499d32b01eb3ecd831035ccb93c0130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash

# Source colors for consistent theming
source "$CONFIG_DIR/colors.sh"

# Multiple methods to detect Bluetooth status
BT_ENABLED=false
AIRPODS_CONNECTED=false

# Method 1: Check using defaults (most reliable)
BT_STATE=$(defaults read /Library/Preferences/com.apple.Bluetooth ControllerPowerState 2>/dev/null)
if [[ "$BT_STATE" == "1" ]]; then
    BT_ENABLED=true
fi

# Method 2: Check using system_profiler with better parsing
if [[ "$BT_ENABLED" == false ]]; then
    BT_POWER=$(system_profiler SPBluetoothDataType 2>/dev/null | grep -i "state" | grep -i "on")
    if [[ -n "$BT_POWER" ]]; then
        BT_ENABLED=true
    fi
fi

# Method 3: Check if blueutil is available and use it (most accurate)
if command -v blueutil &> /dev/null; then
    BT_UTIL_STATE=$(blueutil -p 2>/dev/null)
    if [[ "$BT_UTIL_STATE" == "1" ]]; then
        BT_ENABLED=true
    elif [[ "$BT_UTIL_STATE" == "0" ]]; then
        BT_ENABLED=false
    fi
fi

# Method 4: Check if any Bluetooth devices are connected as fallback
if [[ "$BT_ENABLED" == false ]]; then
    BT_DEVICES=$(system_profiler SPBluetoothDataType 2>/dev/null | grep -i "connected: yes")
    if [[ -n "$BT_DEVICES" ]]; then
        BT_ENABLED=true
    fi
fi

# Check specifically for AirPods connection
if [[ "$BT_ENABLED" == true ]]; then
    # Method 1: Check for AirPods in connected devices (SPBluetoothDataType)
    AIRPODS_CHECK=$(system_profiler SPBluetoothDataType 2>/dev/null | grep -i -A 10 -B 2 "airpods\|air pods")
    if [[ -n "$AIRPODS_CHECK" ]]; then
        # Check if AirPods are actually connected (not just paired)
        AIRPODS_CONNECTED_CHECK=$(echo "$AIRPODS_CHECK" | grep -i "connected: yes")
        if [[ -n "$AIRPODS_CONNECTED_CHECK" ]]; then
            AIRPODS_CONNECTED=true
        fi
    fi
    
    # Method 2: Check if AirPods are active audio device (more reliable)
    if [[ "$AIRPODS_CONNECTED" == false ]]; then
        AIRPODS_AUDIO_CHECK=$(system_profiler SPAudioDataType 2>/dev/null | grep -i -A 5 -B 5 "airpods")
        if [[ -n "$AIRPODS_AUDIO_CHECK" ]]; then
            # Check if AirPods are set as default output device
            AIRPODS_DEFAULT_OUTPUT=$(echo "$AIRPODS_AUDIO_CHECK" | grep -i "default output device: yes")
            if [[ -n "$AIRPODS_DEFAULT_OUTPUT" ]]; then
                AIRPODS_CONNECTED=true
            fi
        fi
    fi
    
    # Method 3: Alternative check using blueutil if available
    if [[ "$AIRPODS_CONNECTED" == false ]] && command -v blueutil &> /dev/null; then
        CONNECTED_DEVICES=$(blueutil --connected 2>/dev/null)
        if [[ "$CONNECTED_DEVICES" == *"AirPods"* ]] || [[ "$CONNECTED_DEVICES" == *"Air Pods"* ]]; then
            AIRPODS_CONNECTED=true
        fi
    fi
fi

# Set icon and color based on connection status
if [[ "$AIRPODS_CONNECTED" == true ]]; then
    # AirPods connected - AirPods icon in white
    ICON="󰋋"
    COLOR=$ACCENT_PINK
elif [[ "$BT_ENABLED" == true ]]; then
    # Bluetooth enabled but no AirPods - blue Bluetooth icon
    ICON="󰂯"
    COLOR=$ACCENT_PRIMARY  # Blue
else
    # Bluetooth disabled - gray icon
    ICON="󰂲"
    COLOR=$GREY
fi

# Update the Bluetooth item - icon only, no label
sketchybar --set "$NAME" icon="$ICON" \
                        icon.color="$COLOR" \
                        label.drawing=off