Documentation ¶
Overview ¶
Package ble provides functions to discover, connect, pair, and communicate with Bluetooth Low Energy peripheral devices.
This implementation uses the BlueZ D-Bus interface, rather than sockets. It is similar to github.com/adafruit/Adafruit_Python_BluefruitLE
Index ¶
- Constants
- func LongUUID(u string) string
- func ShortUUID(u string) string
- func UUIDsInclude(advertised []string, uuids []string) bool
- func ValidAddress(addr string) bool
- func ValidUUID(u string) bool
- type Adapter
- type Address
- type BaseObject
- type Characteristic
- type Connection
- func (conn *Connection) Close()
- func (conn *Connection) Discover(timeout time.Duration, address Address, uuids ...string) (Device, error)
- func (conn *Connection) GetAdapter() (Adapter, error)
- func (conn *Connection) GetCharacteristic(uuid string) (Characteristic, error)
- func (conn *Connection) GetDescriptor(uuid string) (Descriptor, error)
- func (conn *Connection) GetDeviceByAddress(address Address) (Device, error)
- func (conn *Connection) GetDeviceByName(name string) (Device, error)
- func (conn *Connection) GetDeviceByUUID(uuids ...string) (Device, error)
- func (conn *Connection) GetService(uuid string) (Service, error)
- func (conn *Connection) HandleNotify(uuid string, handler NotifyHandler) error
- func (conn *Connection) Print(w io.Writer)
- func (conn *Connection) Update() error
- type Descriptor
- type Device
- type DiscoveryTimeoutError
- type GattHandle
- type NotifyHandler
- type Object
- type Properties
- type ReadWriteHandle
- type Service
- type UUIDs
Constants ¶
const ( // BluetoothBaseUUID for service discovery. // See www.bluetooth.com/specifications/assigned-numbers/service-discovery BluetoothBaseUUID = "0000xxxx-0000-1000-8000-00805f9b34fb" )
const (
// GATTMTU is the maximum size of a write to a GATT characteristic.
GATTMTU = 20
)
Variables ¶
This section is empty.
Functions ¶
func LongUUID ¶
LongUUID returns the 128-bit UUID corresponding to a possibly-shorter UUID, which must be valid.
func ShortUUID ¶
ShortUUID returns the shortest UUID corresponding to the given UUID, which must be valid.
func UUIDsInclude ¶
UUIDsInclude tests whether the advertised UUIDs contain all of the ones in uuids.
func ValidAddress ¶
ValidAddress checks whether addr is a valid MAC address.
Types ¶
type Adapter ¶
type Adapter interface { BaseObject StartDiscovery() error StopDiscovery() error RemoveDevice(Device) error SetDiscoveryFilter(uuids ...string) error Discover(timeout time.Duration, uuids ...string) error }
The Adapter type corresponds to the org.bluez.Adapter1 interface. See bluez/doc/adapter-api.txt
StartDiscovery starts discovery on the adapter.
StopDiscovery stops discovery on the adapter.
RemoveDevice removes the specified device and its pairing information.
SetDiscoveryFilter sets the discovery filter to require LE transport and the given UUIDs.
Discover performs discovery for a device with the given UUIDs, for at most the specified timeout, or indefinitely if timeout is 0. See also the Discover method of the ObjectCache type.
type BaseObject ¶
type BaseObject interface { Conn() *Connection Path() dbus.ObjectPath Interface() string Name() string Print(io.Writer) }
BaseObject is the interface satisfied by bluez D-Bus objects.
type Characteristic ¶
type Characteristic interface { ReadWriteHandle Notifying() bool StartNotify() error StopNotify() error HandleNotify(NotifyHandler) error }
Characteristic corresponds to the org.bluez.GattCharacteristic1 interface. See bluez/doc/gatt-api.txt
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection represents a D-Bus connection.
func (*Connection) Discover ¶
func (conn *Connection) Discover(timeout time.Duration, address Address, uuids ...string) (Device, error)
Discover initiates discovery for a LE peripheral with the given address (if nonempty), advertising the given UUIDs. It waits for at most the specified timeout, or indefinitely if timeout = 0.
func (*Connection) GetAdapter ¶
func (conn *Connection) GetAdapter() (Adapter, error)
GetAdapter finds an Adapter in the object cache and returns it.
func (*Connection) GetCharacteristic ¶
func (conn *Connection) GetCharacteristic(uuid string) (Characteristic, error)
GetCharacteristic finds a Characteristic with the given UUID.
func (*Connection) GetDescriptor ¶
func (conn *Connection) GetDescriptor(uuid string) (Descriptor, error)
GetDescriptor finds a Descriptor with the given UUID.
func (*Connection) GetDeviceByAddress ¶
func (conn *Connection) GetDeviceByAddress(address Address) (Device, error)
GetDeviceByAddress finds a Device in the object cache with the given address.
func (*Connection) GetDeviceByName ¶
func (conn *Connection) GetDeviceByName(name string) (Device, error)
GetDeviceByName finds a Device in the object cache with the given name.
func (*Connection) GetDeviceByUUID ¶
func (conn *Connection) GetDeviceByUUID(uuids ...string) (Device, error)
GetDeviceByUUID finds a Device in the object cache matching the given UUIDs.
func (*Connection) GetService ¶
func (conn *Connection) GetService(uuid string) (Service, error)
GetService finds a Service with the given UUID.
func (*Connection) HandleNotify ¶
func (conn *Connection) HandleNotify(uuid string, handler NotifyHandler) error
HandleNotify enables notifications from the GATT characterisitc with the specified UUID and applies the given handler to them when they arrive.
func (*Connection) Print ¶
func (conn *Connection) Print(w io.Writer)
Print prints the objects in the cache.
func (*Connection) Update ¶
func (conn *Connection) Update() error
Update gets all objects and properties. See http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager
type Descriptor ¶
type Descriptor interface { ReadWriteHandle }
Descriptor corresponds to the org.bluez.GattDescriptor1 interface. See bluez/doc/gatt-api.txt
type Device ¶
type Device interface { BaseObject Address() Address AddressType() string UUIDs() []string Connected() bool Paired() bool Connect() error Disconnect() error Pair() error }
The Device type corresponds to the org.bluez.Device1 interface. See bluez/doc/devicet-api.txt
type DiscoveryTimeoutError ¶
type DiscoveryTimeoutError []string
DiscoveryTimeoutError indicates that discovery has timed out.
func (DiscoveryTimeoutError) Error ¶
func (e DiscoveryTimeoutError) Error() string
type GattHandle ¶
type GattHandle interface { BaseObject UUID() string }
GattHandle is the interface satisfied by GATT handles.
type NotifyHandler ¶
type NotifyHandler func([]byte)
NotifyHandler represents a function that handles notifications.
type Object ¶
type Object = map[string]Properties
Object represents a managed D-Bus object as a map from interface names to properties.
type Properties ¶
Properties represents a dbus interface as a map from property names to values (D-Bus variants).
type ReadWriteHandle ¶
type ReadWriteHandle interface { GattHandle ReadValue() ([]byte, error) WriteValue([]byte) error }
ReadWriteHandle is the interface satisfied by GATT objects that provide ReadValue and WriteValue operations.
type Service ¶
type Service interface { GattHandle }
Service corresponds to the org.bluez.GattService1 interface. See bluez/doc/gatt-api.txt