Documentation ¶
Index ¶
- Variables
- type Adapter
- func (a *Adapter) AddService(s *Service) error
- func (a *Adapter) Connect(address Addresser, params ConnectionParams) (*Device, error)
- func (a *Adapter) DefaultAdvertisement() *Advertisement
- func (a *Adapter) Enable() (err error)
- func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error
- func (a *Adapter) StopScan() error
- type Address
- type Addresser
- type Advertisement
- type AdvertisementFields
- type AdvertisementOptions
- type AdvertisementPayload
- type Characteristic
- type CharacteristicConfig
- type CharacteristicPermissions
- type Connection
- type ConnectionParams
- type Device
- type DeviceCharacteristic
- type DeviceService
- type Duration
- type MAC
- type MACAddress
- type ScanResult
- type Service
- type UUID
Constants ¶
This section is empty.
Variables ¶
var DefaultAdapter = &Adapter{}
DefaultAdapter is the default adapter on the system. On Linux, it is the first adapter available.
Make sure to call Enable() before using it to initialize the adapter.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
func (*Adapter) AddService ¶
AddService creates a new service with the characteristics listed in the Service struct.
func (*Adapter) Connect ¶
func (a *Adapter) Connect(address Addresser, params ConnectionParams) (*Device, error)
Connect starts a connection attempt to the given peripheral device address.
On Linux and Windows, the IsRandom part of the address is ignored.
func (*Adapter) DefaultAdvertisement ¶
func (a *Adapter) DefaultAdvertisement() *Advertisement
DefaultAdvertisement returns the default advertisement instance but does not configure it.
func (*Adapter) Enable ¶
Enable configures the BLE stack. It must be called before any Bluetooth-related calls (unless otherwise indicated).
func (*Adapter) Scan ¶
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error
Scan starts a BLE scan. It is stopped by a call to StopScan. A common pattern is to cancel the scan when a particular device has been found.
On Linux with BlueZ, incoming packets cannot be observed directly. Instead, existing devices are watched for property changes. This closely simulates the behavior as if the actual packets were observed, but it has flaws: it is possible some events are missed and perhaps even possible that some events are duplicated.
type Addresser ¶
type Addresser interface { // String of the address String() string // Set the address Set(val interface{}) // Is this address a random address? // Bluetooth addresses are roughly split in two kinds: public // (IEEE-assigned) addresses and random (not IEEE assigned) addresses. // "Random" here doesn't mean it is exactly random but at least it looks // random. Sometimes, it contains a hash. // For more information: // https://www.novelbits.io/bluetooth-address-privacy-ble/ // Set the address SetRandom(bool) IsRandom() bool }
Addresser contains a Bluetooth address, which is a MAC address plus some extra information.
type Advertisement ¶
type Advertisement struct {
// contains filtered or unexported fields
}
Advertisement encapsulates a single advertisement instance.
func (*Advertisement) Configure ¶
func (a *Advertisement) Configure(options AdvertisementOptions) error
Configure this advertisement.
On Linux with BlueZ, it is not possible to set the advertisement interval.
func (*Advertisement) Start ¶
func (a *Advertisement) Start() error
Start advertisement. May only be called after it has been configured.
type AdvertisementFields ¶
type AdvertisementFields struct { // The LocalName part of the advertisement (either the complete local name // or the shortened local name). LocalName string // ServiceUUIDs are the services (16-bit or 128-bit) that are broadcast as // part of the advertisement packet, in data types such as "complete list of // 128-bit UUIDs". ServiceUUIDs []UUID }
AdvertisementFields contains advertisement fields in structured form.
type AdvertisementOptions ¶
type AdvertisementOptions struct { // The (complete) local name that will be advertised. Optional, omitted if // this is a zero-length string. LocalName string // ServiceUUIDs are the services (16-bit or 128-bit) that are broadcast as // part of the advertisement packet, in data types such as "complete list of // 128-bit UUIDs". ServiceUUIDs []UUID // Interval in BLE-specific units. Create an interval by using NewDuration. Interval Duration }
AdvertisementOptions configures an advertisement instance. More options may be added over time.
type AdvertisementPayload ¶
type AdvertisementPayload interface { // LocalName is the (complete or shortened) local name of the device. // Please note that many devices do not broadcast a local name, but may // broadcast other data (e.g. manufacturer data or service UUIDs) with which // they may be identified. LocalName() string // HasServiceUUID returns true whether the given UUID is present in the // advertisement payload as a Service Class UUID. It checks both 16-bit // UUIDs and 128-bit UUIDs. HasServiceUUID(UUID) bool // Bytes returns the raw advertisement packet, if available. It returns nil // if this data is not available. Bytes() []byte }
AdvertisementPayload contains information obtained during a scan (see ScanResult). It is provided as an interface as there are two possible implementations: an implementation that works with raw data (usually on low-level BLE stacks) and an implementation that works with structured data.
type Characteristic ¶
type Characteristic struct {
// contains filtered or unexported fields
}
Characteristic is a single characteristic in a service. It has an UUID and a value.
type CharacteristicConfig ¶
type CharacteristicConfig struct { Handle *Characteristic UUID Value []byte Flags CharacteristicPermissions WriteEvent func(client Connection, offset int, value []byte) }
CharacteristicConfig contains some parameters for the configuration of a single characteristic.
The Handle field may be nil. If it is set, it points to a characteristic handle that can be used to access the characteristic at a later time.
type CharacteristicPermissions ¶
type CharacteristicPermissions uint8
CharacteristicPermissions lists a number of basic permissions/capabilities that clients have regarding this characteristic. For example, if you want to allow clients to read the value of this characteristic (a common scenario), set the Read permission.
const ( CharacteristicBroadcastPermission CharacteristicPermissions = 1 << iota CharacteristicReadPermission CharacteristicWriteWithoutResponsePermission CharacteristicWritePermission CharacteristicNotifyPermission CharacteristicIndicatePermission )
Characteristic permission bitfields.
func (CharacteristicPermissions) Broadcast ¶
func (p CharacteristicPermissions) Broadcast() bool
Broadcast returns whether broadcasting of the value is permitted.
func (CharacteristicPermissions) Read ¶
func (p CharacteristicPermissions) Read() bool
Read returns whether reading of the value is permitted.
func (CharacteristicPermissions) Write ¶
func (p CharacteristicPermissions) Write() bool
Write returns whether writing of the value with Write Request is permitted.
func (CharacteristicPermissions) WriteWithoutResponse ¶
func (p CharacteristicPermissions) WriteWithoutResponse() bool
WriteWithoutResponse returns whether writing of the value with Write Command is permitted.
type Connection ¶
type Connection uint16
Connection is a numeric identifier that indicates a connection handle.
type ConnectionParams ¶
type ConnectionParams struct { // The timeout for the connection attempt. Not used during the rest of the // connection. If no duration is specified, a default timeout will be used. ConnectionTimeout Duration // Minimum and maximum connection interval. The shorter the interval, the // faster data can travel between both devices but also the more power they // will draw. If no intervals are specified, a default connection interval // will be used. MinInterval Duration MaxInterval Duration }
ConnectionParams are used when connecting to a peripherals.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is a connection to a remote peripheral.
func (*Device) DiscoverServices ¶
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error)
DiscoverServices starts a service discovery procedure. Pass a list of service UUIDs you are interested in to this function. Either a slice of all services is returned (of the same length as the requested UUIDs and in the same order), or if some services could not be discovered an error is returned.
Passing a nil slice of UUIDs will return a complete list of services.
On Linux with BlueZ, this just waits for the ServicesResolved signal (if services haven't been resolved yet) and uses this list of cached services.
type DeviceCharacteristic ¶
type DeviceCharacteristic struct { UUID // contains filtered or unexported fields }
DeviceCharacteristic is a BLE characteristic on a connected peripheral device.
func (DeviceCharacteristic) EnableNotifications ¶
func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error
EnableNotifications enables notifications in the Client Characteristic Configuration Descriptor (CCCD). This means that most peripherals will send a notification with a new value every time the value of the characteristic changes.
func (DeviceCharacteristic) WriteWithoutResponse ¶
func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (n int, err error)
WriteWithoutResponse replaces the characteristic value with a new value. The call will return before all data has been written. A limited number of such writes can be in flight at any given time. This call is also known as a "write command" (as opposed to a write request).
type DeviceService ¶
type DeviceService struct { UUID // contains filtered or unexported fields }
DeviceService is a BLE service on a connected peripheral device.
func (*DeviceService) DiscoverCharacteristics ¶
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error)
DiscoverCharacteristics discovers characteristics in this service. Pass a list of characteristic UUIDs you are interested in to this function. Either a list of all requested services is returned, or if some services could not be discovered an error is returned. If there is no error, the characteristics slice has the same length as the UUID slice with characteristics in the same order in the slice as in the requested UUID list.
Passing a nil slice of UUIDs will return a complete list of characteristics.
type Duration ¶
type Duration uint16
Duration is the unit of time used in BLE, in 0.625µs units. This unit of time is used throughout the BLE stack.
func NewDuration ¶
NewDuration returns a new Duration, in units of 0.625µs. It is used both for advertisement intervals and for connection parameters.
type MAC ¶
type MAC [6]byte
MAC represents a MAC address, in little endian format.
type MACAddress ¶
type MACAddress struct { // MAC address of the Bluetooth device. MAC // contains filtered or unexported fields }
MACAddress contains a Bluetooth address which is a MAC address.
func (MACAddress) IsRandom ¶
func (mac MACAddress) IsRandom() bool
IsRandom if the address is randomly created.
func (MACAddress) SetRandom ¶
func (mac MACAddress) SetRandom(val bool)
SetRandom if is a random address.
type ScanResult ¶
type ScanResult struct { // Bluetooth address of the scanned device. Address Addresser // RSSI the last time a packet from this device has been received. RSSI int16 // The data obtained from the advertisement data, which may contain many // different properties. // Warning: this data may only stay valid until the next event arrives. If // you need any of the fields to stay alive until after the callback // returns, copy them. AdvertisementPayload }
ScanResult contains information from when an advertisement packet was received. It is passed as a parameter to the callback of the Scan method.
type Service ¶
type Service struct { UUID Characteristics []CharacteristicConfig // contains filtered or unexported fields }
Service is a GATT service to be used in AddService.
type UUID ¶
type UUID [4]uint32
UUID is a single UUID as used in the Bluetooth stack. It is represented as a [4]uint32 instead of a [16]byte for efficiency.
func New16BitUUID ¶
New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID.
Note: only use registered UUIDs. See https://www.bluetooth.com/specifications/gatt/services/ for a list.
func ParseUUID ¶
ParseUUID parses the given UUID, which must be in 00001234-0000-1000-8000-00805f9b34fb format. This means that it cannot (yet) parse 16-bit UUIDs unless they are serialized as a 128-bit UUID. If the UUID cannot be parsed, an error is returned. It will always successfully parse UUIDs generated by UUID.String().
func (UUID) Get16Bit ¶
Get16Bit returns the 16-bit version of this UUID. This is only valid if it actually is a 16-bit UUID, see Is16Bit.
func (UUID) Replace16BitComponent ¶
Replace16BitComponent returns a new UUID where bits 16..32 have been replaced with the bits given in the argument. These bits are the same bits that vary in the 16-bit compressed UUID form.
This is especially useful for the Nordic SoftDevice, because it is able to store custom UUIDs more efficiently when only these bits vary between them.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
Package rawterm provides some sort of raw terminal interface, both on hosted systems and baremetal.
|
Package rawterm provides some sort of raw terminal interface, both on hosted systems and baremetal. |
Package winbt provides a thin layer over the WinRT Bluetooth interfaces.
|
Package winbt provides a thin layer over the WinRT Bluetooth interfaces. |