device: add support for running commands on a specific device

This commit is contained in:
2025-10-13 19:39:20 +02:00
parent 050ee6f0f7
commit cce42f7c2d

30
device.go Normal file
View File

@@ -0,0 +1,30 @@
package goadb
import (
"fmt"
"log"
"strings"
)
// IsConnected checks if the device is currently connected via ADB
func (d *Device) IsConnected() bool {
output, err := ExecuteCommand("devices")
if err != nil {
log.Println(fmt.Errorf("Failed to determine device status: %w", err))
return false
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.HasPrefix(line, d.Serial) && strings.Contains(line, "device") {
return true
}
}
return false
}
// Run a command on this specific device
func (d *Device) RunCommand(command string) ([]byte, error) {
return ExecuteCommand(fmt.Sprintf("-s %s %s", d.Serial, command))
}