You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用adb reboot命令报错求助(设备重启但代码报错)

Python执行adb reboot返回错误码255但设备正常重启的解决办法

问题描述

在Python代码中执行adb reboot命令时,会返回错误码255,但设备实际能完成重启;直接在终端执行该adb命令则完全正常。推测是设备重启时adb连接中断导致报错,尝试过多种方案无效,以下是完整代码及输出:

原代码

import subprocess


def execute_adb(adb_command):
    # print(adb_command)
    result = subprocess.run(
        adb_command,
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    if result.returncode == 0:
        return result.stdout.strip()
    print(f"returncode {result.returncode}, stderr: {result.stderr.lower()}, stdout: {result.stdout}")
    return "ERROR"

class AndroidController:
    def __init__(self, device):
        self.device = device

    def is_device_online(self):
        adb_command = f"adb -s {self.device} get-state"
        result = execute_adb(adb_command)
        return result == "device"

    def reset_device(self):
        adb_command = f"adb -s {self.device} shell reboot"
        ret = execute_adb(adb_command)
        return ret

controller = AndroidController("emulator-5554")
print(controller.is_device_online())
controller.reset_device()

执行输出

True
returncode 255, stderr: , stdout: 

Process finished with exit code 0

问题原因

错误码255并非reboot命令执行失败,而是设备执行重启后,adb连接被强制中断,subprocess捕获到连接中断信号并判定为错误返回。但实际上adb shell reboot命令已成功发送,设备也正常执行了重启操作。

解决办法

针对reboot命令的特殊性,可通过以下两种方式处理:

方案1:忽略reboot命令的255错误码

修改execute_adb函数,允许传入允许的错误码列表,针对reboot命令忽略255错误:

import subprocess
import time


def execute_adb(adb_command, allowed_codes=None):
    allowed_codes = allowed_codes or []
    result = subprocess.run(
        adb_command,
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    if result.returncode == 0 or result.returncode in allowed_codes:
        return result.stdout.strip()
    print(f"returncode {result.returncode}, stderr: {result.stderr.lower()}, stdout: {result.stdout}")
    return "ERROR"

class AndroidController:
    def __init__(self, device):
        self.device = device

    def is_device_online(self):
        adb_command = f"adb -s {self.device} get-state"
        result = execute_adb(adb_command)
        return result == "device"

    def reset_device(self):
        adb_command = f"adb -s {self.device} shell reboot"
        # 允许reboot命令返回255错误码
        ret = execute_adb(adb_command, allowed_codes=[255])
        # 验证设备是否完成重启
        print("等待设备重启...")
        time.sleep(3)
        while not self.is_device_online():
            time.sleep(1)
        print("设备已重启完成")
        return ret

controller = AndroidController("emulator-5554")
print(controller.is_device_online())
controller.reset_device()

方案2:单独处理reboot命令的执行逻辑

直接在reset_device方法中执行命令,不依赖通用的execute_adb错误判断,后续通过检查设备在线状态确认重启完成:

import subprocess
import time


def execute_adb(adb_command):
    result = subprocess.run(
        adb_command,
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    if result.returncode == 0:
        return result.stdout.strip()
    print(f"returncode {result.returncode}, stderr: {result.stderr.lower()}, stdout: {result.stdout}")
    return "ERROR"

class AndroidController:
    def __init__(self, device):
        self.device = device

    def is_device_online(self):
        adb_command = f"adb -s {self.device} get-state"
        result = execute_adb(adb_command)
        return result == "device"

    def reset_device(self):
        adb_command = f"adb -s {self.device} shell reboot"
        # 直接执行命令,不检查返回码
        subprocess.run(
            adb_command,
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
        )
        # 等待设备离线并重新上线
        print("等待设备重启...")
        time.sleep(3)
        while not self.is_device_online():
            time.sleep(1)
        print("设备已重启完成")
        return "SUCCESS"

controller = AndroidController("emulator-5554")
print(controller.is_device_online())
controller.reset_device()

说明

两种方案的核心都是:承认reboot命令返回255是正常现象(设备重启必然导致adb连接中断),通过后续检查设备在线状态,确保设备确实完成重启,避免误判命令执行失败。

内容的提问来源于stack exchange,提问作者Janek Podwysocki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 02:46:03