Android Debugging over WiFi

In some situations, using ADB can be a little more difficult, than just connecting your phone via USB cable. This is the case, when your Android device has only one USB socket and you are using this USB socket to connect other USB devices (for instance a USB mouse or a flash drive). In such a case, your Android device will run in USB host mode, playing the role of a PC. If you want to do some on device debugging now, you need to use ADB over WiFi.

Requirements

  • A PC, a WiFi access point and a rooted Android device
  • Android Superuser and Terminal packages installed

WiFi instead of USB

Start off by connecting both your Android device and your PC to a WiFI access point. Now open the Android terminal application and run netcfg to find out the ip address that was assigned to your Android device.

*** Android on-device console ***
netcfg

You should now be able to ping your Android device. Replace x.x.x.x with the actual ip address of your Android device:

*** PC console ***
ping x.x.x.x

Now activate "ADB over Network". Cyanogenmod offers a developer friendly checkbox to let you do this. On a stock Android system you can do the same, by invoking the following commands:

*** Android on-device console ***
setprop service.adb.tcp.port 5555
stop adbd && start adbd

(Yes, it can be rather inconvenient to enter this on a mobile device. Please make sure to read "Creating Android script files" further below. Good news is: you will not need to use your on-device terminal again.)


In order to ADB connect over WiFi, run the following command. Always replace x.x.x.x with the actual ip address of your Android device:

*** PC console ***
adb connect x.x.x.x 

Please make sure to activate "Keep Wi-Fi on during sleep" (under Wi-Fi / Advanced).

Showing system logs

In order to show system logs, run the following command:

*** PC console ***
adb -s x.x.x.x:5555 logcat

Or, to get rid of some noise:

*** PC console ***
adb -s x.x.x.x:5555 logcat |grep -vE "/dalvik"

Showing kernel logs

Open a shell on your Android device:

*** PC console ***
adb -s x.x.x.x:5555 shell

On your PC, in the Android shell, enter these commands to print the kernel logs to the screen:

*** Android console inside PC console ***
su
cat /proc/kmsg |grep -vE "cpu|CPU|bq27541|temp="

This is it. This will print out the complete kernel log from start to now, just like dmesg does. CPU related messages are often not needed and will be discarded by the grep command. Other than dmesg, this command will continue to display all upcoming log messages.


Since this is being displayed in a console on your PC, you can easily insert empty lines by hitting CR in order to create some structure, in order to visually separate different activities from each other. And you can cut&paste interesting sections of the log for later analysis. Control-c will abort this procedure. You can start "cat /proc/kmsg ..." again at any time. It will then only print the newly added log entries.

Fetching old kernel log after reboot

After rebooting, connect your Android device to your PC via USB and start a regular ADB session:

*** PC console ***
adb shell 

Use the following commands to copy the previous session kernel log (from before rebooting) to the SD card on your Android device:

*** Android console inside PC console ***
su
cat /proc/last_kmsg > /sdcard/last_kmsg.txt
exit
exit

Pull the log file to your PC:

*** PC console ***
adb pull /sdcard/last_kmsg.txt .

Clean up the kernel log from some noise (Linux one-liner):

*** PC console ***
cat last_kmsg.txt |grep -vE "cpu_down|stop_machine_cpu_stop|cpu_up|shutdown|CPU|temperature|capacity|bq27541_get_psp|mtp_open|adb_release|mtp_release|adb_open|_bind_config|temp=|rt5640|sensor|cpuidle|sdhci|mmcblk|tegra_uart|nvavp|pn544|tegra-i2c" >last_kmsg_clean.txt

Creating Android script files

I created an "adbt" script file, so I can quickly invoke adb-over-tcp functionality from inside the Android terminal. You need to be superuser in order to create script files in the /system/bin folder.

*** PC console ***
adb -s x.x.x.x:5555 shell
*** Android console inside PC console ***
su
mount -o remount,rw -t yaffs2 /dev/block/mtdblock4 /system
echo "su -c 'setprop service.adb.tcp.port 5555 && stop adbd && start adbd'" > /system/bin/adbt
chmod 755 /system/bin/adbt
mount -o ro,remount -t yaffs2 /dev/block/mtdblock4 /system
sync

(Please note: These are 6 lines. The echo command is one long line.)