通过USB连接检查USB调试状态

时间:2015-10-21 10:37:34

标签: android linux shell debugging adb

在通过 USB 进行连接时,查找检查开发人员选项设置状态中 USB调试 打开/关闭(任何状态信息)的方法

最近检查了adb shell getprop没有结果。

干杯

2 个答案:

答案 0 :(得分:2)

如果关闭USB调试,则无法使用ADB,因此您无法通过它获取任何信息。

  

要将adb与通过USB连接的设备一起使用,您必须启用   在开发人员选项下的设备系统设置中进行USB调试。

     

参考:
  Date Object

您可以致电adb devices并查看结果。如果列表为空,则设备未启用该选项。否则结果将是这样的:

List of devices attached
XXXXXXXXXXXXXX  device

答案 1 :(得分:0)

在航站楼

unique

其中 adb devices -l List of devices attached 2e2a0cb1 device usb:338952192X product:PD2065 model:V2065A device:PD2065 transport_id:3 ... 表示:Android设备已 USB 已连接

通过python代码

usb

通话演示:

import re
import subprocess
import logging

def isAndroidUsbConnected(self, deviceSerialId):
    """Check whether android device is currently USB wired connected or not

    Args:
        deviceSerialId (str): android devivce serial id
    Returns:
        connected or not (bool)
    Raises:
    Examples:
        input: "orga4pmzee4ts47t"
        output: True
    """
    isUsbConnected = False
    isRealSerialId = re.search("\w+", deviceSerialId)
    if not isRealSerialId:
        # makesure is not wifi, such as: 192.168.31.84:5555
        logging.error("Invalid android USB wired connected device serial id %s", deviceSerialId)
        return isUsbConnected

    deviceDetailList = getAndroidDeviceList(isGetDetail=True)
    for eachDevDetailDict in deviceDetailList:
        curDevSerialStr, curDevDetailDict = list(eachDevDetailDict.items())[0]
        if deviceSerialId == curDevSerialStr:
            detailInfoKeyList = list(curDevDetailDict.keys())
            # ['usb', 'product', 'model', 'device', 'transport_id']
            if "usb" in detailInfoKeyList:
                isUsbConnected = True
            break

    return isUsbConnected

def getAndroidDeviceList(self, isGetDetail=False):
    """Get android device list

    Args:
        isGetDetail (bool): True to use `adb devices -l`, False to use `adb devices`
    Returns:
        device list(list)
    Raises:
    Examples:
        output: 
            False -> ["2e2a0cb1", "orga4pmzee4ts47t", "192.168.31.84:5555"]
            True -> [{'2e2a0cb1': {'usb': '338952192X', 'product': 'PD2065', 'model': 'V2065A', 'device': 'PD2065', 'transport_id': '4'}}, {'orga4pmzee4ts47t': {'usb': '338886656X', 'product': 'atom', 'model': 'M2004J7AC', 'device': 'atom', 'transport_id': '24'}}, {'192.168.31.84:5555': {'product': 'PD2065', 'model': 'V2065A', 'device': 'PD2065', 'transport_id': '5'}}]
    """
    deviceList = []

    getDevicesCmd = 'adb devices'
    if isGetDetail:
        getDevicesCmd += " -l"

    deviceLines = get_cmd_lines(getDevicesCmd)

    """
    adb devices :
        List of devices attached
        2e2a0cb1    device
        orga4pmzee4ts47t    device
        192.168.31.84:5555  device
    """

    """
    adb devices -l:
        List of devices attached
        2e2a0cb1               device usb:338952192X product:PD2065 model:V2065A device:PD2065 transport_id:4
        orga4pmzee4ts47t       device usb:338886656X product:atom model:M2004J7AC device:atom transport_id:24
        192.168.31.84:5555     device product:PD2065 model:V2065A device:PD2065 transport_id:5
    """

    for eachLine in deviceLines:
        if not eachLine:
            continue

        if "devices attached" in eachLine:
            continue

        foundDevice = re.search("(?P<devSerial>[\w\.\:]+)\s+device\s*(?P<devDetail>[\w\: ]+)", eachLine)
        # foundDevice=<re.Match object; span=(0, 101), match='2e2a0cb1               device usb:338952192X prod>
        if foundDevice:
            devSerial = foundDevice.group("devSerial")
            # devSerial=2e2a0cb1
            if isGetDetail:
                devDetail = foundDevice.group("devDetail")
                # devDetail=usb:338952192X product:PD2065 model:V2065A device:PD2065 transport_id:4
                keyValueIter = re.finditer("(?P<key>\w+):(?P<value>\w+)", devDetail) # <callable_iterator object at 0x10baa3a60>
                keyValueMatchList = list(keyValueIter)
                # keyValueMatchList=[<re.Match object; span=(0, 14), match='usb:338952192X'>, <re.Match object; span=(15, 29), match='product:PD2065'>, <re.Match object; span=(30, 42), match='model:V2065A'>, <re.Match object; span=(43, 56), match='device:PD2065'>, <re.Match object; span=(57, 71), match='transport_id:4'>]
                detailInfoDict = {}
                for eachMatch in keyValueMatchList:
                    eachKey = eachMatch.group("key")
                    eachValue = eachMatch.group("value")
                    detailInfoDict[eachKey] = eachValue
                # detailInfoDict={'usb': '338952192X', 'product': 'PD2065', 'model': 'V2065A', 'device': 'PD2065', 'transport_id': '4'}
                curDevDetailDict = {
                    devSerial: detailInfoDict
                }
                # curDevDetailDict={'2e2a0cb1': {'usb': '338952192X', 'product': 'PD2065', 'model': 'V2065A', 'device': 'PD2065', 'transport_id': '4'}}
                deviceList.append(curDevDetailDict)
            else:
                deviceList.append(devSerial)

    # deviceList=[{'2e2a0cb1': {'usb': '338952192X', 'product': 'PD2065', 'model': 'V2065A', 'device': 'PD2065', 'transport_id': '4'}}, {'orga4pmzee4ts47t': {'usb': '338886656X', 'product': 'atom', 'model': 'M2004J7AC', 'device': 'atom', 'transport_id': '24'}}, {'192.168.31.84:5555': {'product': 'PD2065', 'model': 'V2065A', 'device': 'PD2065', 'transport_id': '5'}}]
    return deviceList

def get_cmd_lines(cmd, text=False, timeout=2):
    # 执行cmd命令,将结果保存为列表
    resultStr = ""
    resultStrList = []
    try:
        # consoleOutputByte = subprocess.check_output(cmd, shell=True) # b'C02Y3N10JHC8\n'
        consoleOutputByte = subprocess.check_output(cmd, shell=True, timeout=timeout) # b'C02Y3N10JHC8\n'
        try:
            resultStr = consoleOutputByte.decode("utf-8")
        except UnicodeDecodeError:
            # TODO: use chardet auto detect encoding
            # consoleOutputStr = consoleOutputByte.decode("gbk")
            resultStr = consoleOutputByte.decode("gb18030")

        if not text:
            resultStrList = resultStr.splitlines()
    except Exception as err:
        print("err=%s when run cmd=%s" % (err, cmd))

    if text:
        return resultStr
    else:
        return resultStrList

最新代码位于: