python+adb+uiautomator2實現無線連接手機設備


一、adb安裝與環境配置

  具體安裝步驟可百度查找,安裝配置好之后可以命令查看是否安裝成功,如下圖情況表示安裝成功

 

 

 

二、安裝uiautomator2

  pip3 install --pre uiautomator2

 

三、uiautomator2安裝成功后使用數據線連接電腦在終端輸入如下命令進行初始化,手機(連接多個手機都可以)會統一安裝atx 和com.github.uiautomator.test

  python3 -m uiautomator2 init

 

四、wifi連接adb需要tcpip連接模式,所以在數據線連接時我們需要設定端口,使用如下命令(每台手機都需要這樣設置,可以寫個腳本批量設置也是OK的)

  adb tcpip 5566

五、連接測試

  手機需要與電腦連接同一個局域網,比如手機連接wifi后ip為 192.168.0.102,使用adb連接手機設備,如下圖:

 

六、連接腳本

import uiautomator2 as u
import os
from devices import getDevicesAll


# 無線連接手機
def wifi_connect_phone():
    devices_list = getDevicesAll()
    # 設置多個手機連接
    adress_list = ['192.168.xxx.xxx:xxxx']
    for ip in adress_list:
        if ip not in devices_list:
          # 給每個手機設定端口
          os.system('adb -s {} tcpip 5566'.format(ip))
          d = u.connect(ip) 
          print(d.info)
          print('連接成功!')
        else:
          print('手機已連接,無需重新連接!')
    

wifi_connect_phone()

 

執行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM