JeffLIrion / adb_shell

A Python implementation of ADB with shell and FileSync functionality.
Apache License 2.0
530 stars 60 forks source link

How to connect more than two devices at the same time? #207

Open lp1293714059 opened 2 years ago

lp1293714059 commented 2 years ago

Creating two AdbDeviceUsb objects with different SNs will report an error:

from adb_shell.adb_device import AdbDeviceTcp, AdbDeviceUsb
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

# Load the public and private keys
adbkey = 'adbkey'
with open(adbkey) as f:
    priv = f.read()
with open(adbkey + '.pub') as f:
     pub = f.read()
signer = PythonRSASigner(pub, priv)

# Connect via USB (package must be installed via `pip install adb-shell[usb])`
device2 = AdbDeviceUsb(serial="230YV16C83002B")
device2.connect(rsa_keys=[signer], auth_timeout_s=0.1)

device3 = AdbDeviceUsb(serial="230YT16C83002G")
device3.connect(rsa_keys=[signer], auth_timeout_s=0.1)
Traceback (most recent call last):
  File "testDUTS.py", line 16, in <module>
    device3 = AdbDeviceUsb(serial="230YT16C83002G")
  File "C:\python37\lib\site-packages\adb_shell\adb_device.py", line 1552, in __init__
    transport = UsbTransport.find_adb(serial, port_path, default_transport_timeout_s)
  File "C:\python37\lib\site-packages\adb_shell\transport\usb_transport.py", line 628, in find_adb
    default_transport_timeout_s=default_transport_timeout_s
  File "C:\python37\lib\site-packages\adb_shell\transport\usb_transport.py", line 512, in _find
    return cls._find_first(setting_matcher, device_matcher, usb_info=usb_info, default_transport_timeout_s=default_transport_timeout_s)
  File "C:\python37\lib\site-packages\adb_shell\transport\usb_transport.py", line 601, in _find_first
    return next(cls._find_devices(setting_matcher, device_matcher=device_matcher, usb_info=usb_info, default_transport_timeout_s=default_transport_timeout_s))
  File "C:\python37\lib\site-packages\adb_shell\transport\usb_transport.py", line 569, in _find_devices
    if device_matcher is None or device_matcher(transport):
  File "C:\python37\lib\site-packages\adb_shell\transport\usb_transport.py", line 475, in <lambda>
    return lambda device: device.serial_number == serial
  File "C:\python37\lib\site-packages\adb_shell\transport\usb_transport.py", line 415, in serial_number
    return self._device.getSerialNumber()
  File "C:\python37\lib\site-packages\usb1\__init__.py", line 2019, in getSerialNumber
    return self.open().getSerialNumber()
  File "C:\python37\lib\site-packages\usb1\__init__.py", line 2055, in open
    mayRaiseUSBError(libusb1.libusb_open(self.device_p, byref(handle)))
  File "C:\python37\lib\site-packages\usb1\__init__.py", line 127, in mayRaiseUSBError
    __raiseUSBError(value)
  File "C:\python37\lib\site-packages\usb1\__init__.py", line 119, in raiseUSBError
    raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)
usb1.USBErrorAccess: LIBUSB_ERROR_ACCESS [-3]
lp1293714059 commented 2 years ago

I found that there is no error with this code order, Is it okay to execute code in a certain order?:

device3 = AdbDeviceUsb(serial="230YT16C83002G")  # COM3
device3.connect(rsa_keys=[signer], auth_timeout_s=0.1)

device2 = AdbDeviceUsb(serial="230YV16C83002B")  # COM5
device2.connect(rsa_keys=[signer], auth_timeout_s=0.1)

I tried to connect three android devices at the same time, I found that it can only be executed in the following code sequence without error:

device3 = AdbDeviceUsb(serial="230YT16C83002G")  # COM3
device3.connect(rsa_keys=[signer], auth_timeout_s=0.1)

device4 = AdbDeviceUsb(serial="230YV41D4W001J")  # COM17
device4.connect(rsa_keys=[signer], auth_timeout_s=0.1)

device2 = AdbDeviceUsb(serial="230YV16C83002B")  # COM5
device2.connect(rsa_keys=[signer], auth_timeout_s=0.1)

From the above phenomenon, it seems that there is no correlation with the order of COM ports. I am very confused. @JeffLIrion Could you help me solve this problem?o(╥﹏╥)o

X123465 commented 1 year ago

Bro, have you solved this problem?

lp1293714059 commented 1 year ago

Bro, have you solved this problem?

I havent solved it. But I found that the code can run normally like this:

device3 = AdbDeviceUsb(serial="230YT16C83002G")  # COM3
device4 = AdbDeviceUsb(serial="230YV41D4W001J")  # COM17
device2 = AdbDeviceUsb(serial="230YV16C83002B")  # COM5

device3.connect(rsa_keys=[signer], auth_timeout_s=0.1)
device4.connect(rsa_keys=[signer], auth_timeout_s=0.1)
device2.connect(rsa_keys=[signer], auth_timeout_s=0.1)

It seems that all AdbDeviceUsb objects need to be created first, and then connect them, so that no error will be reported.