184 lines
6.1 KiB
Bash
184 lines
6.1 KiB
Bash
# shellcheck shell=bash
|
|
# Module file (no shebang). Bundled by build_bundle.sh
|
|
|
|
# -------------------------
|
|
# ADB wireless pair/connect wizard
|
|
# -------------------------
|
|
cleanup_offline_loopback() {
|
|
local keep_serial="$1" # e.g. 127.0.0.1:41313
|
|
local serial state rest
|
|
while read -r serial state rest; do
|
|
[[ -n "${serial:-}" ]] || continue
|
|
[[ "$serial" == ${HOST}:* ]] || continue
|
|
[[ "$state" == "offline" ]] || continue
|
|
[[ "$serial" == "$keep_serial" ]] && continue
|
|
adb disconnect "$serial" >/dev/null 2>&1 || true
|
|
done < <(adb devices 2>/dev/null | tail -n +2 | sed '/^[[:space:]]*$/d')
|
|
}
|
|
|
|
adb_pair_connect() {
|
|
need adb || die "Missing adb. Install: pkg install android-tools"
|
|
|
|
# Only require Termux:API when we will prompt the user
|
|
if [[ "$ONLY_CONNECT" != "1" || -z "${CONNECT_PORT:-}" ]]; then
|
|
termux_api_ready || die "Termux:API not ready."
|
|
fi
|
|
|
|
echo "[*] adb: $(adb version | head -n 1)"
|
|
adb start-server >/dev/null 2>&1 || true
|
|
|
|
if [[ "$ONLY_CONNECT" == "1" ]]; then
|
|
if [[ -n "$CONNECT_PORT" ]]; then
|
|
CONNECT_PORT="${CONNECT_PORT//[[:space:]]/}"
|
|
[[ "$CONNECT_PORT" =~ ^[0-9]{5}$ ]] || die "Invalid CONNECT PORT (must be 5 digits): '$CONNECT_PORT'"
|
|
else
|
|
echo "[*] Asking CONNECT PORT..."
|
|
CONNECT_PORT="$(ask_port_5digits connect "CONNECT PORT")" || die "Timeout waiting CONNECT PORT."
|
|
fi
|
|
|
|
local serial="${HOST}:${CONNECT_PORT}"
|
|
adb disconnect "$serial" >/dev/null 2>&1 || true
|
|
echo "[*] adb connect $serial"
|
|
adb connect "$serial" >/dev/null || die "adb connect failed to $serial. Verify Wireless debugging is enabled and CONNECT PORT is correct."
|
|
|
|
if [[ "$CLEANUP_OFFLINE" == "1" ]]; then
|
|
cleanup_offline_loopback "$serial"
|
|
fi
|
|
|
|
echo "[*] Devices:"
|
|
adb devices -l
|
|
|
|
echo "[*] ADB check (shell):"
|
|
adb -s "$serial" shell sh -lc 'echo "it worked: adb shell is working"; id' || true
|
|
|
|
cleanup_notif
|
|
ok "ADB connected (connect-only): $serial"
|
|
return 0
|
|
fi
|
|
|
|
if [[ -n "$CONNECT_PORT" ]]; then
|
|
CONNECT_PORT="${CONNECT_PORT//[[:space:]]/}"
|
|
[[ "$CONNECT_PORT" =~ ^[0-9]{5}$ ]] || die "Invalid --connect-port (must be 5 digits): '$CONNECT_PORT'"
|
|
else
|
|
echo "[*] Asking CONNECT PORT..."
|
|
CONNECT_PORT="$(ask_port_5digits connect "CONNECT PORT")" || die "Timeout waiting CONNECT PORT."
|
|
fi
|
|
|
|
echo "[*] Asking PAIR PORT..."
|
|
local pair_port
|
|
pair_port="$(ask_port_5digits pair "PAIR PORT")" || die "Timeout waiting PAIR PORT."
|
|
|
|
echo "[*] Asking PAIR CODE..."
|
|
local code
|
|
code="$(ask_code_6digits)" || die "Timeout waiting PAIR CODE."
|
|
|
|
local serial="${HOST}:${CONNECT_PORT}"
|
|
adb disconnect "$serial" >/dev/null 2>&1 || true
|
|
|
|
echo "[*] adb pair ${HOST}:${pair_port}"
|
|
printf '%s\n' "$code" | adb pair "${HOST}:${pair_port}" || die "adb pair failed. Verify PAIR PORT and PAIR CODE (and that the pairing dialog is showing)."
|
|
|
|
echo "[*] adb connect $serial"
|
|
adb connect "$serial" >/dev/null || die "adb connect failed after pairing. Re-check CONNECT PORT and Wireless debugging."
|
|
|
|
if [[ "$CLEANUP_OFFLINE" == "1" ]]; then
|
|
cleanup_offline_loopback "$serial"
|
|
fi
|
|
|
|
echo "[*] Devices:"
|
|
adb devices -l
|
|
|
|
echo "[*] ADB check (shell):"
|
|
adb -s "$serial" shell sh -lc 'echo "it worked: adb shell is working"; getprop ro.product.model; getprop ro.build.version.release' || true
|
|
|
|
cleanup_notif
|
|
ok "ADB connected: $serial"
|
|
}
|
|
|
|
# Return state for an exact serial (e.g. "device", "offline", empty)
|
|
adb_device_state() {
|
|
local s="$1"
|
|
adb devices 2>/dev/null | awk -v s="$s" 'NR>1 && $1==s {print $2; exit}'
|
|
}
|
|
|
|
# Return first loopback serial in "device" state (e.g. 127.0.0.1:41313)
|
|
adb_any_loopback_device() {
|
|
adb devices 2>/dev/null | awk -v h="$HOST" '
|
|
NR>1 && $2=="device" && index($1, h":")==1 {print $1; found=1; exit}
|
|
END { exit (found ? 0 : 1) }
|
|
'
|
|
}
|
|
|
|
# Pick the loopback serial we will operate on:
|
|
# - If CONNECT_PORT is set, require that exact HOST:PORT to be in "device" state.
|
|
# - Otherwise, return the first loopback device.
|
|
adb_pick_loopback_serial() {
|
|
if [[ -n "${CONNECT_PORT:-}" ]]; then
|
|
local p="${CONNECT_PORT//[[:space:]]/}"
|
|
[[ "$p" =~ ^[0-9]{5}$ ]] || return 1
|
|
local target="${HOST}:${p}"
|
|
[[ "$(adb_device_state "$target")" == "device" ]] && { echo "$target"; return 0; }
|
|
return 1
|
|
fi
|
|
adb_any_loopback_device
|
|
}
|
|
|
|
# If already connected, avoid re-pairing/re-connecting prompts (useful for --all),
|
|
# BUT only consider loopback/target connections as "already connected".
|
|
adb_pair_connect_if_needed() {
|
|
need adb || die "Missing adb. Install: pkg install android-tools"
|
|
adb start-server >/dev/null 2>&1 || true
|
|
|
|
local serial=""
|
|
|
|
# If user provided a connect-port, insist on that exact target serial.
|
|
if [[ -n "${CONNECT_PORT:-}" ]]; then
|
|
CONNECT_PORT="${CONNECT_PORT//[[:space:]]/}"
|
|
[[ "$CONNECT_PORT" =~ ^[0-9]{5}$ ]] || die "Invalid --connect-port (must be 5 digits): '$CONNECT_PORT'"
|
|
|
|
local target="${HOST}:${CONNECT_PORT}"
|
|
|
|
if [[ "$(adb_device_state "$target")" == "device" ]]; then
|
|
ok "ADB already connected to target: $target (skipping pair/connect)."
|
|
return 0
|
|
fi
|
|
|
|
# Try connect-only first (in case it was already paired before)
|
|
adb connect "$target" >/dev/null 2>&1 || true
|
|
if [[ "$(adb_device_state "$target")" == "device" ]]; then
|
|
ok "ADB connected to target: $target (connect-only succeeded; skipping pair)."
|
|
return 0
|
|
fi
|
|
|
|
# Not connected: run full wizard (pair+connect)
|
|
adb_pair_connect
|
|
return $?
|
|
fi
|
|
|
|
# No explicit port: only skip if we already have a loopback device connected.
|
|
if serial="$(adb_any_loopback_device 2>/dev/null)"; then
|
|
ok "ADB already connected (loopback): $serial (skipping pair/connect)."
|
|
return 0
|
|
fi
|
|
|
|
adb_pair_connect
|
|
}
|
|
|
|
require_adb_connected() {
|
|
need adb || { warn_red "Missing adb. Install: pkg install android-tools"; return 1; }
|
|
adb start-server >/dev/null 2>&1 || true
|
|
if ! adb_pick_loopback_serial >/dev/null 2>&1; then
|
|
warn_red "No ADB device connected."
|
|
warn "If already paired before: run --connect-only [PORT]."
|
|
warn "Otherwise: run --adb-only to pair+connect."
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
adb_loopback_serial_or_die() {
|
|
local s
|
|
s="$(adb_pick_loopback_serial 2>/dev/null)" || return 1
|
|
echo "$s"
|
|
}
|