完成基础功能
This commit is contained in:
@@ -21,12 +21,6 @@ type DeviceInfo struct {
|
||||
Props map[string]string // 额外属性
|
||||
}
|
||||
|
||||
// AdbDevice 表示一个已建立连接的 ADB 设备
|
||||
type AdbDevice struct {
|
||||
Connect net.Conn // 到设备的 TCP 连接
|
||||
Serial string // 设备序列号
|
||||
}
|
||||
|
||||
// ListDevicesRaw 向 ADB 服务器请求设备列表并返回原始文本
|
||||
// addr 为 ADB 服务器地址,timeout 为超时时间
|
||||
func ListDevicesRaw(addr string, timeout time.Duration) (string, error) {
|
||||
@@ -146,33 +140,6 @@ func parseGetprop(raw []byte) map[string]string {
|
||||
return m
|
||||
}
|
||||
|
||||
// ParseDevicesMap 将设备列表文本解析为 serial → product 的映射
|
||||
// 同时返回原始行列表
|
||||
func ParseDevicesMap(payload string) (map[string]string, []string) {
|
||||
m := make(map[string]string)
|
||||
lines := strings.Split(payload, "\n")
|
||||
for _, ln := range lines {
|
||||
ln = strings.TrimSpace(ln)
|
||||
if ln == "" || strings.HasPrefix(ln, "List of devices attached") {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(ln)
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
serial := fields[0]
|
||||
product := ""
|
||||
for _, kv := range fields[2:] {
|
||||
if strings.HasPrefix(kv, "product:") {
|
||||
product = strings.TrimPrefix(kv, "product:")
|
||||
break
|
||||
}
|
||||
}
|
||||
m[serial] = product
|
||||
}
|
||||
return m, lines
|
||||
}
|
||||
|
||||
// FindSerialByProduct 根据产品名称查找设备序列号
|
||||
// 优先从设备列表中匹配,如果列表中没有 product 字段,则回退到逐设备查询 getprop
|
||||
func FindSerialByProduct(addr, targetProduct string) (string, error) {
|
||||
@@ -180,10 +147,10 @@ func FindSerialByProduct(addr, targetProduct string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m, _ := ParseDevicesMap(payload)
|
||||
for serial, product := range m {
|
||||
if product == targetProduct {
|
||||
return serial, nil
|
||||
devices := ParseDevicesPayload(payload)
|
||||
for _, dev := range devices {
|
||||
if dev.Product == targetProduct {
|
||||
return dev.Serial, nil
|
||||
}
|
||||
}
|
||||
// 回退:逐设备查询 getprop ro.product.model
|
||||
@@ -198,15 +165,12 @@ func FindSerialByProduct(addr, targetProduct string) (string, error) {
|
||||
continue
|
||||
}
|
||||
serial := fields[0]
|
||||
conn, err := DialADB(addr, 2*time.Second)
|
||||
conn, err := ConnectToDevice(addr, serial, 2*time.Second)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := TransportTo(conn, serial); err != nil {
|
||||
continue
|
||||
}
|
||||
out, err := ExecShell(conn, "getprop ro.product.model")
|
||||
conn.Close()
|
||||
if err == nil {
|
||||
if strings.TrimSpace(string(out)) == targetProduct {
|
||||
return serial, nil
|
||||
@@ -222,13 +186,11 @@ func FindSerialByProduct(addr, targetProduct string) (string, error) {
|
||||
// pmArgs 为 pm install 的额外参数(如 "-r" 表示覆盖安装),默认为 "-r"
|
||||
// debug 为 true 时输出调试信息
|
||||
func InstallApkOnDevice(addr, serial string, remoteTmp string, pmArgs string, debug bool) (string, error) {
|
||||
conn, err := DialADB(addr, 15*time.Second)
|
||||
conn, err := ConnectToDevice(addr, serial, 15*time.Second)
|
||||
if err != nil {
|
||||
fmt.Println("连接失败:", err)
|
||||
return "", err
|
||||
}
|
||||
defer conn.Close()
|
||||
TransportTo(conn, serial)
|
||||
|
||||
if pmArgs == "" {
|
||||
pmArgs = "-r"
|
||||
|
||||
Reference in New Issue
Block a user