#!/bin/bash set -e PHONE_IP="${PHONE_IP:-}" ADB_PORT="${ADB_PORT:-5555}" adb start-server if [ -n "$PHONE_IP" ]; then DEVICE="${PHONE_IP}:${ADB_PORT}" S="-s $DEVICE" echo "TCP/IP mode: connecting to $DEVICE..." ATTEMPT=1 while [ $ATTEMPT -le 10 ]; do adb connect "$DEVICE" 2>&1 if adb $S get-state 2>/dev/null | grep -q device; then break; fi echo " attempt $ATTEMPT/10 failed, retrying in 3s..." sleep 3 ATTEMPT=$((ATTEMPT + 1)) done if ! adb $S get-state 2>/dev/null | grep -q device; then echo "ERROR: Could not connect to $DEVICE after 10 attempts" exit 1 fi else S="-d" echo "USB mode: waiting for device..." ATTEMPT=1 while [ $ATTEMPT -le 10 ]; do if adb $S get-state 2>/dev/null | grep -q device; then SERIAL=$(adb devices | awk '!/List/ && /device$/ {print $1; exit}') echo "Found device: $SERIAL" S="-s $SERIAL" break fi echo " attempt $ATTEMPT/10..." sleep 1 ATTEMPT=$((ATTEMPT + 1)) done if ! adb $S get-state 2>/dev/null | grep -q device; then echo "ERROR: No USB device found after 10 attempts" echo "Check USB cable and that USB debugging is enabled on the phone." exit 1 fi fi echo "Applying non-root settings..." adb $S shell settings put global wifi_sleep_policy 2 || echo " (skipped - wifi_sleep_policy)" adb $S shell svc power stayon usb || echo " (skipped - svc power stayon)" adb $S shell dumpsys deviceidle disable || echo " (skipped - deviceidle disable)" echo "Starting background reconnect loop (every 30s)..." ( while true; do sleep 30 if ! adb $S get-state 2>/dev/null | grep -q device; then echo "$(date) Device disconnected, reconnecting..." if [ -n "$PHONE_IP" ]; then adb connect "${PHONE_IP}:${ADB_PORT}" 2>&1 fi fi done ) & echo "Starting Appium on 0.0.0.0:4723..." exec appium --address 0.0.0.0 --port 4723 --allow-cors --relaxed-security