46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package goadb
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// Get the already connected adb devices
|
|
func GetConnectedDevices() ([]Device, error) {
|
|
var devices []Device
|
|
|
|
output, err := ExecuteCommand("devices")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to determine device status: %w", err)
|
|
}
|
|
|
|
lines := strings.Split(string(output), "\n")
|
|
for _, line := range lines {
|
|
if strings.Contains(line, "device") && !strings.Contains(line, "List of") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) >= 2 && fields[1] == "device" {
|
|
devices = append(devices, Device{Serial: fields[0]})
|
|
}
|
|
}
|
|
}
|
|
|
|
return devices, nil
|
|
}
|
|
|
|
// Execute a raw adb command and return the output
|
|
func ExecuteCommand(command string) ([]byte, error) {
|
|
|
|
//Check if adb is available
|
|
if _, err := exec.LookPath("adb"); err != nil {
|
|
return nil, fmt.Errorf("adb binary not found in PATH")
|
|
}
|
|
|
|
cmd := exec.Command("sh", "-c", "adb "+command)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("command failed: %v", err)
|
|
}
|
|
return output, nil
|
|
}
|