adb over Wi-Fi (Android 11+) on Windows: how to keep a fixed port or connect automatically?

11.3k Views Asked by At

The wireless adb connection works fine on my Android 11 phone + Windows workstation.

But it's not convenient, as every time the phone Wifi disconnects/reconnects, I have to:

  1. Turn on wireless debugging in Android settings.
  2. Take note of the port number XXXXX, which changes every time!
  3. Run adb connect 192.168.1.10:XXXXX on the computer.

Is there a way to skip step 2, by either:

  • making the port fixed?
  • making Windows automatically detect the phone on the new port? (documentation seems to imply that step 2 and 3 are not needed on MacOS, once the pairing is done, I wonder how this works)
10

There are 10 best solutions below

1
On BEST ANSWER

The problem is now solved thanks to a recent update of Android Studio.

All steps can now be automated:

  1. Turn on wireless debugging in Android settings. → this can be automated with a simple Tasker profile: when connected to your office wifi, set a custom setting to enable wireless debugging like so:

tasker profile

tasker task

Or if you don't need full automation, you can probably add a quick switch for convenience. On a Pixel 3 it can be done in Settings > System > Developer options > Quick settings developer tiles > Wireless debugging

  1. Android Studio Bumblebee 2021.1.1 now automatically (after a few seconds) detects the device and connects to it! No more copying of port numbers.
0
On

Building back on the answer from Build3r, here is the PowerShell snippet in order to get a one-line solution for wireless adb on Windows :

adb connect $IP`:$(((nmap $IP -p 37000-44000 | sls tcp) -Split '/')[0])

You'll need to define your device IP (a DHCP permanent lease would come in handy there) beforehand via :

$IP="192.168.0.69"

Some documentation regarding the required adb pairing before being able to use this snippet : https://developer.android.com/studio/command-line/adb#wireless-android11-command-line

0
On

Building back on the answer from Build3r and SonicGold.I write a bat script with Python to auto connect. Python:

ip = "192.168.10.136"
ret = subprocess.run(["powershell", "-command",
                      F"((nmap {ip} -p 37000-44000 | sls tcp) -Split '/')"],
                     capture_output=True)
outputs = ret.stdout.decode().strip().split("\n")
for output in outputs:
    output = output.replace("\r", "")
    if output.isalnum():
        print(f"find port:{output}")
        print(subprocess.run(["powershell", "-command",
                              F"adb connect {ip}:{output}"],
                             capture_output=True).stdout.decode().strip()
              )
time.sleep(5)

bat:

"python" "xxx\scanWifi.py"

It can scan all the match port and try to connect.

1
On

You can dynamically get port using nmap and connect to it.

here is my solution

adb connect <device_ip>:$(nmap $IP -p 37000-44000 | awk "/\/tcp/" | cut -d/ -f1)

Scanning only ports 37000-44000 is sufficient Also wireless debugging should be enabled and device needs to unlocked during nmap scan. Run it again if the nmap doesn't find the port first time.

I have added the command to an alias so it is easy to run
ex:
alias adbw='adb connect 192.168.0.7:$(nmap $IP -p 37000-44000 | awk "/\/tcp/" | cut -d/ -f1)'

To connect next time:

  1. Unlock Device
  2. Enable Wireless debugging (you can add it to status bar icons)
  3. run adbw if alias set.

Ex Output:
connected to 192.168.0.7:38395

4
On

I liked Build3r's answer so i ported it to powershell, you only need to install nmap which is available for windows:

nmap YOUR_IP -p 37000-44000 | Where-Object{$_ -match "tcp open"} | ForEach-Object {$_.split("/")[0]}

i use this in a python script which is in my PATH

ret=subprocess.run(["powershell","-command",F'nmap {ip} -p 37000-44000 | Where-Object{{$_ -match "tcp open"}} | ForEach-Object {{$_.split("/")[0]}}'],capture_output=True)
port=ret.stdout.decode().strip()
3
On

Android broadcasts the connection details over mDNS/DNS-SD with a service type of ._adb-tls-connect._tcp.

You can discover Android devices with wireless adb enabled using something like avahi-browse.

$ avahi-browse --terminate --resolve _adb-tls-connect._tcp
+    br0 IPv6 adb-26df62cd-sGvUmf                           _adb-tls-connect._tcp local
+    br0 IPv4 adb-26df62cd-sGvUmf                           _adb-tls-connect._tcp local
+ enp5s0 IPv6 adb-26df62cd-sGvUmf                           _adb-tls-connect._tcp local
=    br0 IPv6 adb-26df62cd-sGvUmf                           _adb-tls-connect._tcp local
   hostname = [Android.local]
   address = [10.0.0.199]
   port = [37531]
   txt = ["v=ADB_SECURE_SERVICE_VERSION"]
=    br0 IPv4 adb-26df62cd-sGvUmf                           _adb-tls-connect._tcp local
   hostname = [Android.local]
   address = [10.0.0.199]
   port = [37531]
   txt = ["v=ADB_SECURE_SERVICE_VERSION"]
= enp5s0 IPv6 adb-26df62cd-sGvUmf                           _adb-tls-connect._tcp local
   hostname = [Android.local]
   address = [10.0.0.199]
   port = [37531]
   txt = ["v=ADB_SECURE_SERVICE_VERSION"]

Then you can connect using the service name.

$ adb connect adb-26df62cd-sGvUmf
connected to adb-26df62cd-sGvUmf._adb-tls-connect._tcp

Or using the address and port.

$ adb connect 10.0.0.199:37531
connected to 10.0.0.199:37531
2
On

I discovered that once you have paired the device, you will never be asked for anything again to connect to that device (except you later revoke the permissions manually).

To pair a device from adb, first you have to asure the following in this checklist:

  1. You are on the same net (e.g.: your laptop and oyur phone)
  2. You have activated Wireless debug on your phone

Once you have confirmmed those things, you have to go to developers menu in your phone (the one where you can find wireless debug option). Enter in that submenu and go to the option "pair with code". A popup with certain data will be shown. With that in sight, you go to the terminal and put this command:

abd pair <your-device-ip>:<device-port> <pairing-code>

With that already done, you always will see that device in the list of devices when you do an adb devices in your laptop (if all the points from checklist are accomplished)

0
On

I have just found below ways for both Linux and Windows (hope it will help for others). Because ADB 31.0.2 or later disables mDNS discovery, you must enable mDNS discovery and then restart the ADB server.

Linux:

export ADB_MDNS_OPENSCREEN=1
adb kill-server
adb start-server 
adb mdns services

Windows:

set ADB_MDNS_OPENSCREEN=1
adb kill-server
adb start-server 
adb mdns services

Outputs (for example):

List of discovered mdns services
driver_vm       _adb._tcp       192.168.1.49:4444
device_vm       _adb._tcp       192.168.1.49:3333

if there is only one devices, it automatically connects to the devices every time wireless debugging is enabled on this device. Otherwise you can use connect command as shown below

adb connect driver_vm._adb._tcp

or

adb connect 192.168.1.49:3333

Reference: https://source.android.com/docs/automotive/virtualization/tools

0
On

A hint on all the solutions discussed here and on using adb sync and adb connect for wireless debugging with ADB in general:

Instead of providing the (awkward, possibly even volatile) current IP address of the mobile device, it is sufficient to use localhost (or the equivalent IPv4 address 127.0.0.1 or IPv6 address ::1).

4
On

You can make the port fixed until reboot by adb tcpip

After pairing and connecting with the dynamic port

try adb tcpip 5555

then you can use adb connect ip:5555 until reboot (ya after reboot you've to connect with dynamic port and set tcpip to 5555 again)

Edit: I run this command whenever i reboot my phone

adbw() {
    adb connect $IP:$1
    adb tcpip 5555
    adb disconnect
    adb connect $IP:5555
}