openatx / adbutils

pure python adb library for google adb service.
MIT License
729 stars 173 forks source link

[Feature request] Provides the usb-device-to-tcp function #114

Open codematrixer opened 2 months ago

codematrixer commented 2 months ago

adbkit provides a powerful feature: usb-device-to-tcp. Execute the following command to use it node node_modules/adbkit/bin/adbkit usb-device-to-tcp -p ${PORT} ${SERIAL} However, due to the complexity of the Node.js environment and its version requirements, it's difficult to maintain.

Is it possible to implement this feature in adbutils?

codeskyblue commented 2 months ago

Sure it is possible to do it

codeskyblue commented 2 months ago

The adbutils need make some update to support asyncio

chaooe commented 2 months ago

i am also waiting for this feature. and i also like the adbkit provide by you.

chaooe commented 2 months ago

a simple way that using the exist installed adbkit and can be only used one device connected.

from adbutils import adb
import os
import subprocess

global connected_device
connected_device =[]
# 监控设备连接 track-devices
os.system("adb start-server")
while True:
    try:
        for event in adb.track_devices():
            if "emulator" in event.serial or "0123456789ABCDEF" in event.serial:
                continue
            print(event.present, event.serial, event.status)
            if event.status == 'device':
                connected_device.append(event.serial)
                cmd = 'adbkit usb-device-to-tcp -p 5555 {}'.format(event.serial)
                p = subprocess.Popen(cmd, shell=True)
                print(f"set the device {event.serial} to tcp port 5555")
            if event.serial in connected_device and event.status !="device":
                connected_device.remove(event.serial)
                if p is not None:
                    p.terminate()
                    p = None
                print(f"set the device {event.serial} is offline")
    except InterruptedError:
        break
    except Exception as e:
        print(f'adb killed server:{e}')
        if connected_device !=[]:
            connected_device=[]
        if p is not None:
            p.terminate()
            p = None
        os.system("adb start-server")
    finally:
        if p is not None:
            p.terminate()
            p = None
        os.system("adb start-server")