PSA: How to get the random Android wireless debugging port from Windows?

PSA: How to get the random Android wireless debugging port from Windows? Woo hoo!

I found a solution to the problem set Android Wireless debugging posed: *Is there a way to put a command window in the background like Linux does* (so that you can kill the Android mirroring console named scrcpy?)

formatting link

*IMPORTANT UPDATE* to work around the total absence of 'Ctrl-Z,bg' in Windows, which badly affects Android:Windows adb Wi-Fi efficiency!
formatting link
New 'Ctrl+Z,bg' workaround

It took me days (elapsed time) to figure out this latest workaround to the complete and utter total lack of 'Ctrl+Z,bg' in Windows.

Yet, the final solution turns out to be incredibly simple. If you know a few of the tricks, that is.

This lack of 'Ctrl+Z,bg' is a horrible deficiency of Windows indeed. Luckily, with your help, we created a USB 'Ctrl+Z,bg' workaround.

And, after testing for days, *we have a Wi-Fi 'Ctrl+Z,bg' workaround.*

This new 'Ctrl+Z,bg' workaround can now be added to the existing 'Ctrl+Z,bg' workaround batch script kindly authored by Herbert.

It need only be a variable, e.g., %GET-RANDOM-PORT-ASSIGNMENT%

As you may recall, the 'Ctrl+Z,bg' workaround script Herbert kindly proposed was to solve the lack of 'Ctrl+Z,bg' in Windows given conditions of an initial USB connection.

That initial USB connection only needed a _static_ port address.

So the previous 'Ctrl+Z,bg' workaround script worked fine as that previous 'Ctrl+Z,bg' workaround script didn't need to query Android for the port address.

The problem that arose in Android 11+ is the addition of Wireless debugging which necessitated the 'Ctrl-Z,bg' workaround batch script now knowing, a priori, what that randomly assigned wireless debugging IP address is.

But how? Specifically, how do you query Android WITHOUT first establishing a connection to it (where that connection _requires_ the random port!).

Seemingly that's impossible, right? Ah, but it's not impossible.

It took me a few days (elapsed time) to figure out how to query Android from Windows to _obtain_, a priori, that randomly assigned IP address!

Luckily I found two Windows commands which will obtain that random port: C:\> netstat -ano -p tcp | findstr "192.168.1.10" C:\> adb mdns services

Below is a log file reproducing the steps in only a minute or two.

formatting link
New 'Ctrl+Z,bg' workaround

---------------------- Log file for this image:

formatting link
C:\app\editor\android\scrcpy>netstat -ano -p tcp | findstr "192.168.1.10"

C:\app\editor\android\scrcpy>adb shell "ifconfig|grep -A 1 wlan0|tail -n

1|cut -f2 -d:|cut -f1 -d' '"
  • daemon not running; starting now at tcp:5037
  • daemon started successfully adb.exe: no devices/emulators found

C:\app\editor\android\scrcpy>adb devices List of devices attached

C:\app\editor\android\scrcpy>adb mdns services List of discovered mdns services

C:\app\editor\android\scrcpy>On Android, turn wi-fi on & connect to the LAN (wait a minute or two)

C:\app\editor\android\scrcpy>netstat -ano -p tcp | findstr "192.168.1.10"

C:\app\editor\android\scrcpy>adb shell "ifconfig|grep -A 1 wlan0|tail -n

1|cut -f2 -d:|cut -f1 -d' '" adb.exe: no devices/emulators found

C:\app\editor\android\scrcpy>adb devices List of devices attached

C:\app\editor\android\scrcpy>adb mdns services List of discovered mdns services

C:\app\editor\android\scrcpy>Now turn on Developer options, Wireless debugging & accept the "Allow" (wait 2 min)

C:\app\editor\android\scrcpy>Note that the phone knows the address & port of 192.168.1.10:36999 (not the PC)

C:\app\editor\android\scrcpy>When the phone beeps with "Wireless debugging connected", you're ready

C:\app\editor\android\scrcpy>netstat -ano -p tcp | findstr "192.168.1.10" TCP 192.168.1.27:49765 192.168.1.10:36999 ESTABLISHED 3208

C:\app\editor\android\scrcpy>Bingo! The PC now knows the Wireless debugging IP address & random assigned port!

C:\app\editor\android\scrcpy>adb shell "ifconfig|grep -A 1 wlan0|tail -n

1|cut -f2 -d:|cut -f1 -d' '" 192.168.1.10

C:\app\editor\android\scrcpy>Windows adb knows only the IP address (but not the randomly assigned wireless port) :(

C:\app\editor\android\scrcpy>adb devices List of devices attached adb-ABCEFGHIJKL-123456._adb-tls-connect._tcp. device

C:\app\editor\android\scrcpy>adb mdns services List of discovered mdns services adb-ABCEFGHIJKL-123456._adb-tls-connect._tcp. 192.168.1.10:36999

C:\app\editor\android\scrcpy>Ah. Nice. So Windows adb DOES know the IP address & randomly assigned wireless debug port

C:\app\editor\android\scrcpy>scrcpy.exe scrcpy 1.24

formatting link
C:\app\editor\android\scrcpy\scrcpy-server: 1 file pushed, 0 skipped. 0.8 MB/s (41159 bytes in 0.051s) [server] INFO: Device: samsung SM-A326U (Android 12) INFO: Renderer: direct3d INFO: Initial texture: 720x1600

At this point you would die for a "control-Z bg" command sequence. If that 'Ctrl+Z,bg' existed on Windows, this thread wouldn't be needed. But alas, Windows completely lacks such a 'Ctrl+Z,bg' luxury. Hence the need for a 'Ctrl+Z,bg' workaround batch script.

If 'Ctrl+Z,bg' existed on Windows, we would never have this problem of querying Android from Windows to obtain the Wireless-debugging port random assignment (because scrcpy console does NOT need it!).

But if we want to eliminate the scrcpy console, then we need to find a workaround to the fact that Windows completely lacks a basic 'Ctrl+Z,bg' functionality. <=== this is the problem!

Since 'Ctrl+Z,bg' does NOT exist on the PC, we need to tell the 'Ctrl+Z,bg' workaround batch script what that randomly assigned port is set to on Android WITHOUT looking at Android.

The 'Ctrl+Z,bg' workaround script must figure it out on its own! Hence you must figure out the random port assignment from the PC.

Notice scrcpy with the console does NOT need to know this port. It figures it out on its own.

So it's only the 'Ctrl+Z,bg' workaround script that NEEDS this port. All we need now is to ADD that new 'Ctrl+Z,bg' variable to the script!

In summary, the old USB 'Ctrl+Z,bg' workaround, which expects a static port, only needs to have added a new 'Ctrl+Z,bg' variable, e.g., %GET-RANDOM-PORT-ASSIGNMENT%

Reply to
Andy Burnelli
Loading thread data ...

Cabling-Design.com Forums website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.