bluetooth

package
v0.3.26 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 12 Imported by: 1 module, 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.

Documentation

Overview

Package bluetooth is the BlueZ service, the Go counterpart of crates/wayle-bluetooth: the adapter and device model kept live from the ObjectManager and PropertiesChanged signals, the adapter and device controls, and the org.bluez.Agent1 pairing agent whose requests the UI answers.

Index

Constants

View Source
const (
	// BusName is BlueZ's well-known name on the system bus.
	BusName = "org.bluez"
)

BlueZ names (types/mod.rs).

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	Path                 dbus.ObjectPath
	Address              string
	AddressType          AddressType
	Name                 string
	Alias                string
	Class                uint32
	Connectable          bool
	Powered              bool
	PowerState           PowerState
	Discoverable         bool
	DiscoverableTimeout  uint32
	Discovering          bool
	Pairable             bool
	PairableTimeout      uint32
	UUIDs                []string
	Modalias             *string
	Roles                []AdapterRole
	ExperimentalFeatures []string
	Manufacturer         uint16
	Version              uint8
}

Adapter is one org.bluez.Adapter1 (core/adapter). Values handed out by State are snapshots: treat the slices as read-only, the service replaces rather than mutates them.

type AdapterRef

type AdapterRef struct {

	// Path is the adapter's object path.
	Path dbus.ObjectPath
	// contains filtered or unexported fields
}

AdapterRef drives one adapter by path (core/adapter/controls.rs).

func (AdapterRef) ConnectDevice

func (a AdapterRef) ConnectDevice(ctx context.Context, properties map[string]dbus.Variant) (dbus.ObjectPath, error)

ConnectDevice connects to an address without discovery (BlueZ experimental) and returns the device's path.

func (AdapterRef) DiscoveryFilters

func (a AdapterRef) DiscoveryFilters(ctx context.Context) ([]string, error)

DiscoveryFilters lists the filter keys SetDiscoveryFilter accepts.

func (AdapterRef) RemoveDevice

func (a AdapterRef) RemoveDevice(ctx context.Context, device dbus.ObjectPath) error

RemoveDevice removes a device and its bonding.

func (AdapterRef) SetAlias

func (a AdapterRef) SetAlias(ctx context.Context, alias string) error

SetAlias sets the friendly name; empty reverts to the system name.

func (AdapterRef) SetConnectable

func (a AdapterRef) SetConnectable(ctx context.Context, connectable bool) error

SetConnectable sets Connectable (false also clears Discoverable).

func (AdapterRef) SetDiscoverable

func (a AdapterRef) SetDiscoverable(ctx context.Context, discoverable bool) error

SetDiscoverable makes the adapter visible or hidden.

func (AdapterRef) SetDiscoverableTimeout

func (a AdapterRef) SetDiscoverableTimeout(ctx context.Context, seconds uint32) error

SetDiscoverableTimeout sets the discoverable timeout in seconds (0: forever).

func (AdapterRef) SetDiscoveryFilter

func (a AdapterRef) SetDiscoveryFilter(ctx context.Context, options DiscoveryFilterOptions) error

SetDiscoveryFilter sets this client's discovery filter; the zero options clear it.

func (AdapterRef) SetPairable

func (a AdapterRef) SetPairable(ctx context.Context, pairable bool) error

SetPairable sets whether incoming pairing is accepted.

func (AdapterRef) SetPairableTimeout

func (a AdapterRef) SetPairableTimeout(ctx context.Context, seconds uint32) error

SetPairableTimeout sets the pairable timeout in seconds (0: forever).

func (AdapterRef) SetPowered

func (a AdapterRef) SetPowered(ctx context.Context, powered bool) error

SetPowered switches the adapter on or off.

func (AdapterRef) StartDiscovery

func (a AdapterRef) StartDiscovery(ctx context.Context) error

StartDiscovery opens this client's discovery session.

func (AdapterRef) StopDiscovery

func (a AdapterRef) StopDiscovery(ctx context.Context) error

StopDiscovery closes this client's discovery session.

type AdapterRole

type AdapterRole uint8

AdapterRole is one of the adapter's supported roles.

const (
	RoleCentral AdapterRole = iota
	RolePeripheral
	RoleCentralPeripheral
)

The adapter roles.

func ParseAdapterRole

func ParseAdapterRole(s string) AdapterRole

ParseAdapterRole maps BlueZ's role string; unknown reads as central.

func (AdapterRole) String

func (r AdapterRole) String() string

String is BlueZ's spelling.

type AddressType

type AddressType uint8

AddressType is the Bluetooth address type (types/adapter.rs). BlueZ reports it; unknown values read as public, as in the Rust From<&str>.

const (
	AddressPublic AddressType = iota
	AddressRandom
)

The address types.

func ParseAddressType

func ParseAddressType(s string) AddressType

ParseAddressType maps BlueZ's AddressType string, case-insensitively.

func (AddressType) String

func (a AddressType) String() string

String is BlueZ's spelling.

type AgentCapability

type AgentCapability uint8

AgentCapability is the IO capability the agent registers with (types/agent.rs).

const (
	CapabilityKeyboardDisplay AgentCapability = iota
	CapabilityDisplayYesNo
	CapabilityDisplayOnly
	CapabilityKeyboardOnly
	CapabilityNoInputNoOutput
)

The agent capabilities. KeyboardDisplay is the Rust default.

func ParseAgentCapability

func ParseAgentCapability(s string) AgentCapability

ParseAgentCapability maps the capability name; empty or unknown reads as KeyboardDisplay, as in the Rust From<&str>.

func (AgentCapability) String

func (c AgentCapability) String() string

String is BlueZ's spelling.

type Device

type Device struct {
	Path              dbus.ObjectPath
	Address           string
	AddressType       AddressType
	Name              *string
	Icon              *string
	BatteryPercentage *uint8
	Class             *uint32
	Appearance        *uint16
	UUIDs             []string
	Paired            bool
	Bonded            bool
	Connected         bool
	Trusted           bool
	Blocked           bool
	WakeAllowed       bool
	Alias             string
	Adapter           dbus.ObjectPath
	LegacyPairing     bool
	CablePairing      bool
	Modalias          *string
	RSSI              *int16
	TxPower           *int16
	ManufacturerData  map[uint16][]byte
	ServiceData       map[string][]byte
	ServicesResolved  bool
	AdvertisingFlags  []byte
	AdvertisingData   map[uint8][]byte
	Sets              []DeviceSet
	PreferredBearer   *PreferredBearer
}

Device is one org.bluez.Device1 plus its Battery1 percentage (core/device). Optional BlueZ properties are nil when absent, as the Rust Option fields; a nil map or slice is an absent property.

func (Device) DisplayName

func (d Device) DisplayName() string

DisplayName is the alias, falling back to the name (the UI's resolve_device_display); empty when BlueZ has neither.

type DeviceRef

type DeviceRef struct {

	// Path is the device's object path.
	Path dbus.ObjectPath
	// contains filtered or unexported fields
}

DeviceRef drives one device by path (core/device/controls.rs).

func (DeviceRef) CancelPairing

func (d DeviceRef) CancelPairing(ctx context.Context) error

CancelPairing aborts a Pair in progress.

func (DeviceRef) Connect

func (d DeviceRef) Connect(ctx context.Context) error

Connect connects every auto-connectable profile.

func (DeviceRef) ConnectProfile

func (d DeviceRef) ConnectProfile(ctx context.Context, uuid string) error

ConnectProfile connects one profile by UUID.

func (DeviceRef) Disconnect

func (d DeviceRef) Disconnect(ctx context.Context) error

Disconnect disconnects every profile.

func (DeviceRef) DisconnectProfile

func (d DeviceRef) DisconnectProfile(ctx context.Context, uuid string) error

DisconnectProfile disconnects one profile by UUID.

func (DeviceRef) Forget

func (d DeviceRef) Forget(ctx context.Context) error

Forget removes the device through its adapter (RemoveDevice), reading the Adapter property live.

func (DeviceRef) Pair

func (d DeviceRef) Pair(ctx context.Context) error

Pair pairs; BlueZ asks the agent along the way, so this blocks until the pairing prompt is answered.

func (DeviceRef) ServiceRecords

func (d DeviceRef) ServiceRecords(ctx context.Context) ([][]byte, error)

ServiceRecords returns the raw SDP records.

func (DeviceRef) SetAlias

func (d DeviceRef) SetAlias(ctx context.Context, alias string) error

SetAlias sets the device's alias.

func (DeviceRef) SetBlocked

func (d DeviceRef) SetBlocked(ctx context.Context, blocked bool) error

SetBlocked sets Blocked.

func (DeviceRef) SetPreferredBearer

func (d DeviceRef) SetPreferredBearer(ctx context.Context, bearer PreferredBearer) error

SetPreferredBearer sets PreferredBearer.

func (DeviceRef) SetTrusted

func (d DeviceRef) SetTrusted(ctx context.Context, trusted bool) error

SetTrusted sets Trusted (a trusted device's services need no authorization).

func (DeviceRef) SetWakeAllowed

func (d DeviceRef) SetWakeAllowed(ctx context.Context, allowed bool) error

SetWakeAllowed sets WakeAllowed.

type DeviceSet

type DeviceSet struct {
	Path dbus.ObjectPath
	// Rank is the device's rank in the set, nil when BlueZ omits it.
	Rank *uint8
}

DeviceSet is one Device1.Sets membership.

type DisconnectReason

type DisconnectReason uint8

DisconnectReason is Device1.Disconnected's reason.

const (
	DisconnectUnknown DisconnectReason = iota
	DisconnectTimeout
	DisconnectLocal
	DisconnectRemote
	DisconnectAuthentication
	DisconnectSuspend
)

The disconnect reasons.

func ParseDisconnectReason

func ParseDisconnectReason(s string) DisconnectReason

ParseDisconnectReason maps the org.bluez.Reason.* name.

func (DisconnectReason) String

func (r DisconnectReason) String() string

String is the Rust Display text.

type DisconnectedEvent

type DisconnectedEvent struct {
	Reason DisconnectReason
	// Message is BlueZ's human-readable text.
	Message string
}

DisconnectedEvent is one Device1.Disconnected signal.

type DiscoveryFilterOptions

type DiscoveryFilterOptions struct {
	UUIDs         []string
	RSSI          *int16
	Pathloss      *uint16
	Transport     *DiscoveryTransport
	DuplicateData *bool
	Discoverable  *bool
	Pattern       *string
	AutoConnect   *bool
}

DiscoveryFilterOptions is SetDiscoveryFilter's argument; nil fields are left out of the filter (DiscoveryFilterOptions in types/adapter.rs). The zero value clears the filter.

func (DiscoveryFilterOptions) Filter

func (o DiscoveryFilterOptions) Filter() map[string]dbus.Variant

Filter builds the a{sv} dictionary BlueZ takes (to_filter).

type DiscoveryTransport

type DiscoveryTransport uint8

DiscoveryTransport is the discovery filter's transport.

const (
	TransportAuto DiscoveryTransport = iota
	TransportBrEdr
	TransportLE
)

The discovery transports.

func ParseDiscoveryTransport

func ParseDiscoveryTransport(s string) DiscoveryTransport

ParseDiscoveryTransport maps the transport name case-insensitively; unknown reads as auto.

func (DiscoveryTransport) String

func (d DiscoveryTransport) String() string

String is BlueZ's spelling.

type DisplayPasskey

type DisplayPasskey struct {
	Device  dbus.ObjectPath
	Passkey uint32
	Entered uint16
}

DisplayPasskey shows a passkey to type on the remote device, with the count of digits typed so far; nothing to answer.

func (DisplayPasskey) DevicePath

func (r DisplayPasskey) DevicePath() dbus.ObjectPath

DevicePath implements PairingRequest.

type DisplayPinCode

type DisplayPinCode struct {
	Device  dbus.ObjectPath
	PinCode string
}

DisplayPinCode shows a PIN to type on the remote device; nothing to answer.

func (DisplayPinCode) DevicePath

func (r DisplayPinCode) DevicePath() dbus.ObjectPath

DevicePath implements PairingRequest.

type NoPendingRequestError

type NoPendingRequestError struct{ RequestType string }

NoPendingRequestError is an answer to a pairing request that is not the pending one (Error::NoPendingRequest): a PIN while BlueZ asks for a confirmation, or any answer with nothing pending.

func (*NoPendingRequestError) Error

func (e *NoPendingRequestError) Error() string

type NoPrimaryAdapterError

type NoPrimaryAdapterError struct{ Operation string }

NoPrimaryAdapterError is a service-level operation with no adapter to run on (Error::NoPrimaryAdapter).

func (*NoPrimaryAdapterError) Error

func (e *NoPrimaryAdapterError) Error() string

type OperationError

type OperationError struct {
	// Operation names the call, e.g. "start discovery".
	Operation string
	Err       error
}

OperationError is a failed BlueZ call (Error::AdapterOperation, and the device controls' D-Bus errors).

func (*OperationError) Error

func (e *OperationError) Error() string

func (*OperationError) Unwrap

func (e *OperationError) Unwrap() error

type PairingRequest

type PairingRequest interface {
	// DevicePath is the device the request is about.
	DevicePath() dbus.ObjectPath
	// contains filtered or unexported methods
}

PairingRequest is one agent prompt awaiting the user (PairingRequest in types/agent.rs). The concrete types are the seven variants; the interface is sealed.

type PowerState

type PowerState uint8

PowerState is the adapter's power transition state (BlueZ experimental PowerState).

const (
	PowerOff PowerState = iota
	PowerOn
	PowerOffToOn
	PowerOnToOff
	PowerOffBlocked
)

The power states.

func ParsePowerState

func ParsePowerState(s string) PowerState

ParsePowerState maps BlueZ's PowerState string; unknown reads as off.

func (PowerState) String

func (p PowerState) String() string

String is BlueZ's spelling.

type PreferredBearer

type PreferredBearer uint8

PreferredBearer is the device's preferred bearer (types/device.rs).

const (
	BearerLastUsed PreferredBearer = iota
	BearerBrEdr
	BearerLE
	BearerLastSeen
)

The preferred bearers.

func ParsePreferredBearer

func ParsePreferredBearer(s string) PreferredBearer

ParsePreferredBearer maps BlueZ's bearer string; unknown reads as last-used.

func (PreferredBearer) String

func (b PreferredBearer) String() string

String is BlueZ's spelling.

type RequestAuthorization

type RequestAuthorization struct{ Device dbus.ObjectPath }

RequestAuthorization asks to allow a just-works pairing (answer with ProvideAuthorization).

func (RequestAuthorization) DevicePath

func (r RequestAuthorization) DevicePath() dbus.ObjectPath

DevicePath implements PairingRequest.

type RequestConfirmation

type RequestConfirmation struct {
	Device  dbus.ObjectPath
	Passkey uint32
}

RequestConfirmation asks whether Passkey matches the remote device's (answer with ProvideConfirmation).

func (RequestConfirmation) DevicePath

func (r RequestConfirmation) DevicePath() dbus.ObjectPath

DevicePath implements PairingRequest.

type RequestPasskey

type RequestPasskey struct{ Device dbus.ObjectPath }

RequestPasskey asks for the numeric passkey shown on the remote device (answer with ProvidePasskey).

func (RequestPasskey) DevicePath

func (r RequestPasskey) DevicePath() dbus.ObjectPath

DevicePath implements PairingRequest.

type RequestPinCode

type RequestPinCode struct{ Device dbus.ObjectPath }

RequestPinCode asks for a legacy PIN (answer with ProvidePin).

func (RequestPinCode) DevicePath

func (r RequestPinCode) DevicePath() dbus.ObjectPath

DevicePath implements PairingRequest.

type RequestServiceAuthorization

type RequestServiceAuthorization struct {
	Device dbus.ObjectPath
	UUID   string
}

RequestServiceAuthorization asks to allow a profile connection (answer with ProvideServiceAuthorization).

func (RequestServiceAuthorization) DevicePath

DevicePath implements PairingRequest.

type Service

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

Service is the live BlueZ model plus the pairing agent.

func New

func New(conn *dbus.Conn) (*Service, error)

New starts the service on conn, which stays the caller's: it exports and registers the agent, reads the object tree, and follows BlueZ's signals (BluetoothService::new). Close takes back what it put there.

func (*Service) Adapter

func (s *Service) Adapter(path dbus.ObjectPath) AdapterRef

Adapter returns the controls for the adapter at path; the path is not checked until a call.

func (*Service) CancelPendingRequest

func (s *Service) CancelPendingRequest()

CancelPendingRequest dismisses whatever is on screen: the yes/no requests are rejected, the PIN/passkey entries cancelled, a display request just cleared (cancel_pending_request).

func (*Service) Close

func (s *Service) Close() error

Close stops following BlueZ, cancels a pending agent request, and unregisters the agent; the connection stays open. Calling it again does nothing.

func (*Service) Connect

func (s *Service) Connect(ctx context.Context, device dbus.ObjectPath) error

Connect connects the device.

func (*Service) Device

func (s *Service) Device(path dbus.ObjectPath) DeviceRef

Device returns the controls for the device at path; the path is not checked until a call.

func (*Service) Disable

func (s *Service) Disable(ctx context.Context) error

Disable powers the primary adapter off.

func (*Service) Disconnect

func (s *Service) Disconnect(ctx context.Context, device dbus.ObjectPath) error

Disconnect disconnects the device.

func (*Service) Disconnected

func (s *Service) Disconnected(device dbus.ObjectPath) (<-chan DisconnectedEvent, func())

Disconnected streams the device's Disconnected signals until stop (Device::disconnected_signal).

func (*Service) Enable

func (s *Service) Enable(ctx context.Context) error

Enable powers the primary adapter on.

func (*Service) Forget

func (s *Service) Forget(ctx context.Context, device dbus.ObjectPath) error

Forget removes the device from its adapter.

func (*Service) ProvideAuthorization

func (s *Service) ProvideAuthorization(allowed bool) error

ProvideAuthorization answers RequestAuthorization: false rejects.

func (*Service) ProvideConfirmation

func (s *Service) ProvideConfirmation(confirmed bool) error

ProvideConfirmation answers RequestConfirmation: false rejects.

func (*Service) ProvidePasskey

func (s *Service) ProvidePasskey(passkey uint32) error

ProvidePasskey answers RequestPasskey; a passkey above 999999 is an error and the request stays pending.

func (*Service) ProvidePin

func (s *Service) ProvidePin(pin string) error

ProvidePin answers RequestPinCode. BlueZ takes 1-16 characters; a PIN outside that is an error and the request stays pending.

func (*Service) ProvideServiceAuthorization

func (s *Service) ProvideServiceAuthorization(allowed bool) error

ProvideServiceAuthorization answers RequestServiceAuthorization: false rejects.

func (*Service) SetTrusted

func (s *Service) SetTrusted(ctx context.Context, device dbus.ObjectPath, trusted bool) error

SetTrusted sets the device's Trusted.

func (*Service) StartDiscovery

func (s *Service) StartDiscovery(ctx context.Context) error

StartDiscovery scans on the primary adapter until StopDiscovery.

func (*Service) StartTimedDiscovery

func (s *Service) StartTimedDiscovery(ctx context.Context, d time.Duration) error

StartTimedDiscovery scans on the primary adapter and stops that adapter's session after d.

func (*Service) State

func (s *Service) State() State

State copies the model out.

func (*Service) StopDiscovery

func (s *Service) StopDiscovery(ctx context.Context) error

StopDiscovery ends the primary adapter's discovery session.

func (*Service) Subscribe

func (s *Service) Subscribe() (<-chan struct{}, func())

Subscribe ticks on every model or pairing change; the channel closes on stop or Close.

func (*Service) TrustPaired

func (s *Service) TrustPaired(ctx context.Context, device dbus.ObjectPath) error

TrustPaired marks a paired, untrusted device trusted, so BlueZ stops asking the agent to authorize its services on every reconnect; an unpaired device is left alone (a stray trust would linger). It reads Paired and Trusted live, as the UI's trust_paired_device does, since the model may not have folded the pairing's signals in yet.

type Snapshot

type Snapshot struct {
	// Available reports whether a primary adapter exists.
	Available bool
	// Enabled is the primary adapter's Powered.
	Enabled bool
	// Discovering is the primary adapter's Discovering.
	Discovering bool
	// Connected holds the aliases of connected devices.
	Connected []string
}

Snapshot is the bar module's summary of State.

type Source

type Source interface {
	// State is the current model.
	State() State
	// Subscribe ticks (coalesced) on every change until stop.
	Subscribe() (ticks <-chan struct{}, stop func())

	Enable(ctx context.Context) error
	Disable(ctx context.Context) error
	StartTimedDiscovery(ctx context.Context, d time.Duration) error

	Connect(ctx context.Context, device dbus.ObjectPath) error
	Disconnect(ctx context.Context, device dbus.ObjectPath) error
	Forget(ctx context.Context, device dbus.ObjectPath) error
	SetTrusted(ctx context.Context, device dbus.ObjectPath, trusted bool) error
	TrustPaired(ctx context.Context, device dbus.ObjectPath) error

	ProvidePin(pin string) error
	ProvidePasskey(passkey uint32) error
	ProvideConfirmation(confirmed bool) error
	ProvideAuthorization(allowed bool) error
	ProvideServiceAuthorization(allowed bool) error
	CancelPendingRequest()
}

Source is the UI's seam onto the service: the live state, change ticks, and the operations the bar and its dropdown drive.

type State

type State struct {
	// Adapters in discovery order.
	Adapters []Adapter
	// Primary is the adapter the service-level operations use; nil
	// when there is none.
	Primary *Adapter
	// Devices in discovery order, across adapters.
	Devices []Device
	// Available reports a primary adapter.
	Available bool
	// Enabled is the primary adapter's Powered.
	Enabled bool
	// Connected holds the connected devices' addresses.
	Connected []string
	// Pairing is the agent request on screen, nil when none.
	Pairing PairingRequest
}

State is one read of the model (the BluetoothService fields).

func (State) Device

func (s State) Device(path dbus.ObjectPath) (Device, bool)

Device looks a device up by path.

func (State) Snapshot

func (s State) Snapshot() Snapshot

Snapshot summarizes the state for the bar label and icon.

Jump to

Keyboard shortcuts

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