Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically adb connect to a device over wifi

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?

like image 679
rush Avatar asked Apr 08 '15 12:04

rush


2 Answers

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.

like image 85
Kibsgaard Avatar answered Oct 09 '22 09:10

Kibsgaard


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
like image 35
Teocci Avatar answered Oct 09 '22 09:10

Teocci