Sometimes it is useful to grab a screenshot from your new app remotely through Wi-Fi. For example, you might be using sensors like the accelerometer and you would like to show the results while your moving. In this situation it might be more comfortable to move with the device without the attached USB cable.
The powerful ADB tool offers all features that are required to accomplish this. You only need to perform a couple of simple steps.
I'm already assuming that you enabled Developer options on your target device and you also authorized the development device. Additionally, you need to connect both devices to the same Wi-Fi network. Your firewall might interfere with adb communication, so you also need to make sure that it's correctly configured.
Tell ADB Daemon to Listen for TCP/IP Connection on Port 5555
First we want to tell the adbd daemon process on the target device to start to listen for TCP/IP connections. Of course, for this to work, your target device still needs to be connected through USB cable. You can disconnect the USB cable later, once we've got the IP address.
adb tcpip 5555
This will set the property service.adb.tcp.port on the target device to the given port. You can later check the configured port by printing this property with the getprop command.
Get the IP Address of the Target Device
adb shell ip -f inet addr show wlan0
This will give you the following output
24: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 3000 inet 192.168.0.220/24 brd 192.168.0.255 scope global wlan0 valid_lft forever preferred_lft forever
There you can see that my current Wi-Fi IP address is 192.168.0.220. Once you've got the IP address, you can disconnect the USB cable.
Connect to Target Device via TCP/IP
Use the IP address obtained in the previous step to connect to the target device.
adb connect 192.168.0.220
Grab a Screenshot
Once connection is established, you can use ADB as if it would be connected through USB cable. To grab a screenshot, execute the following
adb shell screencap -p /data/local/tmp/screenshot.png
This command will create a screenshot in png format and store it inside of /data/local/tmp folder on the target device.
Pull the Screenshot to Your Development Device
To copy the screenshot from target device to your development device, do the following
adb pull /data/local/tmp/screenshot.png
This should copy the file screenshot.png into your current working directory.
Grabbing a Screenshot Remotely with the Bugjaeger App
I think ADB is a really useful tool. Not only for Android developers. Therefore I decided to port it to Android. I embedded ADB into my mobile app - Bugjaeger. You can now connect one Android device running Bugjaeger to another Android device through USB OTG cable and use many of the ADB features directly from your phone.
To grab a screenshot remotely with Bugjaeger, do the following
- Connect your target device via USB OTG and authorize the device running Bugjaeger (I'm assuming you've already enabled Developer options on the target device)
- Switch to Commands tab in Bugjaeger app
- Execute Connect through WiFi command. This might already cause the target device to connect in one step. However, on many devices the connection is interrupted once you plug out USB OTG cable. Therefore you should also perform the next steps while keeping the USB cable connected.
Execute Get WiFi IP address command. This will show the Wi-Fi IP address once you swipe up to make the result window completely visible.
- Disconnect the USB OTG cable and click on the connect button next to the devices spinner
- Type in your IP address and tap on Connect. After connection is established, a new device should appear in devices spinner.
- Switch to Screencap tab and grab your screenshot remotely
In the video below I capture the screen of my old Asus Zenfone with Android 4.4. wirelessly to my Nexus 5X