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

【已解决】Mac中如何用adb connect连接和检测无线WiFi的安卓手机是否已连接

Mac crifan 428浏览 0评论
折腾:
【已解决】给自动测试工具项目代码中加上判断安卓手机设备是否已连接的逻辑
期间,去实现,Mac中用adb connect的方式去连接和检测当前无线WiFi设备是否已连接
src/common/DevicesMethods.py
    def connectDevice(self):
        """连接/重连 Android/iOS 设备"""
        isConnected = False
        if self.isAndroid:
            adbConnectCmd = "adb connect %s" % self.device
            logging.info("Try connect Android device: %s", adbConnectCmd)
            # os.system(adbConnectCmd) # when failed, will wait too long time: ~ 1 minutes
            cmdOutputStr = CommonUtils.get_cmd_lines(adbConnectCmd, text=True, timeout=1)
            if cmdOutputStr:
                if "connected" in cmdOutputStr:
                    isConnected = True
                elif "failed" in cmdOutputStr:
                    isConnected = False
            else:
                isConnected = False
        elif self.isiOS:
            logging.warning("TODO: add support start iOS device")


        return isConnected
重试
即可:
[201217 13:24:11][AppCrawler.py 32 ] Android device 192.168.31.84:5555 not connected !
src/AppCrawler.py
        # makesure device connected
        if self.connectDevice():
            logging.info("%s device %s connected", self.platformType, self.device)
        else:
            errMsg = "%s device %s not connected !" % (self.platformType, self.device)
            logging.error(errMsg)
            raise Exception(errMsg)
重新调试 :
可以跑出异常了:
发生异常: Exception
Android device 192.168.31.84:5555 not connected !
符合预期。
然后对于此处,最开始初始化的时候,是可以加上重试的
        # makesure device connected
        isConnected = False
        MaxRetryNum = 3
        curRetryNum = 1
        while curRetryNum <= MaxRetryNum:
            isConnected = self.connectDevice()
            if isConnected:
                break

            curRetryNum += 1
        
        if isConnected:
            logging.info("%s device %s connected", self.platformType, self.device)
        else:
            errMsg = "%s device %s not connected after %d retry !" % (self.platformType, self.device, MaxRetryNum)
            logging.error(errMsg)
            raise Exception(errMsg)
结果:
[201217 13:29:09][DevicesMethods.py 1896] Try connect Android device: adb connect 192.168.31.84:5555
err=Command 'adb connect 192.168.31.84:5555' timed out after 1 seconds when run cmd=adb connect 192.168.31.84:5555
突然发现,貌似超时时间太短?
又发现,好像不是,1秒足够正常连接了。
发生异常: Exception
Android device 192.168.31.84:5555 not connected after 3 retry !
符合预期
那再去把WiFi切换回正常的guest 5G:
再去测试,这下应该能连上才对。
Try connect Android device: adb connect 192.168.31.84:5555
返回:
'connected to 192.168.31.84:5555\n'
是正常连上了。
然后后续也可以获取到phone的name了:
加上代码:
            cmdOutputStr = CommonUtils.get_cmd_lines(adbConnectCmd, text=True, timeout=1)
            logging.info("console output: %s", cmdOutputStr)
            # connected to 192.168.31.84:5555
            # already connected to 192.168.31.84:5555
输出:
[201217 13:36:18][DevicesMethods.py 1896] Try connect Android device: adb connect 192.168.31.84:5555
[201217 13:36:19][DevicesMethods.py 1899] console output: already connected to 192.168.31.84:5555
即可。
【后记 20201218】
折腾:
【已解决】Mac中如何用adb检查确保USB有线连接安卓手机
期间,进一步优化。
去调试看看
输出:
[201218 16:27:04][DevicesMethods.py 2032] Try connect Android device: adb connect 192.168.31.84:5555
[201218 16:27:05][DevicesMethods.py 2035] console output: already connected to 192.168.31.84:5555
是我们希望的。
最后是:
    def androidConnectWiFiDevice(self, wifiSerial):
        """Use Android `adb connect` to connect WiFi wireless devive


        Args:
            wifiSerial (str): android devivce WiFi serial, eg: 192.168.31.84:5555
        Returns:
            connect ok or not (bool)
        Raises:
        Examples:
            input: "192.168.31.84:5555"
            output: True
        """
        isConnectOk = False


        adbConnectCmd = "adb connect %s" % wifiSerial
        logging.info("Try connect Android device: %s", adbConnectCmd)
        # os.system(adbConnectCmd) # when failed, will wait too long time: ~ 1 minutes
        cmdOutputStr = CommonUtils.get_cmd_lines(adbConnectCmd, text=True, timeout=1)
        logging.info("console output: %s", cmdOutputStr)
        # connected to 192.168.31.84:5555
        # already connected to 192.168.31.84:5555
        # failed to connect to '192.168.31.84:5555': Operation timed out
        # "failed to connect to '192.168.31.84:5555': Connection refused\n"
        # err=Command 'adb connect 192.168.31.84:5555' timed out after 1 seconds when run cmd=adb connect 192.168.31.84:5555
        if cmdOutputStr:
            if "connected" in cmdOutputStr:
                isConnectOk = True
            elif ("failed" in cmdOutputStr) or ("timed out" in cmdOutputStr):
                isConnectOk = False
        else:
            isConnectOk = False


        return isConnectOk
调用:
self.device="192.168.31.84:5555"
isWiFiConnected = self.androidConnectWiFiDevice(self.device)
即可。
以及相关工具类函数:
CommonUtils.get_cmd_lines
等价于:
https://github.com/crifan/crifanLibPython/blob/master/crifanLib/crifanSystem.py
中的:getCommandOutput

转载请注明:在路上 » 【已解决】Mac中如何用adb connect连接和检测无线WiFi的安卓手机是否已连接

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.197 seconds, using 23.42MB memory