0-ENABLE-TCPIP.BAT 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Read ADB path from adb-path-config.js using PowerShell
  12. echo [0/3] Reading ADB path from adb-path-config.js...
  13. for /f "delims=" %%i in ('powershell -Command "$content = Get-Content '%PROJECT_ROOT%\adb-path-config.js' -Raw; if ($content -match '\"adb-path\"\s*:\s*''([^'']+)''') { $adbPath = $matches[1]; $fullPath = $adbPath.TrimEnd([char]92) + [char]92 + 'adb.exe'; if (Test-Path $fullPath) { Write-Output $fullPath } else { Write-Output 'adb' } } else { Write-Output 'adb' }"') do set "ADB_PATH=%%i"
  14. if "%ADB_PATH%"=="" set "ADB_PATH=adb"
  15. echo Using ADB: %ADB_PATH%
  16. echo.
  17. REM Check if port 5037 is in use and kill the process if needed
  18. echo [1/3] Checking ADB daemon port (5037)...
  19. set "PORT_FOUND=0"
  20. set "KILLED_PID="
  21. for /f "tokens=5" %%a in ('netstat -ano 2^>nul ^| findstr :5037') do (
  22. if !PORT_FOUND! equ 0 (
  23. set "PID=%%a"
  24. if not "!PID!"=="" (
  25. set "PORT_FOUND=1"
  26. set "KILLED_PID=!PID!"
  27. echo Port 5037 is in use by process ID: !PID!
  28. echo Attempting to kill process...
  29. taskkill /F /PID !PID! 2>nul
  30. if !errorlevel! neq 0 (
  31. echo Warning: Failed to kill process !PID!, may need administrator privileges.
  32. ) else (
  33. echo Process !PID! killed successfully.
  34. )
  35. goto :port_killed
  36. )
  37. )
  38. )
  39. :port_killed
  40. REM Kill all ADB processes regardless of port check
  41. echo Killing all ADB processes...
  42. taskkill /F /IM adb.exe 2>nul
  43. if !errorlevel! neq 0 (
  44. echo Warning: Failed to kill ADB processes, may need administrator privileges.
  45. echo Please run this script as Administrator.
  46. ) else (
  47. echo All ADB processes terminated.
  48. )
  49. REM Wait a bit for processes to fully terminate
  50. if !PORT_FOUND! equ 1 (
  51. timeout /t 2 /nobreak >nul
  52. ) else (
  53. timeout /t 1 /nobreak >nul
  54. )
  55. echo.
  56. echo [2/3] List connected devices...
  57. "%ADB_PATH%" devices
  58. echo.
  59. echo [3/3] Enable TCP/IP mode...
  60. "%ADB_PATH%" tcpip 5555
  61. echo.
  62. echo ========================================
  63. echo Done
  64. echo ========================================
  65. echo.
  66. pause