@echo off
chcp 65001 >nul
echo ========================================
echo Test ADB Connection
echo ========================================
echo.
echo Please enter the last two digits of the IP address:
set /p IP_LAST_TWO="Last two digits (e.g., 0.15): "
if "%IP_LAST_TWO%"=="" (
    echo Error: IP address cannot be empty
    pause
    exit /b 1
)
set DEVICE_IP=192.168.%IP_LAST_TWO%
set DEVICE_PORT=5555
set DEVICE_ADDRESS=%DEVICE_IP%:%DEVICE_PORT%
echo.
echo Connecting to %DEVICE_ADDRESS%...
echo.

echo [1/5] Check if ADB is available...
adb version >nul 2>&1
if errorlevel 1 (
    echo Error: ADB not installed or not in PATH
    echo Please ensure Android SDK Platform Tools is installed
    pause
    exit /b 1
)
echo ADB is available
echo.

echo [2/5] Test network connectivity (ping %DEVICE_IP%)...
ping -n 2 %DEVICE_IP% >nul 2>&1
if errorlevel 1 (
    echo Cannot ping %DEVICE_IP%
    echo   Possible reasons: Phone and computer not on same network, or phone is off
    echo.
) else (
    echo Network connectivity OK
    echo.
)

echo [3/5] Check if port %DEVICE_PORT% is open...
powershell -Command "Test-NetConnection -ComputerName %DEVICE_IP% -Port %DEVICE_PORT% -InformationLevel Quiet" >nul 2>&1
if errorlevel 1 (
    echo Port %DEVICE_PORT% may not be open or blocked by firewall
    echo.
) else (
    echo Port %DEVICE_PORT% is accessible
    echo.
)

echo [4/5] Disconnect old connection (if any)...
adb disconnect %DEVICE_ADDRESS% >nul 2>&1
timeout /t 1 /nobreak >nul

echo Attempting to connect %DEVICE_ADDRESS%...
adb connect %DEVICE_ADDRESS%
set CONNECT_RESULT=%ERRORLEVEL%

echo.
if %CONNECT_RESULT% equ 0 (
    echo Connection command completed
    echo.
    timeout /t 2 /nobreak >nul
) else (
    echo.
    echo Connection failed (Error code: %CONNECT_RESULT%)
    echo.
    echo ========================================
    echo Troubleshooting Steps:
    echo ========================================
    echo 1. Enable "Developer Options" on phone
    echo    - Settings ^> About phone ^> Tap "Build number" 7 times
    echo.
    echo 2. Enable "USB debugging"
    echo    - Settings ^> Developer options ^> USB debugging
    echo.
    echo 3. Enable "Wireless debugging" or "Network ADB debugging"
    echo    - Settings ^> Developer options ^> Wireless debugging
    echo    - Or: Settings ^> Developer options ^> Network ADB debugging
    echo    - Confirm port is %DEVICE_PORT%
    echo.
    echo 4. If phone doesn't have "Wireless debugging" option, try:
    echo    a) Connect phone to computer via USB
    echo    b) Run: adb tcpip 5555
    echo    c) Unplug USB, then run this script again
    echo.
    echo 5. Check if phone and computer are on same Wi-Fi network
    echo.
    echo 6. Disable phone firewall or allow ADB through firewall
    echo.
)

echo [5/5] Check device list...
echo.
adb devices
echo.

echo ========================================
echo Test completed
echo ========================================
pause

