Node BLE APIs
May 19, 2024 · View on GitHub
Classes
- Adapter
Adapter class interacts with the local bluetooth adapter
- Bluetooth
Top level object that represent a bluetooth session
- Device ⇐
EventEmitter Device class interacts with a remote device.
- GattCharacteristic ⇐
EventEmitter GattCharacteristic class interacts with a GATT characteristic.
- GattServer
GattServer class that provides interaction with device GATT profile.
- GattService
GattService class interacts with a remote GATT service.
Functions
- createBluetooth() ⇒
NodeBleInit Init bluetooth session and return
Typedefs
- WritingMode
- NodeBleSession :
Object
Specs
Adapter
Adapter class interacts with the local bluetooth adapter
Kind: global class
See: You can construct an Adapter session via getAdapter method
- Adapter
- .getAddress() ⇒
string - .getAddressType() ⇒
string - .getName() ⇒
string - .getAlias() ⇒
string - .isPowered() ⇒
boolean - .isDiscovering() ⇒
boolean - .startDiscovery()
- .stopDiscovery()
- .devices() ⇒
Array.<string> - .getDevice(uuid) ⇒
Device - .waitDevice(uuid, [timeout], [discoveryInterval]) ⇒
Device - .toString() ⇒
string
- .getAddress() ⇒
adapter.getAddress() ⇒ string
The Bluetooth device address.
Kind: instance method of Adapter
adapter.getAddressType() ⇒ string
The Bluetooth device Address Type. (public, random)
Kind: instance method of Adapter
adapter.getName() ⇒ string
The Bluetooth system name
Kind: instance method of Adapter
adapter.getAlias() ⇒ string
The Bluetooth friendly name.
Kind: instance method of Adapter
adapter.isPowered() ⇒ boolean
Current adapter state.
Kind: instance method of Adapter
adapter.isDiscovering() ⇒ boolean
Indicates that a device discovery procedure is active.
Kind: instance method of Adapter
adapter.startDiscovery()
This method starts the device discovery session.
Kind: instance method of Adapter
adapter.stopDiscovery()
This method will cancel any previous StartDiscovery transaction.
Kind: instance method of Adapter
adapter.devices() ⇒ Array.<string>
List of found device names (uuid).
Kind: instance method of Adapter
adapter.getDevice(uuid) ⇒ Device
Init a device instance and returns it.
Kind: instance method of Adapter
| Param | Type | Description |
|---|---|---|
| uuid | string | Device Name. |
adapter.waitDevice(uuid, [timeout], [discoveryInterval]) ⇒ Device
Wait that a specific device is found, then init a device instance and returns it.
Kind: instance method of Adapter
| Param | Type | Default | Description |
|---|---|---|---|
| uuid | string | Device Name. | |
| [timeout] | number | 120000 | Time (ms) to wait before throwing a timeout expection. |
| [discoveryInterval] | number | 1000 | Interval (ms) frequency that verifies device availability. |
adapter.toString() ⇒ string
Human readable class identifier.
Kind: instance method of Adapter
Bluetooth
Top level object that represent a bluetooth session
Kind: global class
See: You can construct a Bluetooth session via createBluetooth function
- Bluetooth
- .adapters() ⇒
Array.<string> - .defaultAdapter() ⇒
Adapter - .getAdapter(adapter) ⇒
Adapter - .activeAdapters() ⇒
Promise.<Array.<Adapter>>
- .adapters() ⇒
bluetooth.adapters() ⇒ Array.<string>
List of available adapter names
Kind: instance method of Bluetooth
bluetooth.defaultAdapter() ⇒ Adapter
Get first available adapter
Kind: instance method of Bluetooth
Throws:
- Will throw an error if there aren't available adapters.
bluetooth.getAdapter(adapter) ⇒ Adapter
Init an adapter instance and returns it
Kind: instance method of Bluetooth
Throw: Will throw adapter not found if provided name isn't valid.
| Param | Type | Description |
|---|---|---|
| adapter | string | Name of an adapter |
bluetooth.activeAdapters() ⇒ Promise.<Array.<Adapter>>
List all available (powered) adapters
Kind: instance method of Bluetooth
Device ⇐ EventEmitter
Device class interacts with a remote device.
Kind: global class
Extends: EventEmitter
See: You can construct a Device object via getDevice method
- Device ⇐
EventEmitter- .getName() ⇒
string - .getAddress() ⇒
string - .getAddressType() ⇒
string - .getAlias() ⇒
string - .getRSSI() ⇒
number - .getTXPower() ⇒
number - .getManufacturerData() ⇒
Object.<string, any> - .getAdvertisingData() ⇒
Object.<string, any> - .getServiceData() ⇒
Object.<string, any> - .isPaired() ⇒
boolean - .isConnected() ⇒
boolean - .pair()
- .cancelPair()
- .connect()
- .disconnect()
- .gatt() ⇒
GattServer - .toString() ⇒
string - "connect"
- "disconnect"
- .getName() ⇒
device.getName() ⇒ string
The Bluetooth remote name.
Kind: instance method of Device
device.getAddress() ⇒ string
The Bluetooth device address of the remote device.
Kind: instance method of Device
device.getAddressType() ⇒ string
The Bluetooth device Address Type (public, random).
Kind: instance method of Device
device.getAlias() ⇒ string
The name alias for the remote device.
Kind: instance method of Device
device.getRSSI() ⇒ number
Received Signal Strength Indicator of the remote device
Kind: instance method of Device
device.getTXPower() ⇒ number
Advertised transmitted power level.
Kind: instance method of Device
device.getManufacturerData() ⇒ Object.<string, any>
Advertised transmitted manufacturer data.
Kind: instance method of Device
device.getAdvertisingData() ⇒ Object.<string, any>
Advertised transmitted data. (experimental: this feature might not be fully supported by bluez)
Kind: instance method of Device
device.getServiceData() ⇒ Object.<string, any>
Advertised transmitted data.
Kind: instance method of Device
device.isPaired() ⇒ boolean
Indicates if the remote device is paired.
Kind: instance method of Device
device.isConnected() ⇒ boolean
Indicates if the remote device is currently connected.
Kind: instance method of Device
device.pair()
This method will connect to the remote device
Kind: instance method of Device
device.cancelPair()
This method can be used to cancel a pairing operation initiated by the Pair method.
Kind: instance method of Device
device.connect()
Connect to remote device
Kind: instance method of Device
device.disconnect()
Disconnect remote device
Kind: instance method of Device
device.gatt() ⇒ GattServer
Init a GattServer instance and return it
Kind: instance method of Device
device.toString() ⇒ string
Human readable class identifier.
Kind: instance method of Device
"connect"
Connection event
Kind: event emitted by Device
Properties
| Name | Type | Description |
|---|---|---|
| connected | boolean | Indicates current connection status. |
"disconnect"
Disconection event
Kind: event emitted by Device
Properties
| Name | Type | Description |
|---|---|---|
| connected | boolean | Indicates current connection status. |
GattCharacteristic ⇐ EventEmitter
GattCharacteristic class interacts with a GATT characteristic.
Kind: global class
Extends: EventEmitter
See: You can construct a GattCharacteristic object via getCharacteristic method.
- GattCharacteristic ⇐
EventEmitter- .getUUID() ⇒
string - .getFlags() ⇒
Array.<string> - .isNotifying() ⇒
boolean - .readValue([offset]) ⇒
Buffer - .writeValue(value, [optionsOrOffset])
- .writeValueWithoutResponse(value, [offset]) ⇒
Promise - .writeValueWithResponse(value, [offset]) ⇒
Promise - .startNotifications()
- "valuechanged"
- .getUUID() ⇒
gattCharacteristic.getUUID() ⇒ string
128-bit characteristic UUID.
Kind: instance method of GattCharacteristic
gattCharacteristic.getFlags() ⇒ Array.<string>
Defines how the characteristic value can be used.
Kind: instance method of GattCharacteristic
gattCharacteristic.isNotifying() ⇒ boolean
True, if notifications or indications on this characteristic are currently enabled.
Kind: instance method of GattCharacteristic
gattCharacteristic.readValue([offset]) ⇒ Buffer
Read the value of the characteristic
Kind: instance method of GattCharacteristic
| Param | Type | Default |
|---|---|---|
| [offset] | number | 0 |
gattCharacteristic.writeValue(value, [optionsOrOffset])
Write the value of the characteristic.
Kind: instance method of GattCharacteristic
| Param | Type | Default | Description |
|---|---|---|---|
| value | Buffer | Buffer containing the characteristic value. | |
| [optionsOrOffset] | number | Object | 0 | Starting offset or writing options. |
| [optionsOrOffset.offset] | number | 0 | Starting offset. |
| [optionsOrOffset.type] | WritingMode | reliable | Writing mode |
gattCharacteristic.writeValueWithoutResponse(value, [offset]) ⇒ Promise
Write the value of the characteristic without waiting for the response.
Kind: instance method of GattCharacteristic
| Param | Type | Default | Description |
|---|---|---|---|
| value | Buffer | Buffer containing the characteristic value. | |
| [offset] | number | 0 | Starting offset. |
gattCharacteristic.writeValueWithResponse(value, [offset]) ⇒ Promise
Write the value of the characteristic and wait for the response.
Kind: instance method of GattCharacteristic
| Param | Type | Default | Description |
|---|---|---|---|
| value | Buffer | Buffer containing the characteristic value. | |
| [offset] | number | 0 | Starting offset. |
gattCharacteristic.startNotifications()
Starts a notification session from this characteristic. It emits valuechanged event when receives a notification.
Kind: instance method of GattCharacteristic
"valuechanged"
Notification event
Kind: event emitted by GattCharacteristic
GattServer
GattServer class that provides interaction with device GATT profile.
Kind: global class
See: You can construct a Device object via gatt method
- GattServer
- .services() ⇒
Array.<string> - .getPrimaryService(uuid) ⇒
GattService
- .services() ⇒
gattServer.services() ⇒ Array.<string>
List of available services
Kind: instance method of GattServer
gattServer.getPrimaryService(uuid) ⇒ GattService
Init a GattService instance and return it
Kind: instance method of GattServer
| Param | Type |
|---|---|
| uuid | string |
GattService
GattService class interacts with a remote GATT service.
Kind: global class
See: You can construct a GattService object via getPrimaryService method.
- GattService
- .isPrimary() ⇒
boolean - .getUUID() ⇒
string - .characteristics() ⇒
Array.<string> - .getCharacteristic(uuid) ⇒
GattCharacteristic - .toString() ⇒
string
- .isPrimary() ⇒
gattService.isPrimary() ⇒ boolean
Indicates whether or not this GATT service is a primary service.
Kind: instance method of GattService
gattService.getUUID() ⇒ string
128-bit service UUID.
Kind: instance method of GattService
gattService.characteristics() ⇒ Array.<string>
List of available characteristic names.
Kind: instance method of GattService
gattService.getCharacteristic(uuid) ⇒ GattCharacteristic
Init a GattCharacteristic instance and return it
Kind: instance method of GattService
| Param | Type | Description |
|---|---|---|
| uuid | string | Characteristic UUID. |
gattService.toString() ⇒ string
Human readable class identifier.
Kind: instance method of GattService
createBluetooth() ⇒ NodeBleInit
Init bluetooth session and return
Kind: global function
Example
const { createBluetooth } = require('node-ble')
async function main () {
const { bluetooth, destroy } = createBluetooth()
const adapter = await bluetooth.defaultAdapter()
// do here your staff
destroy()
}
WritingMode
Kind: global typedef
Properties
| Name | Type | Description |
|---|---|---|
| command | string | Write without response |
| request | string | Write with response |
| reliable | string | Reliable Write |
NodeBleSession : Object
Kind: global typedef
Properties
| Name | Type | Description |
|---|---|---|
| bluetooth | Bluetooth | Bluetooth session |
| destroy | func | Close bluetooth session |