Tuesday, 16 July 2013

ADB Over WiFi.

ADB Over WiFi

Are you an Android developer ? If yes, you certainly use ADB (Android Debug Bridge) to do various things like:

  • Start a shell on the target device.
  • Install/Uninstall some packages/ apps.
  • Monitor the state of the device (memory used, cpu used etc.)
  • Push/Pull some files to/from the target device.
  • Debug Android applications.
Most developers connect their device to their computer using a USB cable and that sucks because:
  • It is not ergonomic.
  • It is ugly.
  • You probably need that cable to plug your device to an electric outlet while you are at work.
  • You may not have a USB cable at all!
Anyway, for those of you who want to connect to your device wirelessly using ADB, I have some good news for you: it is possible and it is easy.
First, let’s talk about how ADB works and then let’s dig into that very straightforward solution (or if you are busy just jump to the second paragraph).
How does ADB work?
ADB is a tool that includes three components:
  • A daemon: It is a background process hosted by the target device.
  • A client: It runs on your development machine. This is where you will be issuing your commands.
  • A server: It acts as a Proxy between the client and the daemon. It runs as a background process on machine.
As you can guess, there is a two way communication going on between the server and the daemon. That communication happens over a medium which can be of two sorts: USB or TCP. Aha, you get it know? If we want to connect to our device over wifi then we just have to switch the transport mode from USB to TCP. Let’s do that!
The solution: Switching ADB transport mode from USB to TCPIP
  1. Before switching to TCP, we’ll need to grab the IP address of the device. To do that, we can either go to the phone settings or use the command line tool netcfg
    view sourceprint?
    adb shell netcfg
  2. Once you are in possession of the device’s IP address (let’s say it’s: 192.168.65.150), you can switch to TCP mode by doing: 
    view sourceprint?
    adb tcpip
     One thing to remember is that you can only choose a port number within the range of [5555...5585] (the reason being that ADB Server only works with these ports). So for instance you could do: 
    view source
    print?
    adb tcpip 5555
  3. Stop ADB Server (we’ll restart it later by issuing any ADB command): 
    view sourceprint?
    adb kill-server
  4. Set ADBHOST to your IP address: 
    view sourceprint?
    ADBHOST=192.168.65.150 adb devices
  5. Make sure your device is connected
the last part of the last command should print the device that you are trying to connect to.
What to do if you don’t have a USB cable from the beginning? (rooting required)
If you don’t have a USB cable at first, then you need to root your device. (can void warranty)
Than run the following command on your device: 
      setprop service.adb.tcp.port 5555
Now you need to stop/restart the adb daemon: 
      stop adbd
      start adbd
How to switch back to USB transport mode? 
      adb usb
But there is one caveat
This only works since Android 4.0 (ICS). If you have to develop/hack on other Android versions then you need to root that device (in order to use ADB in unsecure mode).
That’s all folks, I hope that you are now using debug bridge over the air :)