Skip to content

pure go adb library for google adb service.

License

Notifications You must be signed in to change notification settings

xmsociety/adbutils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

adbutils

GoDoc Go Report Card Sourcegraph Release Goproxy.cn

Transfer from python adbutils

Table of Contents

Install

  • No development plan yet

Usage

Example

Connect ADB Server

package test

import (
	"fmt"
	"testing"

	"github.com/xmsociety/adbutils"
)

var adb = adbutils.AdbClient{Host: "localhost", Port: 5037, SocketTime: 10}

func TestConnect(t *testing.T) {
	for _, i := range adb.DeviceList() {
		adb.Connect(i.Serial)
		snNtid := adbutils.SerialNTransportID{
			Serial: i.Serial,
		}
		fmt.Println(adb.Device(snNtid).SayHello())
	}

}

List all the devices and get device object

package main

import (
	"fmt"

	"github.com/xmsociety/adbutils"
)
adb := adbutils.NewAdb("localhost", 5037, 10)

func ShowSerials() {
	for _, device := range adb.DeviceList() {
		fmt.Println("", device.Serial)
	}
}

type SerialNTransportID struct {
    // you get this struct by adbutils.SerialNTransportID
	Serial      string
	TransportID int
}

just_serial := SerialNTransportID{Serial: "33ff22xx"}
adb.Device(just_serial)

// or
just_transport_id := SerialNTransportID{TransportID: 24}
adb.Device(just_transport_id) // transport_id can be found in: adb devices -l

// # You do not need to offer serial if only one device connected
// # RuntimeError will be raised if multi device connected
// d = adb.Device()

The following code will not write from adbutils import adb for short

Connect or disconnect remote device

Same as command adb connect

output := adb.Connect("127.0.0.1:5555")
// output: already connected to 127.0.0.1:5555

# connect with timeout
// timeout 10
adb := adbutils.NewAdb("localhost", 5037, 10)


// Disconnect
adb.Disconnect("127.0.0.1:5555")
adb.Disconnect("127.0.0.1:5555", raise_error=True) # if device is not present, AdbError will raise

// wait-for-device
// TODO

Create socket connection to the device

For example

func (adbConnection AdbConnection) ReadString(n int) string {
	res := adbConnection.Read(n)
	return string(res)
}

func (adbConnection AdbConnection) ReadStringBlock() string {
	str := adbConnection.ReadString(4)
	if len(str) == 0 {
		log.Fatal("receive data error connection closed")
	}
	size, _ := strconv.ParseUint(str, 16, 32)
	return adbConnection.ReadString(int(size))
}

func (adbConnection AdbConnection) ReadUntilClose() string {
	buf := []byte{}
	for {
		chunk := adbConnection.Read(4096)
		if len(chunk) == 0 {
			break
		}

		buf = append(buf, chunk...)
	}
	return string(buf)
}
func (adbConnection AdbConnection) createSocket() (*net.Conn, error) {
	conn, err := net.Dial("tcp", fmt.Sprintf("%v:%d", adbConnection.Host, adbConnection.Port))
	if err != nil {
		return nil, err
	}
	return &conn, nil
}

There are many other usage, see SERVICES.TXT for more details

Run shell command

I assume there is only one device connected.

package main

import (
	"fmt"

	"github.com/xmsociety/adbutils"
)

// 获取序列号
func GetServerVersion() {
	adb := adbutils.AdbClient{Host: "localhost", Port: 5037, SocketTime: 10}
	version := adb.ServerVersion()
	fmt.Printf("version: %d\n\n", version)
}

func Shell(arg string) {
	adb := adbutils.NewAdb("localhost", 5037, 10)
	for _, device := range adb.DeviceList() {
		fmt.Printf("Now show device: %s, ls: \n", device.Serial)
		fmt.Println(device.Shell(arg, false))
	}
}

func main() {
	GetServerVersion()
	Shell("ls")
}

Other You Can Send:

  • Argument just support str Shell(["getprop", "ro.serial"]) - can't work

  • Can't Set timeout for shell command Shell("sleep 1", timeout=0.5) - Recommend you set timeout by adb's socketTime

  • The advanced shell (returncode archieved by add command suffix: ;echo EXIT:$?)

ret := device.Shell("echo 1")
fmt.Println(ret)
  • show property, also based on d.shell TODO

Environment variables

ANDROID_SERIAL  serial number to connect to
ANDROID_ADB_SERVER_HOST adb server host to connect to
ANDROID_ADB_SERVER_PORT adb server port to connect to

Color Logcat

  • No development plan yet

Experiment

TODO

For more usage, please see the code for details.

Examples

Record video using screenrecord

It is highly recommended that you follow this

// TODO

Reading Logcat

// TODO

Develop

Make sure you can connect Github, Now you can edit code in adbutils and test with

package test
import (
	"github.com/xmsociety/adbutils"
	"testing"
)
// .... test code here ...

Run tests requires one device connected to your computer

# change to repo directory
cd adbutils

go test test/*

Environment

Some environment can affect the adbutils behavior

  • ADBUTILS_ADB_PATH: specify adb path, default search from PATH
  • ANDROID_SERIAL: default adb serial
  • ANDROID_ADB_SERVER_HOST: default 127.0.0.1
  • ANDROID_ADB_SERVER_PORT: default 5037

Watch adb socket data

Watch the adb socket data using socat

$ socat -t100 -x -v TCP-LISTEN:5577,reuseaddr,fork TCP4:localhost:5037

open another terminal, type the following command then you will see the socket data

$ export ANDROID_ADB_SERVER_PORT=5577
$ adb devices

Generate TOC

gh-md-toc --insert README.md

https://github.com/ekalinin/github-markdown-toc

Thanks

Alternative

just like pure-python-adb

Alternative

Ref

LICENSE

MIT