Wednesday, May 25, 2016

Android: WiFi ADB



It's great being able to debug code on a real live Android device. Typically, though, your device needs to be tethered to your host computer. Now ADB has a facility to connect wirelessly, so you can debug code on your device without a physical connection to it. To do this, you need to know the IP address of the device, and follow these steps:
  1. Connect the device to your computer via USB; make sure ADB can see it (ie, make sure it shows up in the command 'adb devices').
  2. Enter the command 'adb tcpip 5037'
  3. Enter the command 'adb connect <ip address>:5037'
You're now free to disconnect your device. Go ahead and disconnect the device, and you'll still see it in the response to 'adb devices', and you can use all the other adb commands as well (shell, push/pull, etc).

Here's a Ruby script that'll do all this for you:

wifiadb.rb
# get the IP address of the device
if ARGV.length == 0
port = 5037
print "PORT not specified: defaulting to 5037\n"
else
port = ARGV[0]
end
wlan0 = `adb shell netcfg | find /i "wlan0"`[/(\d{1,3}\.){3}\d{1,3}/]
printf "The device's IP address is " + wlan0 + "\n"
system("adb tcpip " + port.to_s)
system("adb connect " + wlan0 + ":" + port.to_s)


Now all you need to do is run wifiadb.rb. It'll figure out your device's IP address and run the necessary commands.

For more information:
Android Debug Bridge: https://developer.android.com/studio/command-line/adb.html
Ruby: https://www.ruby-lang.org/en/