-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadb-connect2.sh
More file actions
executable file
·200 lines (166 loc) · 7.39 KB
/
adb-connect2.sh
File metadata and controls
executable file
·200 lines (166 loc) · 7.39 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bash
set -euo pipefail
shopt -s lastpipe
# ────────────────────────────────────────────
# 🎨 Helper: Styling
# ────────────────────────────────────────────
info() { printf "\033[1;34mℹ️ %s\033[0m\n" "$*"; }
success() { printf "\033[1;32m✅ %s\033[0m\n" "$*"; }
warn() { printf "\033[1;33m⚠️ %s\033[0m\n" "$*"; }
error() { printf "\033[1;31m❌ %s\033[0m\n" "$*"; }
section() {
printf "\n\033[1;35m────────────────────────────────────────────\033[0m\n"
printf "📟 \033[1;1mADB + Expo Device Manager (WSL Safe)\033[0m\n"
printf "\033[1;35m────────────────────────────────────────────\033[0m\n"
}
divider() { printf "\033[1;35m────────────────────────────────────────────\033[0m\n"; }
require_cmd() {
command -v "$1" >/dev/null 2>&1 || { error "Missing required command: $1"; exit 1; }
}
# ────────────────────────────────────────────
# 🧠 Detect USB & emulator devices
# ────────────────────────────────────────────
USB_DEVICES=()
EMU_DEVICES=()
ALL_DEVICES=()
get_devices() {
USB_DEVICES=()
EMU_DEVICES=()
# Drop the header line safely; do NOT let grep/sed failure trip set -e
local adb_out
adb_out="$(adb devices -l 2>/dev/null | sed '1d' || true)"
while IFS= read -r line; do
# Skip empty/whitespace-only lines
[[ -z "${line//[[:space:]]/}" ]] && continue
# serial + status are always first two fields
local serial status rest model device_name entry
serial="$(awk '{print $1}' <<<"$line" || true)"
status="$(awk '{print $2}' <<<"$line" || true)"
[[ -z "$serial" || -z "$status" ]] && continue
# Everything after the 2nd field
rest="$(cut -d' ' -f3- <<<"$line" 2>/dev/null || true)"
# Extract model:XYZ if present (no grep -P dependency)
model="$(sed -n 's/.*model:\([^[:space:]]*\).*/\1/p' <<<"$rest" | head -n1 || true)"
device_name="${model:-$serial}"
entry="$serial::$status::$device_name"
if [[ "$serial" == emulator-* ]]; then
EMU_DEVICES+=("$entry")
else
USB_DEVICES+=("$entry")
fi
done <<<"$adb_out"
}
# ────────────────────────────────────────────
# 🔐 Ensure ADB server is running
# ────────────────────────────────────────────
start_adb_if_needed() {
section
require_cmd adb
require_cmd npx
info "Checking ADB server..."
# Starting the server is safe even if it is already running
adb start-server >/dev/null 2>&1 || true
success "ADB server is responsive."
}
# ────────────────────────────────────────────
# 📱 Device Selector Prompt
# ────────────────────────────────────────────
SELECTED_SERIAL=""
SELECTED_NAME=""
SELECTED_STATUS=""
prompt_device_choice() {
divider
info "Select a device to launch Expo:"
local i serial status name icon
for i in "${!ALL_DEVICES[@]}"; do
IFS='::' read -r serial status name <<<"${ALL_DEVICES[$i]}"
icon="📱"
[[ "$serial" == emulator-* ]] && icon="🖥️"
printf " %d) %s %s (%s) [%s]\n" "$((i + 1))" "$icon" "$serial" "$name" "$status"
done
local choice_raw choice_idx max="${#ALL_DEVICES[@]}"
while true; do
read -rp "🚀 Enter choice [1-${max}]: " choice_raw
[[ "$choice_raw" =~ ^[0-9]+$ ]] || { warn "Enter a number from 1 to ${max}."; continue; }
(( choice_raw >= 1 && choice_raw <= max )) || { warn "Out of range. Enter 1..${max}."; continue; }
choice_idx=$((choice_raw - 1))
IFS='::' read -r SELECTED_SERIAL SELECTED_STATUS SELECTED_NAME <<<"${ALL_DEVICES[$choice_idx]}"
break
done
}
# ────────────────────────────────────────────
# ⏳ Wait for device to be online (with timeout if available)
# ────────────────────────────────────────────
wait_for_selected_device() {
divider
info "Waiting for ${SELECTED_SERIAL} to be online..."
if command -v timeout >/dev/null 2>&1; then
# adb syntax: adb -s SERIAL wait-for-device
timeout 90s adb -s "$SELECTED_SERIAL" wait-for-device || {
error "Timed out waiting for ${SELECTED_SERIAL}."
exit 1
}
else
adb -s "$SELECTED_SERIAL" wait-for-device
fi
# Confirm state
local state
state="$(adb -s "$SELECTED_SERIAL" get-state 2>/dev/null || true)"
if [[ "$state" != "device" ]]; then
warn "Device state is '${state:-unknown}'. If this is 'unauthorized', accept the RSA prompt on the device."
else
success "${SELECTED_SERIAL} is online."
fi
}
# ────────────────────────────────────────────
# 🚀 Launch Expo with selected device
# ────────────────────────────────────────────
launch_expo() {
divider
printf "🎯 Selected: \033[1m%s (%s)\033[0m [%s]\n" "$SELECTED_SERIAL" "$SELECTED_NAME" "$SELECTED_STATUS"
# Optional cleanup: try to kill a known zombie emulator-5554 if present
if adb devices 2>/dev/null | awk '{print $1,$2}' | grep -qE '^emulator-5554[[:space:]]+offline$' ; then
warn "Attempting to kill zombie emulator: emulator-5554"
adb -s emulator-5554 emu kill >/dev/null 2>&1 || true
fi
wait_for_selected_device
divider
info "Launching Expo on ${SELECTED_SERIAL}..."
export EXPO_ANDROID_DEVICE_ID="$SELECTED_SERIAL"
# Use expo run:android targeting the chosen device
npx expo run:android --device "$SELECTED_SERIAL"
}
# ────────────────────────────────────────────
# 🧠 Main logic
# ────────────────────────────────────────────
start_adb_if_needed
divider
info "Detecting connected devices..."
get_devices
if [[ "${#USB_DEVICES[@]}" -eq 0 ]]; then
warn "No USB devices found."
else
info "USB Devices detected:"
for dev in "${USB_DEVICES[@]}"; do
IFS='::' read -r serial status name <<<"$dev"
printf " 📱 %s [%s]\n" "$serial" "$status"
done
fi
divider
if [[ "${#EMU_DEVICES[@]}" -eq 0 ]]; then
warn "No emulators running."
else
info "Available Android Virtual Devices:"
for dev in "${EMU_DEVICES[@]}"; do
IFS='::' read -r serial status name <<<"$dev"
printf " 🖥️ %s [%s]\n" "$serial" "$status"
done
fi
ALL_DEVICES=("${USB_DEVICES[@]}" "${EMU_DEVICES[@]}")
if [[ "${#ALL_DEVICES[@]}" -eq 0 ]]; then
divider
error "No available devices to select."
exit 1
fi
prompt_device_choice
launch_expo