-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun-ScrcpyWireless.bat
More file actions
275 lines (240 loc) · 10.1 KB
/
Run-ScrcpyWireless.bat
File metadata and controls
275 lines (240 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
@echo off
setlocal enabledelayedexpansion
title SCRCPY Wireless Launcher
color 0A
:: Set color codes for Windows 10/11
set "CYAN=[36m"
set "GREEN=[32m"
set "YELLOW=[33m"
set "RED=[31m"
set "MAGENTA=[35m"
set "GRAY=[37m"
set "RESET=[0m"
echo.
echo %CYAN%==================================================================%RESET%
echo %CYAN% %RESET%
echo %GREEN% SCRCPY WIRELESS LAUNCHER %RESET%
echo %YELLOW% Version 1.0.0 %RESET%
echo %CYAN% %RESET%
echo %MAGENTA% Author: netphantom.og %RESET%
echo %CYAN% %RESET%
echo %CYAN%==================================================================%RESET%
echo.
:: Resolve base directory and scrcpy tool directory relative to this script
set "baseDir=%~dp0"
set "scrcpyDir=%baseDir%scrcpy"
:: Check required files
set "adbPath=%scrcpyDir%\adb.exe"
set "scrcpyPath=%scrcpyDir%\scrcpy.exe"
if not exist "%adbPath%" (
echo %RED%[ERROR] Missing file: adb.exe%RESET%
echo %YELLOW%Expected at: %adbPath%%RESET%
echo.
echo %CYAN%[SOLUTION] You need to download official scrcpy binaries:%RESET%
:: Detect system architecture
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
echo %GRAY%1. Download (64-bit): https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-win64-v2.4.0.zip%RESET%
) else (
echo %GRAY%1. Download (32-bit): https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-win32-v2.4.0.zip%RESET%
)
echo %GRAY%2. Extract and copy all files to the 'scrcpy' folder%RESET%
echo %GRAY%3. See README.md for detailed instructions%RESET%
echo.
pause
exit /b 1
)
if not exist "%scrcpyPath%" (
echo %RED%[ERROR] Missing file: scrcpy.exe%RESET%
echo %YELLOW%Expected at: %scrcpyPath%%RESET%
echo.
echo %CYAN%[SOLUTION] You need to download official scrcpy binaries:%RESET%
:: Detect system architecture
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
echo %GRAY%1. Download (64-bit): https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-win64-v2.4.0.zip%RESET%
) else (
echo %GRAY%1. Download (32-bit): https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-win32-v2.4.0.zip%RESET%
)
echo %GRAY%2. Extract and copy all files to the 'scrcpy' folder%RESET%
echo %GRAY%3. See README.md for detailed instructions%RESET%
echo.
pause
exit /b 1
)
:: Create Desktop shortcut if missing
echo %CYAN%[INFO] Checking for desktop shortcut...%RESET%
set "shortcutName=SCRCPY Wireless Launcher.lnk"
set "userDesktop=%USERPROFILE%\Desktop"
set "shortcutPath=%userDesktop%\%shortcutName%"
if not exist "%shortcutPath%" (
echo %CYAN%[INFO] Creating desktop shortcut...%RESET%
powershell -Command "$wsh = New-Object -ComObject WScript.Shell; $sc = $wsh.CreateShortcut('%shortcutPath%'); $sc.TargetPath = 'cmd.exe'; $sc.Arguments = '/k \"cd /d \"%baseDir%\" ^& \"%baseDir%Run-ScrcpyWireless.bat\"\"'; $sc.WorkingDirectory = '%baseDir%'; $iconPath = '%baseDir%scrcpy.ico'; if (Test-Path $iconPath) { $sc.IconLocation = $iconPath }; $sc.Save()" 2>nul
)
:: Initialize adb server (do not kill to preserve existing wireless sessions)
echo %CYAN%[INFO] Starting ADB server...%RESET%
"%adbPath%" start-server >nul 2>&1
:: Detect existing TCP/IP device first (reconnect without USB)
echo %CYAN%[INFO] Checking for existing wireless connections...%RESET%
"%adbPath%" devices > "%temp%\adb_devices.txt" 2>&1
set "serial="
set "foundWireless="
set "readyToLaunch="
for /f "tokens=1,2" %%a in ('"%adbPath%" devices ^| findstr /r ".*:5555.*device"') do (
set "serial=%%a"
set "foundWireless=1"
echo %GREEN%[INFO] Using existing wireless device %%a%RESET%
"%adbPath%" connect %%a >nul 2>&1
timeout /t 2 /nobreak >nul
:: Verify connection is still active
"%adbPath%" devices > "%temp%\adb_devices_check.txt" 2>&1
findstr /r "%%a.*device" "%temp%\adb_devices_check.txt" >nul
if not errorlevel 1 (
echo %GREEN%[SUCCESS] Wireless connection verified%RESET%
set "readyToLaunch=1"
goto :launch_scrcpy
) else (
echo %YELLOW%[WARNING] Existing wireless connection is inactive, will re-establish%RESET%
set "foundWireless="
set "serial="
set "readyToLaunch="
)
)
:: If no TCP/IP serial, try USB path
if not defined foundWireless (
echo %CYAN%[INFO] Checking for USB devices...%RESET%
for /f "tokens=1,2" %%a in ('"%adbPath%" devices ^| findstr /r ".*device$"') do (
set "serial=%%a"
echo %GREEN%[INFO] USB device detected: %%a%RESET%
goto :enable_wireless
)
)
:: No devices found
echo %YELLOW%[WARNING] No device detected over USB or wireless.%RESET%
echo %GRAY%➡ If you used this before, just ensure the phone stayed on the same WiFi and try again.%RESET%
echo %GRAY%➡ Otherwise, connect via USB once to initialize wireless mode.%RESET%
echo %GRAY%➡ Make sure USB Debugging is enabled on your phone.%RESET%
pause
exit /b 1
:enable_wireless
:: Enable TCP/IP mode on port 5555
echo %CYAN%[INFO] Enabling TCP/IP mode (port 5555)...%RESET%
"%adbPath%" tcpip 5555 >nul 2>&1
echo %YELLOW%[INFO] Device switching to TCP/IP mode...%RESET%
timeout /t 3 /nobreak >nul
:: Get device IP using improved strategies
echo %CYAN%[INFO] Detecting phone IP address...%RESET%
set "ip="
:: Method 1: Try Android properties first (most reliable)
echo %CYAN%[INFO] Trying Android properties method...%RESET%
for %%p in (dhcp.wlan0.ipaddress dhcp.eth0.ipaddress dhcp.wlan1.ipaddress) do (
if not defined ip (
for /f %%a in ('"%adbPath%" shell getprop %%p 2^>nul') do (
echo %%a | findstr /r "^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$" >nul && (
set "ip=%%a"
echo %GREEN%[INFO] IP found via property %%p: %%a%RESET%
goto :found_ip
)
)
)
)
:: Method 2: Try different network interfaces
if not defined ip (
echo %CYAN%[INFO] Trying network interface method...%RESET%
for %%i in (wlan0 eth0 wlan1 rmnet_data0) do (
if not defined ip (
for /f "tokens=2 delims= " %%a in ('"%adbPath%" shell ip -f inet addr show %%i 2^>nul ^| findstr /r "inet [0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"') do (
set "ip=%%a"
echo %GREEN%[INFO] IP found via interface %%i: %%a%RESET%
goto :found_ip
)
)
)
)
:: Method 3: Try route-based detection
if not defined ip (
echo %CYAN%[INFO] Trying route-based method...%RESET%
for /f "tokens=2 delims= " %%a in ('"%adbPath%" shell ip route 2^>nul ^| findstr /r "src [0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"') do (
set "ip=%%a"
echo %GREEN%[INFO] IP found via route: %%a%RESET%
goto :found_ip
)
)
:: Method 4: Try compact addr list
if not defined ip (
echo %CYAN%[INFO] Trying compact address method...%RESET%
for /f "tokens=3 delims= " %%a in ('"%adbPath%" shell ip -o -4 addr show 2^>nul ^| findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"') do (
set "ip=%%a"
set "ip=!ip:/=!"
echo %GREEN%[INFO] IP found via compact method: %%a%RESET%
goto :found_ip
)
)
:: Method 5: Try to ping common network ranges
if not defined ip (
echo %CYAN%[INFO] Trying network discovery method...%RESET%
for /f "tokens=2 delims= " %%a in ('"%adbPath%" shell "for i in 192.168.1 192.168.0 10.0.0 172.16.0; do ping -c 1 -W 100 $i.1 >/dev/null 2>&1 && echo $i.1 && break; done" 2^>nul') do (
set "ip=%%a"
echo %GREEN%[INFO] IP found via network discovery: %%a%RESET%
goto :found_ip
)
)
:found_ip
if not defined ip (
echo %RED%[ERROR] Could not detect phone IP address.%RESET%
echo %GRAY%➡ Ensure your phone is connected to WiFi.%RESET%
echo %GRAY%➡ Try reconnecting USB and running again.%RESET%
echo %GRAY%➡ Check if your phone's WiFi is enabled.%RESET%
del "%temp%\adb_devices_check.txt" 2>nul
pause
exit /b 1
)
echo %GREEN%[INFO] Phone IP detected: %ip%%RESET%
:: Connect wirelessly
set "serial=%ip%:5555"
echo %CYAN%[INFO] Connecting wirelessly to %serial%...%RESET%
"%adbPath%" connect %serial% > "%temp%\adb_connect.txt" 2>&1
:: Wait a moment for connection to establish
timeout /t 3 /nobreak >nul
:: Check if connection succeeded
"%adbPath%" devices > "%temp%\adb_devices_final.txt" 2>&1
findstr /r ".*:5555.*device" "%temp%\adb_devices_final.txt" >nul
if errorlevel 1 (
echo %RED%[ERROR] Could not connect wirelessly.%RESET%
echo %GRAY%➡ Ensure PC and phone are on the same WiFi network.%RESET%
echo %GRAY%➡ Reconnect USB once and re-run this script.%RESET%
echo %GRAY%➡ Check firewall settings.%RESET%
type "%temp%\adb_connect.txt"
del "%temp%\adb_connect.txt" 2>nul
del "%temp%\adb_devices_final.txt" 2>nul
del "%temp%\adb_devices_check.txt" 2>nul
pause
exit /b 1
)
echo %GREEN%[SUCCESS] Wireless connection established!%RESET%
set "readyToLaunch=1"
del "%temp%\adb_connect.txt" 2>nul
del "%temp%\adb_devices_final.txt" 2>nul
del "%temp%\adb_devices_check.txt" 2>nul
:launch_scrcpy
:: Launch scrcpy only if we have a valid connection
if not defined readyToLaunch (
echo %RED%[ERROR] No valid connection established%RESET%
pause
exit /b 1
)
echo %GREEN%[INFO] Starting scrcpy...%RESET%
echo.
echo %YELLOW%[NOTE] If you restart your PC or kill ADB,%RESET%
echo %YELLOW%you must reconnect your phone via USB once to restore wireless mode.%RESET%
echo.
echo %RED%Do not close this window.!!!%RESET%
echo.
echo %YELLOW%Wireless connection is now active and stable.%RESET%
echo.
echo %GREEN%Use the Desktop shortcut 'SCRCPY Wireless Launcher' for everyday use. Happy journey!%RESET%
echo.
:: Launch scrcpy with the detected serial
"%scrcpyPath%" -s "%serial%" --verbosity=error
:: Keep window open if scrcpy exits
echo.
echo %YELLOW%[NOTE] scrcpy has exited. You can close this window.%RESET%
pause