BlueStacks 模拟器便捷连接

android studio 的模拟器太卡了,于是就用了 BlueStacks 模拟器,但是有个地方太不方便就是不能自动 adb 连接到 android studio,每次都要手动连接

  • 打开模拟器,打开设置查看当前的端口
  • 打开 cmd,输入adb connect +端口号才行

于是就写了个脚本来自动连接。

脚本

import os
import subprocess

def read_conf_file(file_path):
    with open(file_path, 'r') as f:
        lines = f.readlines()
    for line in lines:
        if line.startswith("bst.instance.Pie64.status.adb_port"):
            return line.split("=")[1].strip()

def run_adb_command(port):
    command = f"adb connect 127.0.0.1:{port}"
    subprocess.run(command, shell=True)

if __name__ == "__main__":
    file_path = r"C:\ProgramData\BlueStacks_nxt\bluestacks.conf"
    if os.path.exists(file_path):
        port = read_conf_file(file_path)
        if port:
            run_adb_command(port)
        else:
            print("Could not find adb_port in the configuration file.")
    else:
        print(f"The specified file {file_path} does not exist.")

使用方法

  1. 启动模拟器。Start-Process -FilePath "C:\Program Files\BlueStacks_nxt\HD-Player.exe" -ArgumentList " --instance Pie64",启动完毕后执行下一步
  2. 运行脚本。python .\adb_connect.py