7

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?

rush
  • 413
  • 2
  • 5
  • 13

5 Answers5

13

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.

Kibsgaard
  • 469
  • 3
  • 7
4

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)

1. For Linux and MAC User:

Step 1:

Open terminal and install adb using

sudo apt-get install android-tools-adb android-tools-fastboot

Step 2:

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

Step 3:

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

Step 4:

Setup the communication port using this command:

$ adb -s LGV498b9cacc1 tcpip 5559

restarting in TCP mode port: 5559

Step 5:

Connect to your Android device IP address.

$ adb -s LGV498b9cacc1 connect 192.168.1.185:5559

connected to 192.168.1.185:5559

Step 6:

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
Teocci
  • 7,189
  • 1
  • 50
  • 48
  • Not true as of now. Device can be set up to autostart tcp service. Check this: http://omelina.com/permanent-network-debugging-on-android/ – midenok Jul 09 '22 at 03:18
2

If you are using Windows operating system, you could create a batch file and add

adb connect 192.168.1.179

please replace 192.168.1.179 with your own device ip address.

Then save the bat file and put it in startup folder.

navylover
  • 12,383
  • 5
  • 28
  • 41
0

You can't automatically connect your device over wifi if it's not connected using a USB cable. I've developed an open source IntelliJ Plugin do this as fast as possible. Here you have de code https://github.com/pedrovgs/AndroidWiFiADB and here the plugin https://plugins.jetbrains.com/plugin/7983

0

No you can't do that because once connection to adb server is killed you have to make connection again .
If you still want to connect automatically and don't have to write command again and again than You can create a batch/Script file with all the command and make it run at time of reconnecting to adb devices .

Amit Sharma
  • 645
  • 5
  • 13