2025-08-25 09:41:16 -06:00

223 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
# Auto-discover and connect to Android device over WiFi
# Configuration
PHONE_IP="10.0.0.193" # Your phone's IP (this usually stays the same)
RETRY_ATTEMPTS=5
RETRY_DELAY=2
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m'
# Function to find device port
find_device_port() {
echo -e "${CYAN}Scanning for ADB device on $PHONE_IP...${NC}"
# Common ADB wireless ports range
for port in {5555..5585} {37000..42000}; do
# Try to connect with timeout
timeout 0.5 bash -c "echo >/dev/tcp/$PHONE_IP/$port" 2>/dev/null
if [ $? -eq 0 ]; then
echo -e "${YELLOW}Found open port: $port${NC}"
# Try ADB connection
adb connect "$PHONE_IP:$port" 2>/dev/null | grep -q "connected"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Successfully connected to $PHONE_IP:$port${NC}"
return 0
fi
fi
done
return 1
}
# Function to enable wireless debugging via USB first (if needed)
setup_wireless_adb() {
echo -e "${YELLOW}Setting up wireless ADB...${NC}"
echo "1. Connect your phone via USB cable"
echo "2. Make sure USB debugging is enabled"
echo "Press Enter when ready..."
read
# Check if device is connected via USB
if adb devices | grep -q "device$"; then
echo -e "${GREEN}✓ Device found via USB${NC}"
# Set TCP/IP mode on port 5555
adb tcpip 5555
sleep 2
# Get device IP
DEVICE_IP=$(adb shell ip route | grep wlan0 | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
if [ ! -z "$DEVICE_IP" ]; then
echo -e "${GREEN}Device IP detected: $DEVICE_IP${NC}"
PHONE_IP=$DEVICE_IP
fi
echo "You can now disconnect the USB cable"
sleep 3
# Try to connect wirelessly
adb connect "$PHONE_IP:5555"
return 0
else
echo -e "${RED}No device found via USB${NC}"
return 1
fi
}
# Main auto-connect function
auto_connect() {
echo -e "${CYAN}=== ADB Auto-Connect ===${NC}"
# First, disconnect any existing connections
adb disconnect >/dev/null 2>&1
# Try to find and connect
for attempt in $(seq 1 $RETRY_ATTEMPTS); do
echo -e "${YELLOW}Connection attempt $attempt of $RETRY_ATTEMPTS${NC}"
if find_device_port; then
# Get the connected device
DEVICE_ID=$(adb devices | grep "$PHONE_IP" | awk '{print $1}')
echo -e "${GREEN}✓ Connected to device: $DEVICE_ID${NC}"
# Export for use in other scripts
export DEVICE_IP="$DEVICE_ID"
# Save to config file for other scripts
echo "DEVICE_IP=\"$DEVICE_ID\"" > ~/.adb_device_config
return 0
fi
if [ $attempt -lt $RETRY_ATTEMPTS ]; then
echo -e "${YELLOW}Retrying in $RETRY_DELAY seconds...${NC}"
sleep $RETRY_DELAY
fi
done
echo -e "${RED}Failed to auto-detect device${NC}"
echo "Would you like to set up wireless debugging? (y/n)"
read -r response
if [[ "$response" == "y" ]]; then
setup_wireless_adb
fi
return 1
}
# Quick connect function (tries last known device first)
quick_connect() {
if [ -f ~/.adb_device_config ]; then
source ~/.adb_device_config
echo -e "${CYAN}Trying last known device: $DEVICE_IP${NC}"
if adb connect "$DEVICE_IP" 2>/dev/null | grep -q "connected"; then
echo -e "${GREEN}✓ Quick connect successful!${NC}"
return 0
fi
fi
# Fall back to auto-discovery
auto_connect
}
# Main execution
main() {
clear
echo -e "${GREEN}==================================${NC}"
echo -e "${GREEN} ADB & Scrcpy Auto-Connect${NC}"
echo -e "${GREEN}==================================${NC}"
echo
# Try quick connect first, then full auto-discovery
if quick_connect; then
echo
echo -e "${CYAN}Launching scrcpy...${NC}"
# Ensure the connected device is stable and get its serial (explicit)
MAX_WAIT=20
WAITED=0
DEVICE_ID=""
while [ $WAITED -lt $MAX_WAIT ]; do
DEVICE_ID=$(adb devices | grep "$PHONE_IP" | awk '{print $1}' || true)
if [ -n "$DEVICE_ID" ]; then
# try a reconnect to make sure TCP session is ready
adb reconnect "$DEVICE_ID" >/dev/null 2>&1 || true
sleep 1
# verify still present
if adb devices | grep -q "^${DEVICE_ID}[[:space:]]*device$"; then
break
fi
fi
sleep 1
WAITED=$((WAITED+1))
done
if [ -z "$DEVICE_ID" ]; then
echo -e "${RED}✖ Device did not stabilize on ADB before launching scrcpy${NC}"
echo "Try: adb kill-server && adb start-server && adb connect $PHONE_IP:5555"
exit 1
fi
# Launch scrcpy with explicit device serial to avoid auto-detection races
# Allow extra args via SCRCPY_EXTRA_ARGS but strip any device-selector flags
SCRCPY_EXTRA_ARGS="${SCRCPY_EXTRA_ARGS:-"-w"}"
# remove -e, --select-tcpip and --tcpip=<addr> to avoid selector conflicts with -s
SANITIZED_ARGS=$(echo "$SCRCPY_EXTRA_ARGS" | sed -E 's/(^| )-e($| )/ /g; s/(^| )--select-tcpip($| )/ /g; s/(^| )--tcpip=[^ ]+($| )/ /g' | xargs)
scrcpy -s "$DEVICE_ID" $SANITIZED_ARGS &
SCRCPY_PID=$!
echo -e "${GREEN}✓ Scrcpy launched (PID: $SCRCPY_PID) for device $DEVICE_ID${NC}"
echo
echo "Device is ready for use!"
echo "Connection string: $(adb devices | grep $PHONE_IP | awk '{print $1}')"
# Option to update the SMS script
echo
echo "Update SMS script with new connection? (y/n)"
read -r update_sms
if [[ "$update_sms" == "y" ]]; then
update_sms_script
fi
else
echo -e "${RED}Failed to connect to device${NC}"
echo "Please check:"
echo " 1. Phone is connected to the same WiFi network"
echo " 2. Wireless debugging is enabled on the phone"
echo " 3. Phone IP is correct: $PHONE_IP"
exit 1
fi
}
# Function to update SMS script with new device connection
update_sms_script() {
SMS_SCRIPT="ui.sh" # Your SMS script name
if [ -f "$SMS_SCRIPT" ]; then
# Get current device
CURRENT_DEVICE=$(adb devices | grep "$PHONE_IP" | awk '{print $1}')
# Backup original
cp "$SMS_SCRIPT" "${SMS_SCRIPT}.backup"
# Update DEVICE_IP line in script
sed -i "s/^DEVICE_IP=.*/DEVICE_IP=\"$CURRENT_DEVICE\"/" "$SMS_SCRIPT"
echo -e "${GREEN}✓ Updated $SMS_SCRIPT with device: $CURRENT_DEVICE${NC}"
else
echo -e "${YELLOW}SMS script not found: $SMS_SCRIPT${NC}"
fi
}
# Run main function
main "$@"