[ASUS ZenFone 8] bnsmb:
The Android OS supports adb connections via USB and also via WiFi.

Sometimes it's usefull to enable adb via WiFi in a script. So lets check how that can be done.

To enable adb via WiFi in the Android GUI on the phone just open the Developer Options and activate "Wireless debugging".

View attachment 6056794

After enabling adb via WiFi in the Android GUI the adb connections from machines in the current WLAN must be confirmed in a popup dialog on the phone (if not already done for the current WLAN)

View attachment 6056796

adb via WiFi is then immediately enabled (tick "Always allow on this network" to make the confirmation persistent).

Be aware that the port used for adb via WiFi when enabled in the Android GUI is dynamically selected by the Android OS and is therefor different each time the adb via WiFi is enabled using this method.

The IP address and the port used for adb via WiFi are visible in the "Wireless debugging" details in the Android GUI

View attachment 6056795

The command to connect using adb via WiFi is then:

[ OmniRom 14 Dev - xtrnaw7@t15g ~ ] $ adb -e -L tcp:localhost:5237 connect 192.168.1.148:40719
* daemon not running; starting now at tcp:localhost:5237
* daemon started successfully
connected to 192.168.1.148:40719
[ OmniRom 14 Dev - xtrnaw7@t15g ~ ]]

Now a shell via WiFi adb can be opened using this command:

[ OmniRom 14 Dev - xtrnaw7@t15g ~ ] $ adb -e -L tcp:localhost:5237 shell
ASUS_I006D:/ $ uname -a
Linux localhost 5.4.147-Omni-qgki-perf-g736dda6e5ae7 #29 SMP PREEMPT Sun Dec 3 09:59:38 CET 2023 aarch64 Toybox
ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $

Notes:

192.168.1.148 is the IP from the Wireless Debugging details and 40719 is the port from the Wireless Debugging details.

5237 is the local port used; that can be any unused port on your PC greater then 1024.

The computer trying to connect to the phone using adb via WiFi must already be authorized for adb via USB.

The BSSID of the WLANs authorized for adb via WiFi is stored in the adbd keystore, this is the file adb_temp_keys.xml in the directory /data/misc/adb :

# root access is necessary to read the adbd keystore
#
ASUS_I006D:/ $ su -

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #

ASUS_I006D:/ # cd /data/misc/adb
ASUS_I006D:/data/misc/adb #

# the adbd keystore is in binary xml format and must be converted to plain xml to read the contents
#
ASUS_I006D:/data/misc/adb # abx2xml adb_temp_keys.xml adb_temp_keys.xml.plain
ASUS_I006D:/data/misc/adb #

ASUS_I006D:/data/misc/adb # cat adb_temp_keys.xml.plain
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<keyStore version="1">
<adbKey key="QAAAAAO//XxV3DN2zq7j8w0uc/kHyC9vDeQt+Vx3IEcxRzdidhIuO0V6YhV9wHQO+Fz5qbC+nHte0X3eQxPGkbAYlNgmK/e9OWu0ccO5ena0vNLO03YfIAcKQ4q4XZZTuUMgtC9g5BVrOInNAfcQ9UwSAP9dWCuvnuCvK3FR6DTDRWl+D9DiP5/4eFsHWFKOBvegn4EhsJLMsM4Yj1r3mrqpmTBvYNDI4W44FPAd9BfGSE4oZ51yDQdoAUVpvNpG/samU8fWwQqbFQTvHFONB3V7+cjHrUi7pKY5fUKTENiBEdmW0qeL0xbfktpbBBQMPjcgcPGPDPIwSro1T6F1SO6P9ywUKeu2yfxyzUk1/fsRYoTapkoGs7oNF2N2otaZdNZX4Z7vMR3o9dspBLN9WaLJ/q8Z96+G8pNzNGnmfP7giZsBl+rhiqqp4FLD5l7qCN71FmW2jZt77ovQE/y5lZo8KL0I2UiFmYZc7RpM25t3shBrR7LF9sriqvJN/wG8aDy/puteZO6RifA9zT4Jcpo3QgfCzqMbrSHaVlE3QmApO9RfIO3tZCn7+KSFoLGL/GvCv67q5+dvTTb9oLJ6njxEfZLikRfJe/6eXQ+4osqis2lGlZZuFtjIeSlc4xUb7s2x5h/hyroeUXHFdFnDyKpMcewKppdooJh6sXKv9XCixFEXz3huhgEAAQA= xtrnaw7@t15g.isbs.de" lastConnection="1705393201326" />
<wifiAP bssid="24:4b:fe:f3:02:58" />
</keyStore>ASUS_I006D:/data/misc/adb #

adb via WiFi enabled using the Android GUI is again disabled after the next reboot of the phone. But the BSSID for the already authorized WLANs remains in the adbd keystore (if you ticked "Always allow on this network" in the confirmation dialog for the WLAN).

adb via WiFi using this method can also be enabled with the CLI command settings, e.g:

to retrieve the current status of adb via WiFi use:

settings get global adb_wifi_enabled

to enable adb via WiFi use:

settings put global adb_wifi_enabled 1

to disable adb via WiFi use:

settings put global adb_wifi_enabled 0

Note that enabling adb via WiFi using the setting adb_wifi_enabled will popup a dialog on the phone to authorize the adb via WiFi using the current WLAN if not already authorized.

Spoiler content hidden.

The settings command can be used by the normal user to enable or disable adb via WiFi; no root access is necessary to enable adb via WiFi using this method.

But to retrieve the port used for adb via WiFi root access is required:

ASUS_I006D:/ $ su - -c netstat -tnlp | grep adbd | grep LISTEN | awk -F: '{print $4}'
40719 [
ASUS_I006D:/ $

The entry for the WLAN BSSID can also be written to the adbd keystore in a script, but there is no method to get the adbd to re-read the keystore. Restarting the adbd does not help either, as the daemon overwrites the adbd keystore with the values in memory before it stops.

Therefore, activating adb via WiFi with this method in a script without user intervention only works if adb via WiFi has been activated once in the Android GUI.

Notes:

Note that enabling adb via WiFi using the settings command works only reliable if the development settings are enabled

To enable or disable the development settings via cli command use these commands:

to retrieve the current status of the development settings use:

settings get global development_settings_enabled

to enable the development settings use:

settings put global development_setttings_enabled 1

to disable the development settings use:

settings put global development_settings_enabled 0

Note that the settings command can be used by the normal user; no root access is necessary to enable or disable the development settings using this method.

Spoiler content hidden.

In the end, this method to enable adb via WiFi is only useful for temporary interactive usage but not for using it in scripts.

Another method to enable adb via WiFi is to set the property persist.adb.tcp.port:

The property persist.adb.tcp.port can be set to a port that the adb daemon on the phone (adbd) will listen on after the next reboot. Any free port greater than 1024 can be used for this purpose.

To check the used ports on the phone use the netstat command:

ASUS_I006D:/ $ su - -c ' netstat -tnlp | egrep "State|LISTEN" '
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program Name
tcp6 0 0 [::]:1024 [::]:* LISTEN 19167/adbd
tcp6 0 0 ::ffff:127.0.0.1:41479 [::]:* LISTEN 8156/com.google.android.youtube
ASUS_I006D:/ $

The property can only be set by the root user:

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/

ASUS_I006D:/ $ setprop persist.adb.tcp.port 6666
Failed to set property 'persist.adb.tcp.port' to '6666'.
See dmesg for error reason.
ASUS_I006D:/

1|ASUS_I006D:/ $ su - -c setprop persist.adb.tcp.port 6666
ASUS_I006D:/ $

ASUS_I006D:/ $ getprop persist.adb.tcp.port
6666
ASUS_I006D:/ $

After the next reboot adb connections via WiFi work:

[ OmniRom 14 Dev - xtrnaw7@t15g ~ ] $ adb -e -L tcp:localhost:5237 connect 192.168.1.148:6666
* daemon not running; starting now at tcp:localhost:5237
* daemon started successfully
connected to 192.168.1.148:6666
[ OmniRom 14 Dev - xtrnaw7@t15g ~ ]

[ OmniRom 14 Dev - xtrnaw7@t15g ~ ] $ adb -e -L tcp:localhost:5237 shell
ASUS_I006D:/ $ ls
ADF acct asdf bin cache d data_mirror dev init linkerconfig metadata odm oem proc sdcard storage system system_ext vendor_dlkm
APD apex batinfo bugreports config data debug_ramdisk etc init.environ.rc lost+found mnt odm_dlkm postinstall product second_stage_resources sys system_dlkm vendor
ASUS_I006D:/ $

If adb via WiFi is activated using this method, authorization is only required for the computers that want to connect (analogous to the authorization for adb via USB); the authorization for the used WLAN is not necessary.

And this authorization can be configured via script -- see How to enable access via adb on a new installed_OS.

Therefor this method to enable adb via WiFi can be used in scripts .

Notes:

Instead of rebooting the phone it's also sufficient to restart the adbd after setting the property to enable adb via WiFi . But in this case all active adb sessions will be killed.

adb via WiFi, which is activated via the persist.adb.tcp.port property, only works if adb via USB is also activated and already authorized -- either via the Android GUI or by creating the adbd keystore files with a script.

Invalid values for the property persist.adb.tcp.port will be ignored and adb via WiFi will not be activated

General Notes:

Both methods to enable adb via WiFi can be used in parallel

It is possible to use an adb authorization key to connect from several computers in parallel using adb via WiFi; simply copy the file $HOME/.android/adbkey to the other computers.

Test Environments

OmniROM 13

OmniROM 14

ASUS Android 12

ASUS Android 13