- The
BluetoothPeripheralDeviceinterface - The
BluetoothServicedictionary - The
BluetoothDescriptordictionary - The
BluetoothCharacteristicinterface - The
BluetoothCharacteristicRequestinterface
This API exposes Bluetooth Smart functionality for Bluetooth Smart Peripheral (BSP) mode. The terminology and context is taken from the Bluetooth 4.2. specification.
A Bluetooth Smart Peripheral (BSP) device supports the following functionality:
- Host a GATT (Generic Attribute Profile) server that exposes a hierarchy of
Service,CharacteristicandDescriptorobjects. - Broadcast (advertise) information (e.g. services) using advertisement packets. Advertisement or Scan Response data packet is a 31 byte payload used to advertise the device's capabilities and connection parameters.
- Accept connections from Bluetooth Smart devices in Central mode that can access its services exposed through the the Generic Access Profile (GAP). A device has only one instance of the GAP service in the GATT server. The GAP service is a GATT based service with the service UUID as «GAP Service» defined in the Bluetooth Assigned Numbers document.
The W3C Web Bluetooth API exposes functionality for Bluetooth Smart clients (Central mode).
This API is used by applications that implement a Bluetooth Peripheral device and expose sensor data via Bluetooth.
- Applications define the Bluetooth Smart services, characteristics and descriptors, then register them with the implementation.
- Applications can build Bluetooth advertisement packets, then start and stop advertising.
- For the defined characteristics, applications should register event listeners for handling requests coming from clients. The event listeners receive the request, and should reply to them with appropriate data, or with an error code.
var btdevice = require('bluetooth-peripheral');If the functionality is not supported by the platform, require should throw NotSupportedError. If the underlying platform doesn't allow using the functionality, require should throw SecurityError.
| Property | Type | Optional | Default value | Represents |
|---|---|---|---|---|
address |
String | no | undefined |
Bluetooth Device Address |
addressType |
String enum | no | undefined |
Bluetooth Device Address Type |
name |
String | yes | undefined |
the Bluetooth Device Name in scan responses |
enabled |
boolean | no | false | whether Bluetooth is enabled |
services |
array of service objects | no | [] | Primary services on the device |
version |
string | no | version inpackage.json |
API version |
| Event name | Event callback argument |
|---|---|
statechange |
N/A |
connect |
String (client Bluetooth Device Address) |
disconnect |
String (client Bluetooth Device Address) |
error |
Error |
| Method signature | Description |
|---|---|
enable() |
enable the device |
disable() |
disable the device |
startAdvertising(advertisement, options) |
start sending advertisment packets |
stopAdvertising() |
stop sending advertisment packets |
addService(service) |
add a service object to the device |
removeService(service) |
remove a service object |
The version property is read-only, and provides the master API version, as specified in the version property of package.json.
The statechange event is emitted when the enabled property is changed. Event listeners are called with no parameters and should check the value of the enabled property.
The connect event is emitted when a Bluetooth Smart device in Central mode (client) is connected. Event listeners are invoked with the client's Bluetooth Device Address as a string argument.
The disconnect event is emitted when a connected client device is disconnected. Event listeners are invoked with the client's Bluetooth Device Address as a string argument.
The error event is emitted when a Bluetooth error needs to be reported to the application. Event listeners are invoked with an Error object as argument.
Requests enabling the device, and Bluetooth Smart functionality if needed, then returns.
When the device is enabled, implementations should set the enabled property to true and emit the statechange event.
Requests disabling the device, then returns. When the device is disabled, implementations should set the enabled property to false and emit the statechange event.
Requests the platform to send Bluetooth Smart advertising packets initialized from the arguments.
It is an object with the following properties:
| Property | Type | Optional | Default value | Represents |
|---|---|---|---|---|
connectable |
boolean | yes | true |
whether the device can be connected to |
minInterval |
Number | yes | selected by the platform | minimum time interval between advertisements |
maxInterval |
Number | yes | selected by the platform | maximum time interval between advertisements |
The connectable advertisement option is true when the device is allowed to be connected by clients.
The minInterval advertisement option tells the minimum time interval between consecutive advertisement packets in milliseconds. The value can be between 100 and 32000 milliseconds. Platforms may override this value.
The maxInterval advertisement option tells the maximum time interval between consecutive advertisement packets in milliseconds. The value can be between 100 and 32000 milliseconds. Platforms may override this value.
Advertisement packet data can be of the following types: string (e.g. flags), device name, transmit power, service UUID (16, 32, or 128 bit), service data, public or random target address, advertising interval, device address, Bluetooth Smart role, URI, or manufacturer data. Applications are expected to build the binary advertisement packets based on the Bluetooth specifications as a Buffer object. This API provides helpers for the most common data types. It is an object with properties described in the startadvertising() method steps.
The startadvertising() method runs the following steps:
- Return a
Promiseobjectpromiseand continue in parallel. - If
options.connectableis nottrue, then setoptions.connectabletofalse. - If
options.minIntervalisundefined, setoptions.minIntervalto a platform default value. Otherwise, if it is a number, and it is smaller than 100 or bigger than 32000, rejectpromisewithTypeError. Otherwise, setoptions.minIntervalto that value rounded down to the nearest value divisible by 100. - If
options.maxIntervalisundefined, setoptions.maxIntervalto a platform default value. Otherwise, if it is a number, and it is smaller than 100 or bigger than 32000, rejectpromisewithTypeError. Otherwise, setoptions.maxIntervalto that value rounded down to the nearest value divisible by 100. - If
advertisementis not an object, rejectpromisewithTypeError. - If
advertisement.scanResponseis an object andadvertisement.scanResponse.nameis a string oradvertisement.scanResponse.dateis aBufferobject, then letscanResponsebeadvertisement.scanResponse. - If
advertisement.datais aBufferobject, letbuffertake that value. - Otherwise, let
bufferbe an emptyBuffer, and use the properties ofadvertisement(when defined) to preparebufferbased on the following properties ofadvertisement. The algorithm on how to preparebufferis described in the Bluetooth specification, and the platform may have an API to support that.uuidsis an array of Bluetooth UUID strings representing services; if specified, implementations SHOULD add them to the advertisement packetbuffercompliant to the Bluetooth specification.serviceDatais an object with two properties, a UUID stringuuidand aBufferobjectdata. Implementations should adduuidanddatatobuffer, if there is sufficient space in the advertisement packet. If there is not sufficient space, skip to the next step.manufacturerDatais an object with two properties, a numbermanufacturerIdand aBufferdata. Implementations should addmanufacturerIdanddatatobuffer, if there is sufficient space in the advertisement packet. If there is not sufficient space, skip to the next step.deviceClassis a number. If specified, implementations should add it tobuffer, if there is sufficient space in the advertisement packet. If there is not sufficient space, skip to the next step.includeTxPoweris a boolean value. Iftrue, then implementations should add the transmit power to the advertisement packet, if there is space. If not, skip to the next step.
- Request the underlying platform to start sending the prepared advertisement packet
bufferwith the options specified inoptions. If the request fails, rejectpromisewith anErrorobjecterrorthat haserror.messageset to"BluetoothStartAdvertisement". - If the implementation can read the advertisement options used by the platform for this advertisement, set
options.minIntervalandoptions.maxIntervalto the actual values used by the platform. - Resolve
promisewithoptions.
Requests the platform to stop sending advertisement packets. It runs the following steps:
- Request from the underlying platform to stop the current advertisement.
- If the request is unsuccessful, throw an
Errorobjecterrorwitherror.messageset to"BluetoothStopAdvertisement".
Adds a service to the internal slot representing the primary services. Returns false if service is not a valid BluetoothService object, or if service.primary is false, or if service.uuid has already been added.
Removes a service from the internal slot that represents the primary services of the device. Returns false if service is not a valid BluetoothService object, or if service.uuid is not found in the internal slot of Bluetooth services. If recursive is true, then also removes all services in service.includedServices that are not used by any other service.
It is an object with the following properties:
| Property | Type | Optional | Default value | Represents |
|---|---|---|---|---|
uuid |
String | no | undefined |
Bluetooth Service UUID |
primary |
boolean | no | false |
whether primary (root) service |
characteristics |
array of BluetoothCharacteristic objects |
no | [] | Bluetooth characteristics |
includedServices |
array of String | no | [] | list of UUIDs of included services |
It is an object with the following properties:
| Property | Type | Optional | Default value | Represents |
|---|---|---|---|---|
uuid |
String | no | undefined |
Bluetooth Descriptor UUID |
value |
String | no | undefined |
descriptor value |
flags |
array of String | no | [] | flags (Bluetooth "properties") |
The flags property contains a maximum of 2 strings that can be either "read" or "write".
It has the following read-only properties:
| Property | Type | Optional | Default value | Represents |
|---|---|---|---|---|
uuid |
String | no | undefined |
Bluetooth Descriptor UUID |
flags |
array of String | no | [] | flags (Bluetooth "properties") |
descriptors |
array of objects | no | [] | Bluetooth descriptors |
notifying |
boolean | no | undefined |
whether notifications are on |
| Method signature | Description |
|---|---|
| startNotifications() | turn on notifications |
| stopNotifications() | turn off notifications |
| onread(handler) | registers callback for handling read requests |
| onwrite(handler) | registers callback for handling write requests |
| onsubscribe(handler) | registers callback for handling subscribe requests |
| onunsubscribe(handler) | registers callback for handling unsubscribe requests |
| onerror(handler) | registers error handler |
The BluetoothCharacteristic object can be constructed with a dictionary that can have any or all of the following properties of BluetoothCharacteristic: uuid, flags, descriptors.
The uuid property is a string that represents a Bluetooth UUID.
The flags property is an array of strings that can take the following values: "read", "write", "notify".
The descriptors property is an array of BluetoothDescriptor objects that describe this Bluetooth Characteristic.
The notifying property is a boolean that is true when notifications are turned on and false otherwise.
Requests the underlying platform to start sending notifications for this characteristic to all subscribed clients.
Requests the underlying platform to stop sending notifications for this characteristic.
Registers a function to handle read requests for the Characteristic. The handler argument is a function that accepts a BluetoothCharacteristicRequest argument.
Registers a function to handle write requests for the Characteristic. The handler argument is a function that accepts a BluetoothCharacteristicRequest argument.
Registers a function to handle subscribe requests for the Characteristic. The handler argument is a function that accepts a BluetoothCharacteristicRequest argument.
Registers a function to handle unsubscribe requests for the Characteristic. The handler argument is a function that accepts a BluetoothCharacteristicRequest argument.
Registers a function to handle Bluetooth errors concerning operations with Characteristics that need to be reported to the application. The handler argument is a function that accepts an Error object as argument.
Contains the following read-only properties:
| Property | Type | Optional | Default value | Represents |
|---|---|---|---|---|
type |
String | no | undefined |
request type |
source |
String | no | undefined |
client Bluetooth Device Address |
data |
Buffer |
yes | null |
data |
offset |
number | yes | 0 | Offset in data |
needsResponse |
boolean | yes | true |
whether response needs to be sent |
| Method signature | Description |
|---|---|
| respond(data) | send a response with data |
| respondWithError(error) | send an error response |
The type property is a string that can take one of the following values: "read", "write", "notify", "subscribe", "unsubscribe".
The source property is a string representing the Bluetooth Device Address of the client.
The data property is a Buffer object that is not null for "write" operation, and null otherwise.
The offset property is a positive number that specifies the byte offset in data pertaining to the operation (read or write).
The needsResponse property is a boolean that is by default true.
Sends a response to the request. It executes the following steps:
- Create a response to the request, and include
datain the response. - Request from the underlying platform to send the response to the client.
- If the request is unsuccessful, throw an
Errorobjecterrorwitherror.messageset to"BluetoothSendResponse".
Sends an error response to the request. It executes the following steps:
- Create an error response to the request, and include
errorin the response. - Request from the underlying platform to send the response to the client.
- If the request is unsuccessful, throw an
Errorobjecterrorwitherror.messageset to"BluetoothSendErrorResponse".