NOTE: for below article, replace IP address whereever required with your IP address
If you want to connect your mobile/device over wifi adb and access the shell, by default most of the mobiles will not be able to connect, but if you are actively working with some development platform, it might have adb working properly over network, in both of the scenarios, use below mentioned commands to connect adb over network.
If you are using a android development environment device or a rooted device which allows to do certain experiments, in those adb might be already enabled over network,
1. Check the IP address of the device, for this, make sure you have your device connected to network, then goto Settings -> Network & Internet -> click on connected network interface ( Wifi/Ethernet ) -> Select Again -> This will show the device IP address.
OR you can connect device over USB cable and type,
$ adb shell
$ ifconfig
Note the IP address and then exit from USB adb and remove USB cable. Now, connect over network using IP address as,
$ adb connect 192.168.0.104:5555
connected to 192.168.0.104:5555
$ adb shell
Sometimes, you may seen an error as below,
$ adb connect 192.168.0.104
unable to connect to 192.168.0.104:5555
For this, we need to restart the TCP port using USB adb as, connect device over USB cable and type
$ adb tcpip 5555
restarting in TCP mode port: 5555
Now, you will be able to connect over network as,
$ adb connect 192.168.0.104:5555
connected to 192.168.0.104:5555
If you have not removed the USB cable, you might seen the same device as two devices as,
$ adb devices
List of devices attached
B2NGAA8831702707 device
192.168.0.104:5555 device
$ adb -s 192.168.0.104:5555 shell
B2N_sprout:/ $
You can also automate this adb connection over network to your device using a bash script as below, this script will wait till your android device is connected to network and then only it will attempt to connect over adb.
$ vim adb_connect.sh
#!/bin/bash
# sudo apt install fping
#IP_LAST_DIGIT=$1
#DEVICE_IP=192.168.1.$IP_LAST_DIGIT
DEVICE_IP=192.168.1.XXX
ADB_PORT=5555
sudo adb kill-server
itest=$(fping $DEVICE_IP | grep alive)
while [ \"$itest\" == \"\" ]
do
echo "device not online yet.. wait for 1 sec.."
sleep 1
itest=$(fping $DEVICE_IP | grep alive)
done
echo "$DEVICE_IP is now online"
sudo adb connect $DEVICE_IP
sleep 3
adb -s "$DEVICE_IP:$ADB_PORT" shell
In the above script, you need to change DEVICE_IP to your android device’s IP address. If you are connecting to different devices over same network, you can also use the provision of passing last digit of ip address by enabling DEVICE_IP=192.168.1.$IP_LAST_DIGIT and by passing IP last digit from command line to the script.