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

Viewed 2746

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)
6 Answers

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

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
}

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()

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.

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)

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
Related