34

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)
Sébastien
  • 13,831
  • 10
  • 55
  • 70

8 Answers8

13

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

Build3r
  • 1,748
  • 16
  • 22
12

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
}
Edward Kenway
  • 131
  • 1
  • 7
4

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()
edel-david
  • 66
  • 1
  • 4
  • 3
    for anyone else wondering like I was, you can directly put in the following in powershell after having installed nmap to complete adb connection: `adb connect YOUR_IP:$(nmap YOUR_IP -p 37000-44000 | Where-Object{$_ -match "tcp open"} | ForEach-Object {$_.split("/")[0]})` – Ontropy Mar 07 '23 at 03:55
  • The port range should be wider than 37000-44000. My current device uses port 34869. – Sébastien May 12 '23 at 14:09
  • I used this extensively on my phone with android 11. can you share your android version or phone model? – edel-david May 13 '23 at 15:31
3

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
aostanin
  • 39
  • 3
  • 1
    Excellent answer! I've created a shell script based on your information https://gist.github.com/gnumoksha/f9a5b2e01b1e74ffa2a055b6e18f7c58 – Tobias Sette Oct 10 '22 at 00:39
  • This is indeed a **very** underrated answer, using such technique you can write a script to automatically connect to your phone when it appears. The ID also seems to look like persistent, so you can use that to filter to which phones to connect. – Skydev May 08 '23 at 07:40
3

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

SonicGold
  • 35
  • 1
  • 7
1

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.
Sébastien
  • 13,831
  • 10
  • 55
  • 70
  • Update after a few months: the detection of the device by Android Studio is, unfortunately, *extremely* unreliable and inconsistent. – Sébastien Sep 12 '22 at 13:58
1

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.

番薯_
  • 21
  • 2
0

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)

Ppica88
  • 51
  • 9
  • This solution is not working for me. After pairing, upon connection request I get `No connection could be made because the target machine actively refused it. (10061)` The port keep changing like before I can only connect using currently used port number. Don't see the point of pairing... – Rayearth Feb 19 '23 at 17:12
  • mmm...It sounds to me like there is a network issue around there...You can check this post about that: [link](https://stackoverflow.com/questions/37267335/android-studio-wireless-adb-error-10061). I get so much help from adb official doc. You can check here: [link](https://stackoverflow.com/questions/37267335/android-studio-wireless-adb-error-10061) – Ppica88 Feb 21 '23 at 16:43