0-ENABLE-TCPIP.BAT 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. @echo off
  2. chcp 65001 >nul
  3. setlocal enabledelayedexpansion
  4. echo ========================================
  5. echo Enable ADB TCP/IP Mode (Port 5555)
  6. echo ========================================
  7. echo.
  8. REM Get the directory where this batch file is located
  9. set "SCRIPT_DIR=%~dp0"
  10. set "PROJECT_ROOT=%SCRIPT_DIR%.."
  11. REM Use ADB from project root adb-tools directory
  12. set "ADB_PATH=%PROJECT_ROOT%\adb-tools\adb.exe"
  13. REM Check if local ADB exists, otherwise fallback to system PATH
  14. if not exist "%ADB_PATH%" (
  15. echo Warning: Local ADB not found at %ADB_PATH%
  16. echo Falling back to system PATH...
  17. set "ADB_PATH=adb"
  18. )
  19. echo [0/3] Using ADB: %ADB_PATH%
  20. echo.
  21. REM Check if port 5037 is in use and kill the process if needed
  22. echo [1/3] Checking ADB daemon port (5037)...
  23. set "PORT_FOUND=0"
  24. set "KILLED_PID="
  25. for /f "tokens=5" %%a in ('netstat -ano 2^>nul ^| findstr :5037') do (
  26. if !PORT_FOUND! equ 0 (
  27. set "PID=%%a"
  28. if not "!PID!"=="" (
  29. set "PORT_FOUND=1"
  30. set "KILLED_PID=!PID!"
  31. echo Port 5037 is in use by process ID: !PID!
  32. echo Attempting to kill process...
  33. taskkill /F /PID !PID! 2>nul
  34. if !errorlevel! neq 0 (
  35. echo Warning: Failed to kill process !PID!, may need administrator privileges.
  36. ) else (
  37. echo Process !PID! killed successfully.
  38. )
  39. goto :port_killed
  40. )
  41. )
  42. )
  43. :port_killed
  44. REM Kill all ADB processes regardless of port check
  45. echo Killing all ADB processes...
  46. taskkill /F /IM adb.exe 2>nul
  47. if !errorlevel! neq 0 (
  48. echo Warning: Failed to kill ADB processes, may need administrator privileges.
  49. echo Please run this script as Administrator.
  50. ) else (
  51. echo All ADB processes terminated.
  52. )
  53. REM Wait a bit for processes to fully terminate
  54. if !PORT_FOUND! equ 1 (
  55. timeout /t 2 /nobreak >nul
  56. ) else (
  57. timeout /t 1 /nobreak >nul
  58. )
  59. echo.
  60. echo [2/3] List connected devices...
  61. "%ADB_PATH%" devices
  62. echo.
  63. echo [3/3] Enable TCP/IP mode...
  64. "%ADB_PATH%" tcpip 5555
  65. echo.
  66. echo ========================================
  67. echo Done
  68. echo ========================================
  69. echo.
  70. pause