Tuesday, 19 August 2014

Automating ADB over Wi-Fi for multiple devices

Here is a script that will scan through your Android devices that are connected to ADB via a USB cable and then connect them all via ADB over Wi-Fi.

Usage is very simple.  If needed, modify the script with your IP range and then run the script. You will need to run this script every time you want to connect to your Wi-Fi network.

Note that I haven't yet found a way to test if a device is already connected via Wi-Fi so you may get errors such as "unable to connect to".  You'll know that the device is already connected if you see the double port value at the end.

Thanks to +Tony Owen for the original idea and to +Wolfram Rittmeyer with the help on the script.

Please feel free to expand this script or modify it. Let me know if you do so that I can update this post!

 #Modify this with your IP range  
 #You usually wouldn't have to modify this  
 #List the devices on the screen for your viewing pleasure  
 adb devices  
 #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)"  
 for index in ${!deviceArray[*]}  
   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))"  
 adb devices -l