This gist covers how you allow the WSL install of ADB to receive data from the host Windows instance of ADB.
This method was derived from the following Stack Overflow questions:
- https://stackoverflow.com/questions/60166965/adb-device-list-empty-using-wsl2#answer-77049846
- https://stackoverflow.com/questions/71094306/adb-server-socket
The following section describes how to set up the host machine so that the guest and host versions of ADB are able to communicate, thus allowing you to develop in WSL and pass through data to an external device connected to your host machine.
To summarise, the required ADB port is open between the guest and host WSL instance; then every time you start up your host machine, you start the ADB server in a particular mode that allows the forwarding of data.
This only needs to be run once.
Buy running enable-adb-port-for-wsl2.ps1
you will set up a Firewall rule on the host machine that will allow data coming from the WSL subnet on port 5037 to be passed through to any process listening on 5037 on your host machine. This firewall rule will be given the name "ADB WSL2: Open Port 5037" should you need to find it and make changes.
Note: You'll want to 'Run As Administrator'
This will need to be run every time you boot up your machine.
By running start-adb-for-wsl.bat
on your host machine, you will restart ADB in nodaemon mode, which allows it to port forward any data it receives, thereby acting like a proxy between your test devices and your guest OS (WSL).
The following section describes how to set up the guest WSL OS so that the guest versions of ADB is able to communicate with the host.
This only needs to be run once.
By running setup-adb-env-var.sh
you will find the IP of your host machine and create a variable called ADB_SERVER_SOCKET
that is acts as a directive for ADB to look for tcp data from the host IP over the default ADB port, 5037.
If you choose this method, this will need to be run every time you boot up your machine.
If for some reason to don't want to set up env vars that configure your WSL ADB server to look for external connections, you can use socat to open up a TCP connection on an ad-hoc basis.
Run listen-for-host-adb-data.sh
when you start up WSL to start listening for tcp data on port 5037 from the host machine.
You need to install socat on WSL if it's not already installed, here's an article about socat https://www.redhat.com/sysadmin/getting-started-socat.