I have an android device connected through adb over wifi. Now, due to some reason, the adb server is killed using command 'adb kill-server'.
Once I restart the server or issue the command 'adb devices', I would like the devices that were connected over wifi to appear in the list of devices, Just like the devices connected by usb appear in the list.
How can this be achieved? Can I put the ipaddresses of the devices in some file and they get connected automatically when the adb server restarts?
I have made batch scripts for automatically setting up a device for Wifi adb bridge, getting the IP and connecting to it. You just plug your device in, run the script and then unplug the device again.
Windows batch (wifi-connect.bat):
@echo off
echo Disconnecting old connections...
adb disconnect
echo Setting up connected device
adb tcpip 5555
echo Waiting for device to initialize
timeout 3
FOR /F "tokens=2" %%G IN ('adb shell ip addr show wlan0 ^|find "inet "') DO set ipfull=%%G
FOR /F "tokens=1 delims=/" %%G in ("%ipfull%") DO set ip=%%G
echo Connecting to device with IP %ip%...
adb connect %ip%
pause
Unix / Mac (wifi-connect.sh)
#!/bin/sh
adb disconnect
adb tcpip 5555
sleep 3
IP=$(adb shell ip addr show wlan0 | grep 'inet ' | cut -d' ' -f6| cut -d/ -f1)
echo "${IP}"
adb connect $IP
Both scripts requires adb to be in your path or in the same folder as the script.
You cannot automatically connect your device over WiFi if it that DEVICE is not connected using a USB cable first, because you need to config the device to listen a port and open a connection. What you can do is try to run these commands using a script.
From a computer, if you have USB access already (NO root required)Open terminal and install adb using
sudo apt-get install android-tools-adb android-tools-fastboot
Connect your phone via USB cable to the PC. Type following command in terminal to get the device ID:
$ adb devices
List of devices attached
LGV498b9cacc1 device
192.168.1.187:5558 device
192.168.1.184:5557 device
192.168.1.186:5556 device
192.168.1.143:5555 device
Using the device name listed above, get the IP of your Android device (if you know you can skip this step)
$ adb -s LGV498b9cacc1 shell ip -f inet addr show wlan0
22: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
inet 192.168.1.185/24 brd 192.168.1.255 scope global wlan0
Setup the communication port using this command:
$ adb -s LGV498b9cacc1 tcpip 5559
restarting in TCP mode port: 5559
Connect to your Android device IP address.
$ adb -s LGV498b9cacc1 connect 192.168.1.185:5559
connected to 192.168.1.185:5559
Verify if the device was added to the list:
$ adb devices
List of devices attached
192.168.1.185:5559 device
LGV498b9cacc1 device
192.168.1.187:5558 device
192.168.1.184:5557 device
192.168.1.186:5556 device
192.168.1.143:5555 device
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With