Skip to content

Instantly share code, notes, and snippets.

@SIDOVSKY
Last active February 15, 2021 22:15
Show Gist options
  • Save SIDOVSKY/d6d50e40bf368a15ddfd21218f860c33 to your computer and use it in GitHub Desktop.
Save SIDOVSKY/d6d50e40bf368a15ddfd21218f860c33 to your computer and use it in GitHub Desktop.
Find connected devices and establish ADB over WiFi with the selected one

PLATFORM

In order for these scripts to work adb must be reachable in the shell. If it's not then add adb location to PATH. On Windows you may also place adb.exe, AdbWinApi.dll, AdbWinUsbApi.dll to the same folder with the script.

On MacOS to open the script with double click it should be saved with .command extension.

Example:

image

@echo off
setlocal EnableDelayedExpansion
set devices=
for /F "skip=1 tokens=1" %%F in ('adb devices') do (set devices=%%F;!devices!)
set deviceCount=0
call:arraySize "%devices%" deviceCount
IF "%deviceCount%"=="0" (
echo No adb devices found :(
echo Press any key to exit...
pause >nul
EXIT
)
IF "%deviceCount%"=="1" (
echo Found 1 device: %devices%
call:connectDevice %devices%
goto:eof
)
echo Avaliable devices for connecting:
set i=0
for %%a in (%devices%) do (
echo !i!. %%a
set /a i+=1
)
echo/
set /p "deviceNumber=Choose device number to start: "
set i=0
for %%a in (%devices%) do (
if "!i!"=="%deviceNumber%" (
call:connectDevice %%a
goto:eof
)
set /a i+=1
)
echo No device selected. Press any key to exit...
pause >nul
:: Function section
goto:eof
::
:connectDevice
:: -- %~1: deviceId
set deviceId=%~1
echo Searching for an IP address of %deviceId%...
set "ip=none"
FOR /F "tokens=*" %%F IN ('adb -s %deviceId% shell "ifconfig | grep -A 1 wlan0 | tail -n 1 | cut -f2 -d: | cut -f1 -d' '"') DO (SET ip=%%F)
IF [%ip%]==[none] (
echo No IP found.
) ELSE (
echo Found IP %ip%, connecting...
adb -s %deviceId% tcpip 5555
adb connect %ip%:5555
adb -s %deviceId% wait-for-device
)
echo/
adb devices
echo Press any key to exit...
pause >nul
GOTO:EOF
:arraySize
:: -- %~1: array
:: -- %~2: array size output
SETLOCAL
set i=0
for %%a in (%~1) do (set /a i+=1)
(ENDLOCAL
IF "%~2" NEQ "" SET %~2=%i%
)
GOTO:EOF
connect () {
device_id=$1
echo "Searching for an IP address of $device_id..."
ip=$(adb -s $device_id shell "ifconfig | grep -A 1 wlan0 | tail -n 1 | cut -f2 -d: | cut -f1 -d' '")
if [ $ip ]; then
echo "Found IP $ip, connecting..."
adb -s $device_id tcpip 5555
adb connect $ip:5555
adb -s $device_id wait-for-device
echo
adb devices
else
echo "No IP found."
fi
}
devices=( $(adb devices | awk 'NR > 1 {print $1}') )
if [ ${#devices[@]} -eq 0 ]; then
echo 'No adb devices found :('
exit
elif [ ${#devices[@]} -eq 1 ]; then
device_id=${devices[0]}
echo "Found 1 device: $device_id"
connect $device_id
else
echo 'Avaliable devices for connecting:'
select device_id in ${devices[@]}; do
if [ $device_id ]; then
connect $device_id
break;
fi
done
fi
read -rsn1 -p "Press any key to exit..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment