Windows上是否有adb等待设备超时的方法?

ljo96ir5  于 2023-06-24  发布在  Windows
关注(0)|答案(2)|浏览(158)

我想用限时的adb wait-for-device,但是好像没有超时功能,
我的解决方法如下,有没有更有效的方法来实现它?

#<python on Windows>
t0 = datetime.datetime.now()
cmd = 'date'
cmd = 'adb -s %s shell \"'+ cmd+ '\"'
r = 1
while(r!=0 and (t1-t0).seconds < 60):
    r = os.system(cmd)
    t1 = datetime.datetime.now()
toiithl6

toiithl61#

没有特定的Timeout方法可用,但您可以尝试

def wait_for_device(self, timeout=5):
    """
    Perform `adb wait-for-device` command

    Args:
        timeout: time interval in seconds to wait for device

    Raises:
        DeviceConnectionError: if device is not available after timeout

    Returns:
        None

    """
    try:
        self.cmd("wait-for-device", timeout=timeout)
    except RuntimeError as e:
        raisefrom(DeviceConnectionError, "device not ready", e)
a7qyws3x

a7qyws3x2#

PowerShell Wait-* cmdlet具有可以使用的超时选项。您可以使用Wait-Job来完成此操作

$timeout = 60
$job = sajb { adb wait-for-device }
if (-not ($job | wjb -T $timeout)) { spjb $job }

您可以将其放入配置文件中以供重用。在PowerShell中运行notepad $PROFILE并粘贴以下函数

Function Invoke-WithTimeOut {
    Param (
        [int]$timeout,
        [scriptblock]$script
    )

    $job = Start-Job $script
    $done = $job | Wait-Job -TimeOut $timeout
    if (-not $done) { Stop-Job $job }
}

例如,以40 s超时运行

Invoke-WithTimeOut 40 { adb wait-for-device }

另一个内置解决方案是ScriptRunner.exe,但它会在新窗口中打开进程

ScriptRunner.exe -appvscript path\to\platform-tools\adb.exe wait-for-device -appvscriptrunnerparameters -wait -timeout=60

行为与使用Start-ProcessWait-Process而不是Start-Job/Wait-Job运行上述PowerShell代码段时相同

相关问题