Hsüan's Blog

2023-10-22

windows wsl2 adb connect host phone

可依照此篇操作

  • On host(Windows), install platform-tools

    • Install Android Studio 就會有了
  • On wsl, install platform-tools

    • apt install android-tools-adb
  • On host, get the ip of wsl network interface. The ip for following example is 172.28.96.1

    PS C:\WINDOWS\system32> ipconfig
    
    Windows IP Configuration
    
    
    Ethernet adapter vEthernet (WSL):
    
      Connection-specific DNS Suffix  . :
      Link-local IPv6 Address . . . . . : fe80::270d:56b2:7c32:9228%42
      IPv4 Address. . . . . . . . . . . : 172.28.96.1
      Subnet Mask . . . . . . . . . . . : 255.255.240.0
      Default Gateway . . . . . . . . . :
  • 殺掉 host 的 adb

    adb kill-server
  • 在 host 重啟 adb 且綁定 all network interfaces

    # 擇一即可
    
    # 都用 default,只是多綁定其他 network interfaces
    adb -a start-server
    
    # 改用其他 port
    adb -a -P 5555 start-server
    
    # 不放背景執行,順便看 log
    adb -a nodaemon server
  • 將 wsl 裡面 adb 綁定的 ip 跟 port 改為 host 的

    # in ~/.bashrc or ~/.bash_aliases
    export ADB_SERVER_SOCKET=tcp:$(cat /etc/resolv.conf | awk '/nameserver/ {print $2}'):5037
  • 之後在 wsl 裡面,就都依照一般情況操作即可

    adb devices
    
    # 想手動指定也可以
    adb -H 172.28.96.1 -P 5037 devices

© 2019 ~ 2025 Hsüan, Powered by Gatsby, Theme Material UI