最新消息:20210917 已从crifan.com换到crifan.org

【已解决】adb安装apk期间uiautomator2报错:RuntimeError USB device is offline

USB crifan 1333浏览 0评论
折腾:
【已解决】自动测试工具优化adb的无线WiFi连接网络切换支持之后的效果
期间,又先后遇到几次错误。
期间由于网络中断导致了报错:
[W 201217 16:00:37 __init__:203] atx-agent has something wrong, auto recovering
[D 201217 16:00:37 __init__:287] device 192.168.31.84:5555 is online
[I 201217 16:00:43 __init__:258] USB disconnected, fallback to WiFi, ATX_AGENT_URL=http://192.168.31.84:7912
[201217 16:00:43][AppCrawler.py 133] closed
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
        six.raise_from(e, None)
      File "<string>", line 3, in raise_from
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
        httplib_response = conn.getresponse()
      File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1322, in getresponse
        response.begin()
      File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
        version, status, reason = self._read_status()
      File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
        line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
      File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
        return self._sock.recv_into(b)
    socket.timeout: timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
        resp = conn.urlopen(
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 719, in urlopen
        retries = retries.increment(
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/util/retry.py", line 400, in increment
        raise six.reraise(type(error), error, _stacktrace)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/packages/six.py", line 735, in reraise
        raise value
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
        httplib_response = self._make_request(
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 423, in _make_request
        self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 330, in _raise_timeout
        raise ReadTimeoutError(
    urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='localhost', port=56443): Read timed out. (read timeout=3)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 290, in _prepare_atx_agent
        version = requests.get(version_url, timeout=3).text
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/api.py", line 75, in get
        return request('get', url, params=params, **kwargs)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/api.py", line 60, in request
        return session.request(method=method, url=url, **kwargs)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/sessions.py", line 533, in request
        resp = self.send(prep, **send_kwargs)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/sessions.py", line 646, in send
        r = adapter.send(request, **kwargs)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/adapters.py", line 529, in send
        raise ReadTimeout(e, request=request)
    requests.exceptions.ReadTimeout: HTTPConnectionPool(host='localhost', port=56443): Read timed out. (read timeout=3)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 121, in start
        self.set_InitialUrl()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 208, in set_InitialUrl
        self.doGameAutoTest()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 833, in doGameAutoTest
        self.gameAutomation_Manual()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 922, in gameAutomation_Manual
        self.getCurScreenshot()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/common/DevicesMethods.py", line 4891, in getCurScreenshot
        fullImgFilePath = self.driver.screenshot(fullImgFilePath) # 'debug/Android/app/游戏app/screenshot/20201208_205117.jpg'
      File "</Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/decorator.py:decorator-gen-10>", line 2, in screenshot
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/retry/api.py", line 73, in retry_decorator
        return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/retry/api.py", line 33, in __retry_internal
        return f()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 858, in screenshot
        r = self.http.get("/screenshot/0", timeout=10)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/sessions.py", line 546, in get
        return self.request('GET', url, **kwargs)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 208, in request
        self.__client._prepare_atx_agent()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 294, in _prepare_atx_agent
        self._setup_atx_agent()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 300, in _setup_atx_agent
        self._kill_process_by_name("atx-agent", use_adb=True)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 669, in _kill_process_by_name
        for p in self._iter_process(use_adb=use_adb):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 689, in _iter_process
        output = self._adb_device.shell("ps; ps -A")
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/adbutils/__init__.py", line 485, in shell
        ret = self._client.shell(self._serial, cmdargs, stream=stream, timeout=timeout)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/adbutils/__init__.py", line 241, in shell
        c.check_okay()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/adbutils/__init__.py", line 156, in check_okay
        raise AdbError(self.read_string())
    adbutils.errors.AdbError: closed
[W 201217 16:00:46 __init__:203] atx-agent has something wrong, auto recovering
Exception in thread watcher:
Traceback (most recent call last):
  File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/watcher.py", line 225, in _watch_forever
    triggered = self.run()
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/watcher.py", line 237, in run
    return self._run_watchers(source=source)
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/watcher.py", line 244, in _run_watchers
    source = source or self._dump_hierarchy()
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/watcher.py", line 173, in _dump_hierarchy
    return self._d.dump_hierarchy()
  File "</Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/decorator.py:decorator-gen-12>", line 2, in dump_hierarchy
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/retry/api.py", line 73, in retry_decorator
    return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/retry/api.py", line 33, in __retry_internal
    return f()
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 924, in dump_hierarchy
    content = self.jsonrpc.dumpWindowHierarchy(compressed, None)
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 435, in __call__
    return self.server._jsonrpc_retry_call(self.method, params,
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 442, in _jsonrpc_retry_call
    return self._jsonrpc_call(*args, **kwargs)
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 471, in _jsonrpc_call
    res = self.http.post("/jsonrpc/0",
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/requests/sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 208, in request
    self.__client._prepare_atx_agent()
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 286, in _prepare_atx_agent
    raise RuntimeError("USB device %s is offline" % self._serial)
RuntimeError: USB device 192.168.31.84:5555 is offline
只能重新测试了。
也是网络问题,不是代码问题。
刚才又出现一次同样问题:
        self.__client._prepare_atx_agent()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 286, in _prepare_atx_agent
        raise RuntimeError("USB device %s is offline" % self._serial)
    RuntimeError: USB device 192.168.31.84:5555 is offline
貌似网络虽然慢,但是是正常的?
不确定是否是网络问题导致的。
重试。
问题依旧:
用adb去安装apk:
[201217 16:23:00][DevicesMethods.py 4216] Run length=276, command: adb -s 192.168.31.84:5555 install -r -f xxx/20201217_fengyun_VivoGameXianXiaChuanQi5th10_gameApp_Android_14.apk
Performing Streamed Install
但是过了会报错:
[I 201217 16:24:15 __init__:258] USB disconnected, fallback to WiFi, ATX_AGENT_URL=http://192.168.31.84:7912
adb: failed to install xxx/20201217_fengyun_VivoGameXianXiaChuanQi5th10_gameApp_Android_14.apk:
且还有其他错误:
[I 201217 16:24:17 __init__:258] USB disconnected, fallback to WiFi, ATX_AGENT_URL=http://192.168.31.84:7912
[W 201217 16:24:17 __init__:203] atx-agent has something wrong, auto recovering
Exception in thread watcher:
Traceback (most recent call last):
  File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
。。。
    res = self.http.post("/jsonrpc/0",
  File "xxx/AppCrawler/venv/lib/python3.8/site-packages/requests/sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "xxx/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 208, in request
    self.__client._prepare_atx_agent()
  File "xxx/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 286, in _prepare_atx_agent
    raise RuntimeError("USB device %s is offline" % self._serial)
RuntimeError: USB device 192.168.31.84:5555 is offline
[I 201217 16:24:20 __init__:258] USB disconnected, fallback to WiFi, ATX_AGENT_URL=http://192.168.31.84:7912
[W 201217 16:24:22 __init__:203] atx-agent has something wrong, auto recovering
[201217 16:24:22][AppCrawler.py 133] USB device 192.168.31.84:5555 is offline
    Traceback (most recent call last):
      File "xxx/AppCrawler/src/AppCrawler.py", line 120, in start
        self.init_account(item)
。。。
        return self.request('POST', url, data=data, json=json, **kwargs)
      File "xxx/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 208, in request
        self.__client._prepare_atx_agent()
      File "xxx/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 286, in _prepare_atx_agent
        raise RuntimeError("USB device %s is offline" % self._serial)
    RuntimeError: USB device 192.168.31.84:5555 is offline
这次,貌似不是网络问题。
不过也看出点其他问题:
对于uiautomator2,显示出:
USB disconnected, fallback to WiFi, ATX_AGENT_URL=http://192.168.31.84:7912
那感觉是:
uiautomator2,把最开始的连接,当成USB有线连接了?
虽然表面上是用:
u2.connect(“192.168.31.84:5555")
去用无线WiFi连接的。
但是估计是内部判断成有线USB了。
所以后续才会这种报错的。
所以感觉有必要去,改为:
middleware/config.yml
# adb and uiautomator2 connect android phone use:
# isWirelessConnect: False # Wired USB cable
isWirelessConnect: True # Wireless WiFi
src/common/DevicesMethods.py
    def init_device_driver_Android(self):
        # 连接uiautomator2服务
        logging.info("start init driver")
        if self.config["isWirelessConnect"]:
            # self.driver = u2.connect_wifi(self.device)


            self.driver = u2.connect_adb_wifi(self.device)
            # Equals to
            # + Shell: adb connect 10.0.0.1:5555
            # + Python: u2.connect_usb("10.0.0.1:5555")
        else:
            self.driver = u2.connect(self.device)
去调试看看,看看后续,当网络断开后,是否还会提示:USB disconnected
输出:
[201217 16:36:53][DevicesMethods.py 88 ] start init driver
already connected to 192.168.31.84:5555
[201217 16:37:05][DevicesMethods.py 111] driverInfo={'currentPackageName': 'com.bbk.launcher2', 'displayHeight': 1459, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 800, 'displayWidth': 720, 'productName': 'PD2065', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
[201217 16:37:07][DevicesMethods.py 114] deviceInfo={'udid': '2e2a0cb1-b2:c6:b6:88:b2:2d-V2065A', 'version': '10', 'serial': '2e2a0cb1', 'brand': 'vivo', 'model': 'V2065A', 'hwaddr': 'b2:c6:b6:88:b2:2d', 'port': 7912, 'sdk': 29, 'agentVersion': '0.9.5', 'display': {'width': 720, 'height': 1600}, 'battery': {'acPowered': False, 'usbPowered': False, 'wirelessPowered': False, 'status': 3, 'health': 2, 'present': True, 'level': 84, 'scale': 100, 'voltage': 4165, 'temperature': 271, 'technology': 'Li-poly'}, 'memory': {'total': 5832196, 'around': '6 GB'}, 'cpu': {'cores': 8, 'hardware': 'Qualcomm Technologies, Inc BENGAL'}, 'arch': '', 'owner': None, 'presenceChangedAt': '0001-01-01T00:00:00Z', 'usingBeganAt': '0001-01-01T00:00:00Z', 'product': None, 'provider': None}
其中的:
already connected to 192.168.31.84:5555
说明内部的确调用了:
Shell: adb connect 10.0.0.1:5555
继续。

然后目前重试,是没问题的:
[201217 16:37:58][DevicesMethods.py 4208] file size: 698.6MB
[201217 16:37:58][DevicesMethods.py 4224] Run length=276, command: adb -s 192.168.31.84:5555 install -r -f /xxx/20201217_fengyun_VivoGameXianXiaChuanQi5th10_gameApp_Android_14.apk
Performing Streamed Install
[I 201217 16:39:12 watcher:255] XPath(hook:): ["//android.widget.LinearLayout[@resource-id='com.bbk.account:id/dialog_pwd']"]
[201217 16:39:12][DevicesMethods.py 253] Try auto input vivo password
[201217 16:39:12][DevicesMethods.py 258] True to found password dialog
[201217 16:39:14][DevicesMethods.py 262] Has input password to dialog
[201217 16:39:14][DevicesMethods.py 271] True to found 确定 button
[201217 16:39:15][DevicesMethods.py 274] Clicked 确定 for vivo password
[I 201217 16:39:21 watcher:255] XPath(hook:继续安装): ['继续安装']
Success
就看之后效果了:
是否还会出现,我觉得不应出现的:
USB disconnected
方面的提示。
结果很快,(可能是网络不稳定导致的?)
[W 201217 16:42:30 __init__:203] atx-agent has something wrong, auto recovering
Exception in thread watcher:
Traceback (most recent call last):
  File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/watcher.py", line 225, in _watch_forever
    triggered = self.run()
。。。
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 208, in request
    self.__client._prepare_atx_agent()
  File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/uiautomator2/__init__.py", line 286, in _prepare_atx_agent
    raise RuntimeError("USB device %s is offline" % self._serial)
RuntimeError: USB device 192.168.31.84:5555 is offline
又有错误了。
虽然没看到:USB disconnected
但是:USB device 192.168.31.84:5555 is offline
说明也是把设备当成USB有线连接了?
又或者是,只是提示不够友好,应该写成WiFi无线设备
但是内部代码逻辑其实都正常,都没问题?
继续多次测试看看。
暂时忽略上述警告,代码可以继续运行。
或许是已经解决了问题了。
并没有。
此处,后续uiautomator2没工作:
对于自动监测到并输入vivo密码 没生效:
所以,此处需要:
重启调试,或者想办法代码中加上支持:
能让uiautomator2重新连接才可以。
那去加上试试:
src/AppCrawler.py
    def gameAutomation_Manual(self):
。。。
            "5": "initDriver",
        }
。。。
                elif curOptionNum == "5":
                    logging.info("re-init %s driver", isConnected, self.platformType)
                    self.init_device_driver()
去调试看看效果:
期间又遇到:
[W 201217 17:18:35 __init__:203] atx-agent has something wrong, auto recovering
[D 201217 17:18:35 __init__:287] device 192.168.31.84:5555 is online
[D 201217 17:18:39 __init__:671] kill atx-agent
[I 201217 17:18:39 init:155] uiautomator2 version: 2.12.0
正好去试试:
输入5
[D 201217 17:18:39 __init__:671] kill atx-agent
[I 201217 17:18:39 init:155] uiautomator2 version: 2.12.0
5
[W 201217 17:25:05 __init__:203] atx-agent has something wrong, auto recovering
[D 201217 17:25:05 __init__:287] device 192.168.31.84:5555 is online
5
[D 201217 17:25:08 __init__:671] kill atx-agent
[I 201217 17:25:08 init:155] uiautomator2 version: 2.12.0
结果有问题。
没法触发代码调试。
算了。重新调试吧。
遇到了,切换WiFi,去调试:
[201217 17:29:48][AppCrawler.py 271] Wait long time 2.0 seconds
Action:
  1: markClickPaymentMoney
  2: end
  3: getScreenshot
  4: adbConnect
  5: initDriver


your input:4
[201217 17:29:57][DevicesMethods.py 1904] Try connect Android device: adb connect 192.168.31.84:5555
[201217 17:29:57][DevicesMethods.py 1907] console output: already connected to 192.168.31.84:5555
    
[201217 17:29:59][AppCrawler.py 939] True to connect current Android device 192.168.31.84:5555
Action:
  1: markClickPaymentMoney
  2: end
  3: getScreenshot
  4: adbConnect
  5: initDriver


your input:5
。。。
[201217 17:30:23][DevicesMethods.py 88 ] start init driver
already connected to 192.168.31.84:5555
是可以成功初始化driver,打印出信息的:
[201217 17:30:32][DevicesMethods.py 111] driverInfo={'currentPackageName': 'com.sj.sbcq.vivo', 'displayHeight': 1459, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 800, 'displayWidth': 720, 'productName': 'PD2065', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
[201217 17:30:33][DevicesMethods.py 114] deviceInfo={'udid': '2e2a0cb1-b2:c6:b6:88:b2:2d-V2065A', 'version': '10', 'serial': '2e2a0cb1', 'brand': 'vivo', 'model': 'V2065A', 'hwaddr': 'b2:c6:b6:88:b2:2d', 'port': 7912, 'sdk': 29, 'agentVersion': '0.9.5', 'display': {'width': 720, 'height': 1600}, 'battery': {'acPowered': False, 'usbPowered': False, 'wirelessPowered': False, 'status': 3, 'health': 2, 'present': True, 'level': 50, 'scale': 100, 'voltage': 3813, 'temperature': 304, 'technology': 'Li-poly'}, 'memory': {'total': 5832196, 'around': '6 GB'}, 'cpu': {'cores': 8, 'hardware': 'Qualcomm Technologies, Inc BENGAL'}, 'arch': '', 'owner': None, 'presenceChangedAt': '0001-01-01T00:00:00Z', 'usingBeganAt': '0001-01-01T00:00:00Z', 'product': None, 'provider': None}
即:
adb和uiautomator2的driver分别都可以初始化
继续调试,看看是否正常。
至少此处是没问题的。后续代码运行正常。
那可以再去加一个:
6: reConnectAdb_reInitU2Driver
"6": "adbConnect+initDriver”,

                elif curOptionNum == "6":
                    logging.info("Reconnect adb and reinit driver:")
                    isConnected = self.connectDevice()
                    logging.info("%s to connect current %s device %s", isConnected, self.platformType, self.device)
                    logging.info("re-init %s driver", self.platformType)
                    self.init_device_driver()
看看后续调试如何
目前看来没问题。

顺带也去看看
uiautomator2 RuntimeError USB device is offline
uiautomator2/__init__.py at master · openatx/uiautomator2
android adb devices offline的解决办法 – 清风软件测试 – 博客园
android – adb device offline with ADB wireless – Stack Overflow
android – Error: device offline – Stack Overflow
【总结】
此处,对于网络切换的,导致WiFi连接的adb和uiautomator2断开了,报错:
RuntimeError: USB device 192.168.31.84:5555 is offline
解决办法:
之前对于uiautomator2的初始化代码,变成显示指定wifi:
    def init_device_driver_Android(self):
        # 连接uiautomator2服务
        logging.info("start init driver")
        if self.config["isWirelessConnect"]:
            # self.driver = u2.connect_wifi(self.device)

            self.driver = u2.connect_adb_wifi(self.device)
            # Equals to 
            # + Shell: adb connect 10.0.0.1:5555
            # + Python: u2.connect_usb("10.0.0.1:5555")
        else:
            self.driver = u2.connect(self.device)

以及:
后续如果断开,再去:
输入6,然后:
                elif curOptionNum == "6":
                    logging.info("Reconnect adb and reinit driver:")
                    isConnected = self.connectDevice()
                    logging.info("%s to connect current %s device %s", isConnected, self.platformType, self.device)
                    logging.info("re-init %s driver", self.platformType)
                    self.init_device_driver()
  • 重新adb(通过WiFi)连接安卓设备
  • 重新初始化driver驱动 = uiautomator2的重新初始化
如此,即可解决:
当WiFi网络切换,导致adb和uiautomator2断开的问题。
【后记20201218】
当手机中uiautomator2没启动时,去调试:
self.driver = u2.connect_adb_wifi(self.device)
则会触发:
already connected to 192.168.31.84:5555
[W 201218 10:55:43 __init__:203] atx-agent has something wrong, auto recovering
[D 201218 10:55:43 __init__:287] device 192.168.31.84:5555 is online
[I 201218 10:55:43 init:155] uiautomator2 version: 2.12.0
[201218 10:55:44][api.py 40 ] ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')), retrying in 0.5 seconds...
继续调试看看
[D 201218 10:57:53 __init__:600] kill process(ps): uiautomator
[D 201218 10:57:54 __init__:618] uiautomator-v2 is starting ... left: 40.0s
[D 201218 10:57:54 __init__:600] kill process(ps): uiautomator
[D 201218 10:57:55 __init__:618] uiautomator-v2 is starting ... left: 38.9s
[D 201218 10:57:56 __init__:618] uiautomator-v2 is starting ... left: 40.0s
[I 201218 10:57:56 __init__:572] restart-uiautomator since "uiautomator2.GatewayError(gateway error, time used 0.1s)"
[D 201218 10:57:56 __init__:600] kill process(ps): uiautomator
[D 201218 10:57:56 __init__:654] grant permissions
[D 201218 10:57:57 __init__:618] uiautomator-v2 is starting ... left: 39.0s
[D 201218 10:57:57 __init__:618] uiautomator-v2 is starting ... left: 40.0s
[I 201218 10:57:57 __init__:572] restart-uiautomator since "uiautomator2.GatewayError(gateway error, time used 0.1s)"
[D 201218 10:57:57 __init__:600] kill process(ps): uiautomator
[D 201218 10:57:58 __init__:654] grant permissions
[D 201218 10:57:58 __init__:618] uiautomator-v2 is starting ... left: 38.9s
[D 201218 10:57:59 __init__:618] uiautomator-v2 is starting ... left: 40.0s
[D 201218 10:57:59 __init__:618] uiautomator-v2 is starting ... left: 37.9s
[D 201218 10:58:00 __init__:618] uiautomator-v2 is starting ... left: 39.0s
[D 201218 10:58:01 __init__:618] uiautomator-v2 is starting ... left: 36.4s
[D 201218 10:58:01 __init__:618] uiautomator-v2 is starting ... left: 37.8s
[I 201218 10:58:01 __init__:583] uiautomator back to normal
[I 201218 10:58:01 __init__:583] uiautomator back to normal
最后可以连上设备,输出设备信息了
[201218 10:58:01][DevicesMethods.py 111] driverInfo={'currentPackageName': 'com.bbk.launcher2', 'displayHeight': 1459, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 800, 'displayWidth': 720, 'productName': 'PD2065', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
[201218 10:58:01][DevicesMethods.py 114] deviceInfo={'udid': '2e2a0cb1-b2:c6:b6:88:b2:2d-V2065A', 'version': '10', 'serial': '2e2a0cb1', 'brand': 'vivo', 'model': 'V2065A', 'hwaddr': 'b2:c6:b6:88:b2:2d', 'port': 7912, 'sdk': 29, 'agentVersion': '0.9.5', 'display': {'width': 720, 'height': 1600}, 'battery': {'acPowered': True, 'usbPowered': False, 'wirelessPowered': False, 'status': 2, 'health': 2, 'present': True, 'level': 76, 'scale': 100, 'voltage': 4199, 'temperature': 275, 'technology': 'Li-poly'}, 'memory': {'total': 5832196, 'around': '6 GB'}, 'cpu': {'cores': 8, 'hardware': 'Qualcomm Technologies, Inc BENGAL'}, 'arch': '', 'owner': None, 'presenceChangedAt': '0001-01-01T00:00:00Z', 'usingBeganAt': '0001-01-01T00:00:00Z', 'product': None, 'provider': None}
即可继续正常调试。

转载请注明:在路上 » 【已解决】adb安装apk期间uiautomator2报错:RuntimeError USB device is offline

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.130 seconds, using 21.80MB memory