~kris/dots

srice

srice/.config/eww/scripts/scan_bluetooth.sh -rw-r--r-- 2.2 KiB
e98f3b03 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
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
#!/bin/bash

# Pastikan bluetooth aktif
if ! bluetoothctl show | grep -q "Powered: yes"; then
    echo "[]"
    exit 0
fi

# Scan devices (timeout 3 detik) - KURANGI dari 5 detik
timeout 3 bluetoothctl scan on &>/dev/null &
SCAN_PID=$!
sleep 3
kill $SCAN_PID 2>/dev/null
bluetoothctl scan off &>/dev/null  # Stop scan

# Function untuk escape JSON string dengan benar
json_escape() {
    # Encode ke base64 untuk avoid semua masalah encoding
    echo -n "$1" | base64 -w0
}

# Function untuk create JSON value dengan proper escaping
json_value() {
    local key="$1"
    local value="$2"
    printf '"%s":"%s"' "$key" "$(json_escape "$value")"
}

# Array untuk menyimpan JSON
devices_json="["
first=true

while read -r line; do
    mac=$(echo "$line" | awk '{print $2}')
    name=$(echo "$line" | cut -d' ' -f3-)
    
    # Skip jika nama kosong
    [ -z "$name" ] && continue
    
    # Check connected
    if bluetoothctl info "$mac" 2>/dev/null | grep -q "Connected: yes"; then
        connected="true"
    else
        connected="false"
    fi
    
    # Check paired
    if bluetoothctl info "$mac" 2>/dev/null | grep -q "Paired: yes"; then
        paired="true"
    else
        paired="false"
    fi
    
    # Get device type - GUNAKAN TEXT BIASA, bukan emoji
    dev_type=$(bluetoothctl info "$mac" 2>/dev/null | grep "Icon:" | awk '{print $2}')
    case "$dev_type" in
        *phone*|*mobile*) type_icon="phone";;
        *audio*|*headset*|*headphone*) type_icon="headphone";;
        *computer*) type_icon="computer";;
        *keyboard*) type_icon="keyboard";;
        *mouse*) type_icon="mouse";;
        *) type_icon="device";;
    esac
    
    # Escape name untuk JSON dengan proper method
    name_escaped=$(echo -n "$name" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g; s/\n/\\n/g; s/\r/\\r/g')
    
    # Build JSON - SIMPLE format tanpa icon emoji
    if [ "$first" = false ]; then
        devices_json+=","
    fi
    devices_json+="{\"name\":\"$name_escaped\",\"mac\":\"$mac\",\"connected\":$connected,\"paired\":$paired,\"type\":\"$type_icon\"}"
    first=false
done < <(bluetoothctl devices 2>/dev/null | head -20)  # Limit 20 devices

devices_json+="]"

# Output JSON
echo "$devices_json"