Categorygithub.com/QuantumIntegration/gatt
modulepackage
0.0.0-20210422070550-2cf5192549a3
Repository: https://github.com/quantumintegration/gatt.git
Documentation: pkg.go.dev

# README

Package gatt provides a Bluetooth Low Energy GATT implementation.

Gatt (Generic Attribute Profile) is the protocol used to write BLE peripherals (servers) and centrals (clients).

As a peripheral, you can create services, characteristics, and descriptors, advertise, accept connections, and handle requests.

As a central, you can scan, connect, discover services, and make requests.

SETUP

gatt supports both Linux and OS X.

On Linux:

To gain complete and exclusive control of the HCI device, gatt uses HCI_CHANNEL_USER (introduced in Linux v3.14) instead of HCI_CHANNEL_RAW. Those who must use an older kernel may patch in these relevant commits from Marcel Holtmann:

Bluetooth: Introduce new HCI socket channel for user operation
Bluetooth: Introduce user channel flag for HCI devices
Bluetooth: Refactor raw socket filter into more readable code

Note that because gatt uses HCI_CHANNEL_USER, once gatt has opened the device no other program may access it.

Before starting a gatt program, make sure that your BLE device is down:

sudo hciconfig
sudo hciconfig hci0 down  # or whatever hci device you want to use

If you have BlueZ 5.14+ (or aren't sure), stop the built-in bluetooth server, which interferes with gatt, e.g.:

sudo service bluetooth stop

Because gatt programs administer network devices, they must either be run as root, or be granted appropriate capabilities:

sudo <executable>
# OR
sudo setcap 'cap_net_raw,cap_net_admin=eip' <executable>
<executable>

Usage

Please see godoc.org for documentation.

Examples

Build and run the examples on a native environment (Linux or OS X)

Go is a compiled language, which means to run the examples you need to build them first.

# Build the sample server.
go build examples/server.go
# Start the sample server.
sudo ./server

Alternatively, you can use "go run" to build and run the examples in a single step:

# Build and run the sample server.
sudo go run examples/server.go

Discoverer and explorer demonstrates central (client) functions:

# Discover surrounding peripherals.
sudo go run examples/discoverer.go

# Connect to and explorer a peripheral device.
sudo go run examples/explorer.go <peripheral ID>

Cross-compile and deploy to a target device

# Build and run the server example on a ARMv5 target device.
GOARCH=arm GOARM=5 GOOS=linux go build examples/server.go
cp server <target device>
# Start the server on the target device
sudo ./server

See the server.go, discoverer.go, and explorer.go in the examples/ directory for writing server or client programs that run on Linux and OS X.

Users, especially on Linux platforms, seeking finer-grained control over the devices can see the examples/server_lnx.go for the usage of Option, which are platform specific.

See the rest of the docs for other options and finer-grained control.

Note

Note that some BLE central devices, particularly iOS, may aggressively cache results from previous connections. If you change your services or characteristics, you may need to reboot the other device to pick up the changes. This is a common source of confusion and apparent bugs. For an OS X central, see http://stackoverflow.com/questions/20553957.

Known Issues

Currently OS X vesion does not support subscribing to indications. Please check #32 for the status of this issue.

REFERENCES

gatt started life as a port of bleno, to which it is indebted: https://github.com/sandeepmistry/bleno. If you are having problems with gatt, particularly around installation, issues filed with bleno might also be helpful references.

To try out your GATT server, it is useful to experiment with a generic BLE client. LightBlue is a good choice. It is available free for both iOS and OS X.

gatt is similar to bleno and noble, which offer BLE GATT implementations for node.js.

Gatt is released under a BSD-style license.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

CentralConnected returns a Handler, which sets the specified function to be called when a device connects to the server.
CentralDisconnected returns a Handler, which sets the specified function to be called when a device disconnects from the server.
LnxDeviceID specifies which HCI device to use.
LnxMaxConnections is an optional parameter.
LnxSendHCIRawCommand sends a raw command to the HCI device This option can be used with NewDevice or Option on Linux implementation.
LnxSetAdvertisingData sets the advertising data to the HCI device.
LnxSetAdvertisingEnable sets the advertising data to the HCI device.
LnxSetAdvertisingParameters sets the advertising parameters to the HCI device.
LnxSetScanParameters sets the scan parameters to the HCI device.
LnxSetScanResponseData sets the scan response data to the HXI device.
MustParseUUID parses a standard-format UUID string, like ParseUUID, but panics in case of error.
NewCharacteristic creates and returns a Characteristic.
NewDescriptor creates and returns a Descriptor.
No description provided by the author
NewService creates and initialize a new Service using u as it's UUID.
No description provided by the author
ParseUUID parses a standard-format UUID string, such as "1800" or "34DA3AD1-7110-41A1-B1EF-4430F509CDE7".
PeripheralConnected returns a Handler, which sets the specified function to be called when a remote peripheral device connects.
PeripheralDisconnected returns a Handler, which sets the specified function to be called when a remote peripheral device disconnects.
PeripheralDiscovered returns a Handler, which sets the specified function to be called when a remote peripheral device is found during scan procedure.
UUID16 converts a uint16 (such as 0x1800) to a UUID.
UUIDContains returns a boolean reporting whether u is in the slice s.

# Constants

No attribute found within the given attribute handle range.
The attribute cannot be read or written using the Read Blob Request.
The attribute requires authentication before it can be read or written.
The attribute requires authorization before it can be read or written.
The attribute requires encryption before it can be read or written.
The Encryption Key Size used for encrypting this link is insufficient.
Insufficient Resources to complete the request.
The attribute value length is invalid for the operation.
The attribute handle given was not valid on this server.
Offset specified was past the end of the attribute.
The attribute PDU was invalid.
Too many prepare writes have been queued.
The attribute cannot be read.
Attribute server does not support the request received from the client.
Success.
The attribute request that was requested has encountered an error that was unlikely, and therefore could not be completed as requested.
The attribute type is not a supported grouping attribute as defined by a higher layer specification.
The attribute cannot be written.
may be brocasted.
supports extended properties.
supports Indications.
supports notifications.
may be read.
supports signed write.
may be written to, with a reply.
may be written to, with no reply.
No description provided by the author
MaxEIRPacketLength is the maximum allowed AdvertisingPacket and ScanResponsePacket length.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Supported statuses for GATT characteristic read/write operations.
Supported statuses for GATT characteristic read/write operations.
Supported statuses for GATT characteristic read/write operations.

# Variables

No description provided by the author
ref.
ErrEIRPacketTooLong is the error returned when an AdvertisingPacket or ScanResponsePacket is too long.
No description provided by the author

# Structs

This is borrowed from core bluetooth.
AdvPacket is an utility to help crafting advertisment or scan response data.
A Characteristic is a BLE characteristic.
Descriptor is a BLE descriptor.
A ReadRequest is a characteristic read request from a connected device.
A Request is the context for a request from a connected central device.
A Service is a BLE service.
FIXME: check the unmarshalling of this data structure.
A UUID is a BLE UUID.

# Interfaces

Central is the interface that represent a remote central device.
Device defines the interface for a BLE device.
A Notifier provides a means for a GATT server to send notifications about value changes to a connected device.
A NotifyHandler handles GATT notification requests.
Peripheral is the interface that represent a remote peripheral device.
A ReadHandler handles GATT read requests.
No description provided by the author
A WriteHandler handles GATT write requests.

# Type aliases

No description provided by the author
No description provided by the author
A Handler is a self-referential function, which registers the options specified.
NotifyHandlerFunc is an adapter to allow the use of ordinary functions as NotifyHandlers.
An Option is a self-referential function, which sets the option specified.
No description provided by the author
ReadHandlerFunc is an adapter to allow the use of ordinary functions as ReadHandlers.
No description provided by the author
WriteHandlerFunc is an adapter to allow the use of ordinary functions as WriteHandlers.