Compare commits

..

2 Commits

Author SHA1 Message Date
f8acd46914 adb: pair/connect support 2025-10-13 19:21:06 +02:00
d1da3deb8d adb: Allow raw command execution 2025-10-13 19:04:32 +02:00
3 changed files with 66 additions and 0 deletions

58
adb.go Normal file
View File

@@ -0,0 +1,58 @@
package goadb
import (
"fmt"
"os/exec"
"strings"
)
// Pair a device using the 6 digit code
func PairDevice(endpoint string, code string) error {
_, err := ExecuteCommand(fmt.Sprintf("pair %s %s", endpoint, code))
return err
}
// Connect to a device
func ConnectDevice(endpoint string) error {
//todo: check if the device is paired before connecting
_, err := ExecuteCommand("connect " + endpoint)
return err
}
// 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
}

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.plabble.org/misthios/goadb
go 1.24.6

5
types.go Normal file
View File

@@ -0,0 +1,5 @@
package goadb
type Device struct {
Serial string
}