Documentation ¶
Overview ¶
Package adb is a Go package for interoperation with the Android Debug Bridge (adb).
The client/server spec is defined at https://android.googlesource.com/platform/system/core/+/master/adb/OVERVIEW.TXT.
WARNING This library is under heavy development, and its API is likely to change without notice. Use versioning!
See README for more information. Use `go doc` or godoc.org for documentation.
Example ¶
package main import ( "fmt" "time" "github.com/d1ced/adb" ) func main() { client, _ := adb.NewDefault() serverVersion, _ := client.Version() fmt.Println("Server version:", serverVersion) deviceInfo, _ := client.ListDevices() fmt.Println("Devices:") for _, device := range deviceInfo { fmt.Println(device) } fmt.Println("Watching for device state changes.") watcher, err := client.NewDeviceWatcher() if err != nil { panic(err) } go func() { <-time.After(20 * time.Second) watcher.Close() }() for event := range watcher.C() { fmt.Printf("\t[%s]%+v\n", time.Now(), event) } if err = watcher.Err(); err != nil { fmt.Println(err) } client.Kill() }
Output:
Index ¶
- Constants
- Variables
- func FormatDeviceInfo(dd []DeviceInfo) string
- type Cmd
- type Device
- func (d *Device) Command(cmd string, args ...string) *Cmd
- func (d *Device) CopyFile(path string, r io.Reader, perms os.FileMode, modtime time.Time) (int, error)
- func (d *Device) DeviceInfo() (DeviceInfo, error)
- func (d *Device) DevicePath() (string, error)
- func (d *Device) Forward(local, remote ForwardSpec) error
- func (d *Device) ForwardList() ([][2]ForwardSpec, error)
- func (d *Device) ForwardRemove(local ForwardSpec) error
- func (d *Device) ForwardRemoveAll() error
- func (d *Device) ForwardToFreePort(remote ForwardSpec) (int, error)
- func (d *Device) List(path string) ([]DirEntry, error)
- func (d *Device) OpenWrite(path string, perms os.FileMode, mtime time.Time) (io.WriteCloser, error)
- func (d *Device) ReadFile(path string) (io.ReadCloser, error)
- func (d *Device) Serial() (string, error)
- func (d *Device) Stat(path string) (DirEntry, error)
- func (d *Device) State() (DeviceState, error)
- func (d *Device) String() string
- type DeviceInfo
- type DeviceState
- type DeviceStateChangedEvent
- type DeviceWatcher
- type DirEntry
- type ForwardSpec
- type Server
- type ShellExitError
- type UnexpectedStatusError
Examples ¶
Constants ¶
const ( FProtocolTCP = "tcp" FProtocolLocal = "local" FProtocolJDWP = "jdwp" // TODO(jmh): check if they are still in use. FProtocolAbstract = "localabstract" FProtocolReserved = "localreserved" FProtocolFilesystem = "localfilesystem" )
ForwardSpec protocols
const ( // DefaultExecutableName is the name of the ADB-Server on the Path DefaultExecutableName = "adb" // DefaultPort is the default port for the ADB-Server to listens on. DefaultPort = 5037 )
Variables ¶
var ( // The connection to the server was reset in the middle of an operation. Server probably died. ErrConnectionReset = errors.New("connection reset") // Tried to perform an operation on a path that doesn't exist on the device. ErrFileNotExist = errors.New("file does not exist") ErrNotImplemented = errors.New("not implemented") )
Sentinel error values used by this package
Functions ¶
func FormatDeviceInfo ¶
func FormatDeviceInfo(dd []DeviceInfo) string
Types ¶
type Cmd ¶
Cmd represents a command that can be executed on a device. Use Command to get an instance.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device communicates with a specific Android device. To get an instance, call Server.Device(serial).
func (*Device) Command ¶
Command sets up a command to execute on device d. Command takes ownership of args.
func (*Device) CopyFile ¶
func (d *Device) CopyFile(path string, r io.Reader, perms os.FileMode, modtime time.Time) (int, error)
CopyFile copies the contents of r writing them to path on the device.
func (*Device) DeviceInfo ¶
func (d *Device) DeviceInfo() (DeviceInfo, error)
DeviceInfo queries the server for information on the current device.
func (*Device) DevicePath ¶
DevicePath returns the current devices path.
func (*Device) Forward ¶
func (d *Device) Forward(local, remote ForwardSpec) error
Forward remote connection to local
func (*Device) ForwardList ¶
func (d *Device) ForwardList() ([][2]ForwardSpec, error)
ForwardList returns list with struct ForwardPair If no device serial specified all devices's forward list will returned [2]ForwardSpec is {local, remote} serial is d.serial
func (*Device) ForwardRemove ¶
func (d *Device) ForwardRemove(local ForwardSpec) error
ForwardRemove specified forward
func (*Device) ForwardRemoveAll ¶
ForwardRemoveAll cancel all exists forwards
func (*Device) ForwardToFreePort ¶
func (d *Device) ForwardToFreePort(remote ForwardSpec) (int, error)
ForwardToFreePort return random generated port If forward already exists, just return current forworded port
func (*Device) OpenWrite ¶
OpenWrite opens the file at path on the device, creating it with the permissions specified by perms if necessary, and returns a writer that writes to the file. The files modification time will be set to mtime when the WriterCloser is closed. The zero value is TimeOfClose, which will use the time the Close method is called as the modification time. Deprecate this. Use CopyFile instead!
func (*Device) ReadFile ¶
func (d *Device) ReadFile(path string) (io.ReadCloser, error)
ReadFile returns a a reader for the given path on the device.
func (*Device) State ¶
func (d *Device) State() (DeviceState, error)
type DeviceInfo ¶
type DeviceInfo struct { // Must be always set. Serial string // Product, device, and model are not set in the short form. Product string Model string Device string // Only set for devices connected via USB. USB string }
func (DeviceInfo) IsUSB ¶
func (d DeviceInfo) IsUSB() bool
IsUSB returns true if the device is connected via USB. remove?
type DeviceState ¶
type DeviceState uint8
DeviceState represents one of the 3 possible states adb will report devices. A device can be communicated with when it's in StateOnline. A USB device will make the following state transitions:
Plugged in: StateDisconnected->StateOffline->StateOnline Unplugged: StateOnline->StateDisconnected
const ( StateInvalid DeviceState = iota StateDisconnected StateOffline StateOnline )
func (DeviceState) String ¶
func (i DeviceState) String() string
type DeviceStateChangedEvent ¶
type DeviceStateChangedEvent struct { Serial string OldState DeviceState NewState DeviceState }
DeviceStateChangedEvent represents a device state transition. Contains the device’s old and new states, but also provides methods to query the type of state transition.
func (DeviceStateChangedEvent) CameOnline ¶
func (e DeviceStateChangedEvent) CameOnline() bool
CameOnline returns true if this event represents a device coming online.
func (DeviceStateChangedEvent) WentOffline ¶
func (e DeviceStateChangedEvent) WentOffline() bool
WentOffline returns true if this event represents a device going offline.
type DeviceWatcher ¶
type DeviceWatcher struct {
// contains filtered or unexported fields
}
DeviceWatcher publishes device status change events. If the server dies while listening for events, it restarts the server.
func (*DeviceWatcher) C ¶
func (w *DeviceWatcher) C() <-chan DeviceStateChangedEvent
C returns a channel than can be received on to get events. If an unrecoverable error occurs, or Shutdown is called, the channel will be closed.
func (*DeviceWatcher) Close ¶
func (w *DeviceWatcher) Close() error
Close stops the watcher from listening for events and closes the channel returned from C. Don't double close a DeviceWatcher.
func (*DeviceWatcher) Err ¶
func (w *DeviceWatcher) Err() error
Err returns the error that caused the channel returned by C to be closed, if C is closed. If C is not closed, its return value is undefined.
type DirEntry ¶
DirEntry holds information about a directory entry on a device.
func ReadAllDirEntries ¶
ReadAllDirEntries reads directory entries into a slice, closes self, and returns any error. If err is non-nil, result will contain any entries read until the error occurred.
type ForwardSpec ¶
type ForwardSpec string
func (ForwardSpec) Port ¶
func (f ForwardSpec) Port() int
Port returns -1 if the endpoint has no port.
func (ForwardSpec) Protocol ¶
func (f ForwardSpec) Protocol() string
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server holds information needed to connect to a server repeatedly. Use New or NewDefault to create one.
func NewDefault ¶
NewDefault creates a new Adb client that uses the default ServerConfig.
func (*Server) ListDeviceSerials ¶
ListDeviceSerials returns the serial numbers of all attached devices.
func (*Server) ListDevices ¶
func (s *Server) ListDevices() ([]DeviceInfo, error)
ListDevices returns the list of connected devices.
func (*Server) NewDeviceWatcher ¶
func (s *Server) NewDeviceWatcher() (*DeviceWatcher, error)
NewDeviceWatcher starts a new device watcher.
type ShellExitError ¶
Rename CmdError?
func (ShellExitError) Error ¶
func (s ShellExitError) Error() string
type UnexpectedStatusError ¶
type UnexpectedStatusError struct {
// contains filtered or unexported fields
}
func (*UnexpectedStatusError) Error ¶
func (us *UnexpectedStatusError) Error() string