Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active May 9, 2025 19:00
Show Gist options
  • Save benigumocom/a6a87fc1cb690c3c4e3a7642ebf2be6f to your computer and use it in GitHub Desktop.
Save benigumocom/a6a87fc1cb690c3c4e3a7642ebf2be6f to your computer and use it in GitHub Desktop.
Connect Wireless Debug from Terminal on Android11
#!/usr/bin/env python3
"""
Android11
Pair and connect devices for wireless debug on terminal
python-zeroconf: A pure python implementation of multicast DNS service discovery
https://github.com/jstasiak/python-zeroconf
"""
import subprocess
from zeroconf import ServiceBrowser, Zeroconf
TYPE = "_adb-tls-pairing._tcp.local."
NAME = "debug"
PASS = "123456"
FORMAT_QR = "WIFI:T:ADB;S:%s;P:%s;;"
CMD_SHOW = "qrencode -t UTF8 '%s'"
CMD_PAIR = "adb pair %s:%s %s"
CMD_DEVICES = "adb devices -l"
class MyListener:
def remove_service(self, zeroconf, type, name):
print("Service %s removed." % name)
print("Press enter to exit...\n")
def add_service(self, zeroconf, type, name):
info = zeroconf.get_service_info(type, name)
print("Service %s added." % name)
print("service info: %s\n" % info)
self.pair(info)
def pair(self, info):
cmd = CMD_PAIR % (info.server, info.port, PASS)
print(cmd)
subprocess.run(cmd, shell=True)
def main():
text = FORMAT_QR % (NAME, PASS)
subprocess.run(CMD_SHOW % text, shell=True)
print("Scan QR code to pair new devices.")
print("[Developer options]-[Wireless debugging]-[Pair device with QR code]")
zeroconf = Zeroconf()
listener = MyListener()
browser = ServiceBrowser(zeroconf, TYPE, listener)
try:
input("Press enter to exit...\n\n")
finally:
zeroconf.close()
subprocess.run(CMD_DEVICES, shell=True)
if __name__ == '__main__':
main()
@Vazgen005
Copy link

I also created a python package: https://pypi.org/project/adb-wifi-py/

@Benjamin-Wegener
Copy link

Benjamin-Wegener commented May 9, 2025

so this uses python qrencode instead of system lib, use pip install qrcode beforehand

#!/usr/bin/env python3

"""
Android11
Pair and connect devices for wireless debug on terminal

python-zeroconf: A pure python implementation of multicast DNS service discovery
https://github.com/jstasiak/python-zeroconf

qrcode: Pure python QR Code generator
https://github.com/lincolnloop/python-qrcode
"""

import subprocess
import qrcode
from zeroconf import ServiceBrowser, Zeroconf


TYPE = "_adb-tls-pairing._tcp.local."
NAME = "debug"
PASS = "123456"
FORMAT_QR = "WIFI:T:ADB;S:%s;P:%s;;"

CMD_PAIR = "adb pair %s:%s %s"
CMD_DEVICES = "adb devices -l"

class MyListener:

    def remove_service(self, zeroconf, type, name):
        print("Service %s removed." % name)
        print("Press enter to exit...\n")

    def add_service(self, zeroconf, type, name):
        info = zeroconf.get_service_info(type, name)
        print("Service %s added." % name)
        print("service info: %s\n" % info)
        self.pair(info)

    def pair(self, info):
        cmd = CMD_PAIR % (info.server, info.port, PASS)
        print(cmd)
        subprocess.run(cmd, shell=True)


def display_qr_code(text):
    """Generate and display a QR code in the terminal."""
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=1,
        border=1
    )
    qr.add_data(text)
    qr.make(fit=True)
    
    qr.print_ascii(invert=True)


def main():
    text = FORMAT_QR % (NAME, PASS)
    display_qr_code(text)

    print("Scan QR code to pair new devices.")
    print("[Developer options]-[Wireless debugging]-[Pair device with QR code]")

    zeroconf = Zeroconf()
    listener = MyListener()
    browser = ServiceBrowser(zeroconf, TYPE, listener)

    try:
        input("Press enter to exit...\n\n")
    finally:
        zeroconf.close()
        subprocess.run(CMD_DEVICES, shell=True)


if __name__ == '__main__':
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment