accessory

package module
v0.9.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Introduction

The go-accessoryhid package is an implementation of the AOA 2.0 protocol for Android HID devices.

Dependence on gousb and libusb-1.0.

Documentation

TODO.

Installation

go get -u github.com/Tryanks/go-accessoryhid

Usage

Example:

package main

import (
	accessory "github.com/Tryanks/go-accessoryhid"
	"github.com/google/gousb"
	"time"
)

func main() {
	ctx := gousb.NewContext()
	defer ctx.Close()
	devices, err := accessory.GetDevices(ctx, 2)
	if err != nil {
		panic(err)
	}
	phone := devices[0]
	defer phone.Close()
	keyboard, err := phone.Register(accessory.KeyboardReportDesc) // Register keyboard report descriptor
	time.Sleep(200 * time.Millisecond)
	err = keyboard.SendEvent([]byte{
		0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
	}) // Hid event (pressing 'a')
	if err != nil {
		panic(err)
	}
	err = keyboard.SendEvent([]byte{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	}) // Hid event (releasing 'a')
	if err != nil {
		panic(err)
	}
}

FAQ

Q: Getting libusb: pipe error [code -9] when sending HID events after calling Register().

A: This error typically occurs due to sending events too quickly after HID descriptor registration.

The Android device may not have completed its initialization process.

The solution is to add a short delay after calling Register(),

Documentation

Index

Constants

View Source
const (
	AccessoryRegisterHid      uint8 = 54
	AccessoryUnregisterHid          = 55
	AccessorySetHidReportDesc       = 56
	AccessorySendHidEvent           = 57

	AccessoryGetProtocol = 51
)

AOAv2 Basic Protocol

USB Control transfer type

Variables

View Source
var (
	ErrorNoAccessoryDevice   = errors.New("ErrorNoAccessoryDevice")
	ErrorFailedToGetProtocol = errors.New("ErrorFailedToGetProtocol")
)

Error definitions

View Source
var ConsumerReportDesc = []byte{
	0x05, 0x0c,
	0x09, 0x01,
	0xa1, 0x01,
	0x15, 0x00,
	0x25, 0x01,
	0x75, 0x01,
	0x95, 0x04,
	0x0a, 0x23, 0x02,
	0x0a, 0x24, 0x02,
	0x09, 0x40,
	0x09, 0x34,
	0x81, 0x06,
	0x75, 0x04,
	0x95, 0x01,
	0x81, 0x03,
	0xc0,
}

ConsumerReportDesc is the HID report descriptor for a consumer control.

View Source
var KeyboardReportDesc = []byte{
	0x05, 0x01,
	0x09, 0x06,
	0xA1, 0x01,
	0x05, 0x07,
	0x19, 0xE0,
	0x29, 0xE7,
	0x15, 0x00,
	0x25, 0x01,
	0x75, 0x01,
	0x95, 0x08,
	0x81, 0x02,
	0x75, 0x08,
	0x95, 0x01,
	0x81, 0x01,
	0x05, 0x08,
	0x19, 0x01,
	0x29, 0x05,
	0x75, 0x01,
	0x95, 0x05,
	0x91, 0x02,
	0x75, 0x03,
	0x95, 0x01,
	0x91, 0x01,
	0x05, 0x07,
	0x19, 0x00,
	0x29, scHidKeyboardKeys - 1,
	0x15, 0x00,
	0x25, scHidKeyboardKeys - 1,
	0x75, 0x08,
	0x95, reportKeyboardMaxKeys,
	0x81, 0x00,
	0xC0,
}

KeyboardReportDesc is the HID report descriptor for a keyboard.

View Source
var MouseReportDesc = []byte{
	0x05, 0x01,
	0x09, 0x02,
	0xA1, 0x01,
	0x09, 0x01,
	0xA1, 0x00,
	0x05, 0x09,
	0x19, 0x01,
	0x29, 0x05,
	0x15, 0x00,
	0x25, 0x01,
	0x95, 0x05,
	0x75, 0x01,
	0x81, 0x02,
	0x95, 0x01,
	0x75, 0x03,
	0x81, 0x01,
	0x05, 0x01,
	0x09, 0x30,
	0x09, 0x31,
	0x09, 0x38,
	0x15, 0x81,
	0x25, 0x7F,
	0x75, 0x08,
	0x95, 0x03,
	0x81, 0x06,
	0xC0,
	0xC0,
}

MouseReportDesc is the HID report descriptor for a mouse.

View Source
var SkipList = []uint16{
	0x8087,
	0x1d6b,
	0x2109,
}

SkipList is a list of vendor IDs that are known to not support the accessory protocol and should be skipped TODO: Add more vendor IDs to the list

View Source
var TouchscreenReportDesc = []byte{
	0x05, 0x0d,
	0x09, 0x04,
	0xa1, 0x01,
	0x09, 0x22,
	0xa1, 0x00,
	0x09, 0x42,
	0x09, 0x51,
	0x15, 0x00,
	0x25, 0x01,
	0x75, 0x01,
	0x95, 0x02,
	0x81, 0x02,
	0x95, 0x0e,
	0x81, 0x03,
	0x05, 0x01,
	0x75, 0x10,
	0x95, 0x01,
	0x55, 0x0d,
	0x65, 0x33,
	0x15, 0x00,
	0x26, 0xff, 0x7f,
	0x09, 0x30,
	0x81, 0x02,
	0x09, 0x31,
	0x81, 0x02,
	0xc0,
	0xc0,
}

TouchscreenReportDesc is the HID report descriptor for a touchscreen.

Functions

This section is empty.

Types

type Accessory

type Accessory struct {
	ParentDevice *AccessoryDevice
	// contains filtered or unexported fields
}

func (*Accessory) SendEvent

func (a *Accessory) SendEvent(event []byte) error

func (*Accessory) Unregister

func (a *Accessory) Unregister() error

type AccessoryDevice

type AccessoryDevice struct {
	Device       *gousb.Device
	Protocol     uint16
	Manufacturer string
	// contains filtered or unexported fields
}

AccessoryDevice Connected Android Device

func GetDevice added in v0.9.2

func GetDevice(ctx *gousb.Context) (device *AccessoryDevice, err error)

GetDevice return a single device match protocol version 2

func GetDeviceWithSerial added in v0.9.2

func GetDeviceWithSerial(ctx *gousb.Context, serial string) (device *AccessoryDevice, err error)

GetDeviceWithSerial return a device match serial with device.SerialNumber()

func GetDevices

func GetDevices(ctx *gousb.Context, protocolVersion uint16) (accessoryList []*AccessoryDevice, err error)

GetDevices return a list of devices that support the specified protocol version

func NewAccessoryDevice

func NewAccessoryDevice(device *gousb.Device, protocol uint16, manu string) *AccessoryDevice

NewAccessoryDevice Create a new AccessoryDevice

func (*AccessoryDevice) Close

func (a *AccessoryDevice) Close() error

func (*AccessoryDevice) Register

func (a *AccessoryDevice) Register(reportDesc []byte) (accessory *Accessory, err error)

func (*AccessoryDevice) SendHidEvent

func (a *AccessoryDevice) SendHidEvent(hidID uint16, event []byte) error

func (*AccessoryDevice) SerialNumber added in v0.9.2

func (a *AccessoryDevice) SerialNumber() (string, error)

func (*AccessoryDevice) Unregister

func (a *AccessoryDevice) Unregister(hidID uint16) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址