python+adb

embedded/2024/9/23 22:41:02/
python">#!/usr/bin/python env
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
from time import sleepimport logging
logging.basicConfig(level=logging.DEBUG)
class ScreenCapture():def get_screen_size(self):"""获取手机分辨率"""action = subprocess.Popen(['adb', 'shell', 'wm', 'size'], stdout=subprocess.PIPE)result = action.stdout.read()_, size = result.decode().split(':')self.width,self.height = size.strip().split('x')print(f'获取到手机分辨率为:{self.width},{self.height}')  # 1080,2436# return int(width), int(height)# print(f"result是{result}")# print(result.decode())# ss=result.decode().split(':')# aaa=ss[1].strip()self.x1=int(self.width)/2self.x2=int(self.width)/2self.y1=int(self.height)/4self.y2=int(self.height)/1.5print(f"x1是{self.x1},x2是{self.x2},y1是{self.y1},y2是{self.y2}")@staticmethoddef show_menu():print("========================================================== ")print("||      <1>远程连接设备       <2>向左滑动       <3>向右滑动    ||\n")print("||      <4>向上滑动           <5>向下滑动       <6>跑Monkey    ||\n")print("||      <7>单场景chrome测试   <8>               <9>          ||\n")print(" ========================================================= \n")# subprocess.Popen(#     ['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],#     stdout=subprocess.PIPE)# return int(width), int(height)# self.ScreenCapture(y1, y2, height)# subprocess.Popen(['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],stdout=subprocess.PIPE)def up_swipe(self):up_num = int(input("向上滑动多少次,用数字替代:"))interval_time = int(input("输入间隔的时间:"))for i in range(up_num):subprocess.Popen(['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],stdout=subprocess.PIPE)print("第{}次滑动".format(i + 1))sleep(interval_time)print("滑动结束,退出。。")sys.exit()def down_swipe(self):up_num = int(input("向下滑动多少次,用数字替代:"))interval_time=int(input("输入间隔的时间:"))for i in range(up_num):subprocess.Popen(['adb', '-s',{self.ip_input},'shell', 'input', 'swipe', str(self.x1), str(self.y2), str(self.x1), str(self.y1), '500'],stdout=subprocess.PIPE)sleep(interval_time)print("滑动结束,退出。。")sys.exit()def left_swipe(self):x1=str(int(self.width)*0.8)x2=str(int(self.width)*0.2)y1=str(int(self.height)*0.5)print(x1)print("滑动开始前")up_num = int(input("向左滑动多少次,用数字替代:"))for i in range(up_num):subprocess.Popen(['adb', 'shell', 'input', 'swipe', x2, y1, x1, y1, '500'],stdout=subprocess.PIPE)print("第{}次滑动".format(i+1))sleep(3)print("滑动结束,退出。。")sys.exit()def right_swipe(self):x1 = str(int(self.width) * 0.8)x2 = str(int(self.width) * 0.2)y1 = str(int(self.height) * 0.5)print(x1)print("滑动开始前")up_num = int(input("向左滑动多少次,用数字替代:"))for i in range(up_num):subprocess.Popen(['adb', 'shell', 'input', 'swipe', x1, y1, x2, y1, '300'],stdout=subprocess.PIPE)print("第{}次滑动".format(i + 1))sleep(3)print("滑动结束,退出。。")sys.exit()def get_device_id(self):self.adb_devices=os.popen("adb devices").read()self.adb_output = self.adb_devices.split("List of devices attached\n", 1)[1]lines = self.adb_output.strip().split('\n')# 遍历每一行,提取设备号self.device_ids = []for line in lines:# 如果行不是空的,并且包含"device",则提取设备号if line and "device" in line:device_id, _ = line.split(maxsplit=1)  # maxsplit=1确保只分割一次self.device_ids.append(device_id.strip())self.device_ids=self.device_ids[0]logging.info(f'device_ids号是: {self.device_ids}')return self.device_idsdef adb_connect(self, device_ip):adb_command = ['adb', 'connect', f'{device_ip}:5555']try:subprocess.Popen("adb tcpip 5555", stdout=subprocess.PIPE)# subprocess.check_call("adb -s {} tcp ip 5555", stdout=subprocess.PIPE)  #output = subprocess.check_output(adb_command, shell=True, stderr=subprocess.STDOUT)print(f"Connected to device at {device_ip}:5555")print(output.decode())except:print(f"Failed to connect to device at {device_ip}:5555", "请检查错误")sys.exit()def get_and_connect_ip(self):# 将原本在if num == 1下的代码移到这个方法中adb deip_command = f'adb -s {self.get_device_id()} shell ip route'result = subprocess.check_output(ip_command, shell=True, text=True)# 提取 IP 地址(这里假设IP地址是route命令输出的最后一行最后一个元素)ip_address = result.strip('\n').strip(' ').split(' ')[-1]print(f"Detected IP address: {ip_address},远程连接需拔掉USB设备")self.ip_input = input("输入你的IP地址(或者按回车使用检测到的IP):")if not self.ip_input:self.ip_input = ip_address  # 如果用户没有输入,则使用检测到的IPself.adb_connect(self.ip_input)self.run() # 连接后重新显示菜单def chrome_switch(self):os.system("adb devices")while True:os.system(f'adb -s {self.ip_input} shell am start -a android.intent.action.VIEW -d "http://baidu.com"')sleep(5)for i in range(20):subprocess.Popen(['adb', '-s',self.ip_input,'shell', 'input', 'swipe', str(self.x1), str(self.y2),str(self.x1), str(self.y1), '500'],stdout=subprocess.PIPE)sleep(3)os.system(f'adb -s {self.ip_input} shell am start -a android.intent.action.VIEW -d "http://163.com"')sleep(5)for i in range(20):subprocess.Popen(['adb', '-s', self.ip_input, 'shell', 'input', 'swipe', str(self.x1), str(self.y2),str(self.x1),str(self.y1), '500'],stdout=subprocess.PIPE)sleep(3)#def adb_connect(self, ip_input):# 假设此方法用于连接ADB#print(f"Connecting to ADB with IP: {ip_input}")# ... 省略实际连接ADB的代码def run(self):ScreenCapture.show_menu()  # 假设ScreenCapture是另一个模块或类,并且有show_menu方法num = int(input("输入你要选择的功能,用数字替代:"))# 定义动作字典action_mapping = {1: self.get_and_connect_ip,2: self.left_swipe,3: self.right_swipe,4: self.up_swipe,5: self.down_swipe,7: self.chrome_switch,}# 检查输入是否在映射中if num in action_mapping:action_mapping[num]()elif num == 6:# 对于特定的num值,可以直接处理passelse:print("\n\t输入有误!!!\t\n")self.run()  # 递归调用,重新显示菜单和接收输入if __name__ == '__main__':ScreenCapture().run()

http://www.ppmy.cn/embedded/112830.html

相关文章

支持升降压型、升压、降压、60V的1.2MHz频率LED恒流驱动器LGS63040、LGS63042

前言&#xff1a; 一款支持升降压的LED驱动器。适合单节锂电池使用。当然不仅于此。SOT23-5封装的外形和丝印 特性 宽输入电压、宽输出电压范围&#xff1a;3.0V-60V 支持 PWM 调光及模拟调光 内置 60V/350mΩ低侧金属氧化物半导体场效应晶体管 1.2MHz固定工作频率 逐周期峰值…

新能源汽车BMS 学习笔记篇—AFE 菊花链通信中电容隔离 电感隔离的使用

在汽车高压BMS系统中&#xff0c;通常采用 CAN 总线或菊花链&#xff08;&#xff08;Daisy Chain&#xff09;架构。菊花链架构通过串行连接每个节点&#xff0c;通常只需要两条信号线穿过所有节点。相比之下&#xff0c;CAN总线通常需要多个并行连接到总线上&#xff0c;布线…

OpenCV结构分析与形状描述符(21)计算包围给定点集的最小面积三角形函数minEnclosingTriangle()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 找到一个包围二维点集的最小面积三角形&#xff0c;并返回其面积。 该函数找到一个包围给定的二维点集的最小面积三角形&#xff0c;并返回其面…

区块链学习笔记3--以太坊

智能合约&#xff1a;跑在以太坊系统中的代码合同&#xff0c;其实质是一段代码。目前已经存在180多万个智能合约。 智能合约能表达&#xff1a;规则明确&#xff0c;不受主观因素影响的业务。 智能合约能表达&#xff1a;规则不轻易修改的业务 如果业务的规则经常变化&#x…

【计算机网络】UDP 协议详解及其网络编程应用

文章目录 一、引言二、UDP1、UDP的协议格式2、UDP 报文的解包和分用3、UDP面向数据报的特点 三、UDP输入输出四、UDP网络编程 一、引言 UDP&#xff08;User Datagram Protocol&#xff0c;用户数据报协议&#xff09;是一种网络通信协议&#xff0c;它属于传输层的协议。是一…

PHP环境搭建教程

在现代web开发中&#xff0c;PHP是一种广泛使用的服务器端脚本语言&#xff0c;适用于动态网页的创建。在本文中&#xff0c;我们将详细介绍如何在Windows和Linux环境中搭建PHP开发环境&#xff0c;帮助你快速开始PHP编程之旅。 一、准备工作 在开始搭建环境之前&#xff0c;…

Java读取损坏的xls表格

Java读取损坏的xls表格 1. 损坏的文件1.1 正常的xls文件用360解压后是这样↓1.2 被损坏的xls文件用360解压后是这样↓ 2. Java代码读取Excel文件分析2.1 使用EasyExcel读取损坏的xls文件报错2.2 使用POI读取损坏的xls文件报错 3. 损坏文件修复方案4. 代码 由于不可抗原因在网站…

运维工程师面试整理-操作系统

在运维工程师的面试中,操作系统相关的知识通常是重中之重,尤其是Linux/Unix系统。以下是针对操作系统部分的一些详细内容,帮助你更好地准备面试。 1. Linux/Unix 基础 ● 常用命令 ○ 文件和目录管理:ls, cd, cp, mv, rm, mkdir, rmdir, find, grep, awk, sed