ble

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotConnected     = errors.New("not connected to device")
	ErrNoDevice         = errors.New("no device found")
	ErrTimeout          = errors.New("operation timeout")
	ErrNoService        = errors.New("service not found")
	ErrNoCharacteristic = errors.New("characteristic not found")
	ErrFrameTooLarge    = errors.New("frame exceeds device buffer")
)
View Source
var (
	// ServiceUUID is the primary BLE service UUID for battery communication
	ServiceUUID = bluetooth.NewUUID([16]byte{
		0x00, 0x00, 0x10, 0x06, 0x00, 0x00, 0x10, 0x00,
		0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb,
	}) // 00001006-0000-1000-8000-00805f9b34fb

	// WriteCharacteristicUUID is the characteristic for writing commands to the battery
	WriteCharacteristicUUID = bluetooth.NewUUID([16]byte{
		0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x10, 0x00,
		0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb,
	}) // 00001008-0000-1000-8000-00805f9b34fb

	// NotifyCharacteristicUUID is the characteristic for receiving notifications from the battery
	NotifyCharacteristicUUID = bluetooth.NewUUID([16]byte{
		0x00, 0x00, 0x10, 0x07, 0x00, 0x00, 0x10, 0x00,
		0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb,
	}) // 00001007-0000-1000-8000-00805f9b34fb

	// CCCDescriptorUUID is the Client Characteristic Configuration descriptor
	CCCDescriptorUUID = bluetooth.NewUUID([16]byte{
		0x00, 0x00, 0x29, 0x02, 0x00, 0x00, 0x10, 0x00,
		0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb,
	}) // 00002902-0000-1000-8000-00805f9b34fb
)

UUIDs for Voltgo BLE protocol (compatible with Enerwatt, TCED Worldwide, and other brands)

Functions

func ParseAddress

func ParseAddress(s string) (bluetooth.Address, error)

ParseAddress parses a BLE MAC address string such as "a4:c1:37:43:a4:42". bluetooth.ParseMAC only accepts uppercase hex digits, so normalize first.

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection represents a BLE connection to a battery

func NewConnection

func NewConnection() (*Connection, error)

NewConnection creates a new BLE connection handler

func (*Connection) Connect

func (c *Connection) Connect(ctx context.Context, address bluetooth.Address) error

Connect connects to a BLE device by address and prepares the battery's write and notify characteristics.

A link the battery accepts and then aborts is retried rather than returned on the first failure; see peerAbortReasons for why that failure is treated differently from every other connect error.

func (*Connection) Disconnect

func (c *Connection) Disconnect() error

Disconnect disconnects from the device

func (*Connection) IsConnected

func (c *Connection) IsConnected() bool

IsConnected returns whether the connection is active

func (*Connection) ReadFrame

func (c *Connection) ReadFrame(ctx context.Context, timeout time.Duration) ([]byte, error)

ReadFrame waits for the next notification frame, up to timeout.

func (*Connection) Request

func (c *Connection) Request(ctx context.Context, frame []byte, timeout time.Duration) ([]byte, error)

Request writes a frame and waits for the response notification. Any stale notifications queued before the write are discarded.

func (*Connection) Scan

func (c *Connection) Scan(ctx context.Context, duration time.Duration) ([]bluetooth.ScanResult, error)

Scan scans for nearby battery devices

func (*Connection) WriteFrame

func (c *Connection) WriteFrame(_ context.Context, frame []byte) error

WriteFrame writes a raw frame to the device's write characteristic.

Jump to

Keyboard shortcuts

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