Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
shell script for adb wifi
#!/bin/bash
#Modify this with your IP range
MY_IP_RANGE="192\.168\.1"
#You usually wouldn't have to modify this
PORT_BASE=5555
#List the devices on the screen for your viewing pleasure
adb devices
echo
#Find USB devices only (no emulators, genymotion or connected devices
declare -a deviceArray=(`adb devices -l | grep -v emulator | grep -v vbox | grep -v "${MY_IP_RANGE}" | grep " device " | awk '{print $1}'`)
echo "found ${#deviceArray[@]} device(s)"
echo
for index in ${!deviceArray[*]}
do
echo "finding IP address for device ${deviceArray[index]}"
IP_ADDRESS=$(adb -s ${deviceArray[index]} shell ifconfig wlan0 | awk '{print $3}')
echo "IP address found : $IP_ADDRESS "
echo "Connecting..."
adb -s ${deviceArray[index]} tcpip $(($PORT_BASE + $index))
adb -s ${deviceArray[index]} connect "$IP_ADDRESS:$(($PORT_BASE + $index))"
echo
echo
done
adb devices -l
#exit
@flyingzl

This comment has been minimized.

Copy link

@flyingzl flyingzl commented Jan 12, 2015

very cool. You can also install the app named "adb wireless", it's more easy and useful, and can be used out of box.

@jamescfli

This comment has been minimized.

Copy link

@jamescfli jamescfli commented Jul 31, 2015

This does not work for Moto G either.

The last two lines of the output show unauthorized:

...
List of devices attached 
192.168.6.110:5555     unauthorized
@jxllh123456

This comment has been minimized.

Copy link

@jxllh123456 jxllh123456 commented Sep 13, 2016

finding IP address for device 47Q6R16328002931
IP address found : 172.18.214.94
Connecting...

@jxllh123456

This comment has been minimized.

Copy link

@jxllh123456 jxllh123456 commented Sep 13, 2016

right ?

@meicuihui

This comment has been minimized.

Copy link

@meicuihui meicuihui commented Jun 24, 2017

good

@akidee

This comment has been minimized.

Copy link

@akidee akidee commented Aug 18, 2017

I updated your script to consider netcfg, ifconfig and currently connected ports: https://gist.github.com/akidee/04ade213c92f087b06920698190f71b5

@jpwiedekopf

This comment has been minimized.

Copy link

@jpwiedekopf jpwiedekopf commented Mar 24, 2020

Having reached this gist from Google, I adapted it for my needs.
In particular, the command in line 22 to find the IP failed for me on Android 10 due to a permission error. I suspect something changed that made ifconfig require elevated rights. With this solution from StackOverflow, I was able to get the script running by changing that line to this:

IP_ADDRESS=$(adb -s ${deviceArray[index]} shell "ip addr show wlan0 | grep -e wlan0$ | cut -d\" \" -f 6 | cut -d/ -f 1")

Also, I'd like to thank you for introducing me to the declare/array syntax, which I had never used before!

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