Created
September 2, 2020 13:12
-
-
Save jpmcosta/aced6a1ac03e61a7b0fa29082d0b51ce to your computer and use it in GitHub Desktop.
ADB over WiFi Batch Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal | |
rem Change to suit your configuration. | |
set netmask=192.168 | |
rem Set adb. | |
if [%1] == [] ( | |
set adb=%ANDROID_HOME%\platform-tools\adb.exe | |
) else ( | |
set adb=%1%\adb.exe | |
) | |
rem Check if we are already connected. | |
%adb% devices | find "%netmask%" > nul | |
if %errorlevel% EQU 0 goto end | |
rem Find network ssid to get cached ip. | |
set tmp_file=%~dp0adb_wifi_tmp | |
netsh wlan show interface > %tmp_file% | |
for /F "tokens=2 delims=: " %%a in ('findstr /r "Profile" %tmp_file%') do ( | |
set ssid=%%a | |
) | |
del %tmp_file% | |
rem Find ip entry for current ssid in ips file. | |
set ips_file_name=adb_wifi_ips | |
set ips_file=%~dp0%ips_file_name% | |
for /F %%a in ('findstr /r %ssid% %ips_file%') do ( | |
set ip_entry=%%a | |
) | |
if [%ip_entry%] == [] goto connect | |
rem Get ip from ip entry. | |
for %%a in (%ip_entry::= %) do ( | |
set ip=%%a | |
) | |
rem Remove ip_entry from ips file. | |
findstr /v %ip_entry% %ips_file% > %tmp_file% | |
del %ips_file% | |
rename %tmp_file% %ips_file_name% | |
rem Try to connect and save ip_entry (ssid:ip) on success. | |
%adb% connect %ip% | |
%adb% devices | find "%netmask%" > nul | |
if %errorlevel% EQU 0 goto save_ip_entry | |
:connect | |
rem Find the ip of the connected device (should be connected by USB). | |
%adb% -d shell ip -o a > %tmp_file% | |
for /F "tokens=4 delims=/ " %%a in ('findstr /r "%netmask%.[0-9][0-9]*\.[0-9][0-9]*/" %tmp_file%') do ( | |
set ip=%%a | |
) | |
del %tmp_file% | |
rem Try to connect to device using the ip found. | |
%adb% -d tcpip 5555 | |
%adb% connect %ip% | |
rem Cache the found ip if we were able to connect. | |
%adb% devices | find "%netmask%" > nul | |
if %errorlevel% equ 0 goto save_ip_entry | |
:end | |
exit /b %errorlevel | |
:save_ip_entry | |
echo.%ssid%:%ip% >> %ips_file% | |
exit /b %errorlevel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ADB over WiFi Batch Script
A simple batch script to manage some of the workflow needed to debug your Android app over WiFi.
Once the ADB connection over WiFi is set up, it is possible to connect to your device as long as you don't restart it.
Workflow:
Add the script to the list of Startup Tasks on Android Studio:
<path to adb_wifi.bat>
Notes:
adb_wifi.bat c:\android-sdk\platform-tools