Skip to content

Instantly share code, notes, and snippets.

@jpmcosta
Created September 2, 2020 13:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmcosta/aced6a1ac03e61a7b0fa29082d0b51ce to your computer and use it in GitHub Desktop.
Save jpmcosta/aced6a1ac03e61a7b0fa29082d0b51ce to your computer and use it in GitHub Desktop.
ADB over WiFi Batch Script
@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
@jpmcosta
Copy link
Author

jpmcosta commented Sep 2, 2020

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:

  1. checks for a connected device over WiFi
  2. if no devices are connected, tries to connect to a device using the latest cached IP for your current network
  3. if it cannot connect to a device using the cached IP, it:
    1. sets up your ADB connection over WiFi, using the device connected by USB
    2. caches your device's IP and your current network SSID

Add the script to the list of Startup Tasks on Android Studio:

  1. Settings > Tools > Startup Tasks > + > Add New Configuration > Shell Script
  2. Name: adb_wifi
  3. Script path: <path to adb_wifi.bat>
  4. Apply or OK

Notes:

  1. The script creates a file named "adb_wifi_ips" to cache your device's IPs and network SSIDs
  2. Make sure "adb.exe" is on "%ANDROID_HOME%\platform-tools" directory
    • If it isn't, you can pass its directory as argument. Example: adb_wifi.bat c:\android-sdk\platform-tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment