BLE

Platform Support

The following shows BLE module APIs available for each platform.

LinuxRT-ThreadZephyr
ble.startAdvertisingOXO
ble.stopAdvertisingOXO
ble.setServicesOXO

BLE - Bluetooth Low Energy

Event: ‘advertisingStart’

  • callback {Function}
    • error {Error}

Emitted when advertisement starts.

Example

  1. var ble = require('ble');
  2. ble.on('advertisingStart', function(error) {
  3. console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
  4. if (!error) {
  5. ble.setServices([
  6. // service data
  7. ]);
  8. }
  9. });

Event: ‘stateChange’

  • callback {Function}
    • state {String} Can be ‘unknown’, ‘resetting’, ‘unsupported’, ‘unauthorized’, ‘poweredOff’ or ‘poweredOn’.

Emitted when adapter state is changed.

Example

  1. var ble = require('ble');
  2. ble.on('stateChange', function(state){
  3. console.log('onStateChange: ' + state);
  4. if (state == 'poweredOn') {
  5. ble.startAdvertising('iotjs', ['data'], function(err) {
  6. });
  7. } else {
  8. ble.stopAdvertising(function(err) {
  9. });
  10. }
  11. });

ble.startAdvertising(name, serviceUuids[, callback])

  • name {string} Maximum 26 bytes.
  • serviceUuids {Array[String]}
    • 1 128-bit service UUID
    • 1 128-bit service UUID + 2 16-bit service UUIDs
    • 7 16-bit service UUID
  • callback {Function} Error handler.
    • error {Error}

Starts advertising.

ble.state must be in poweredOn state before advertising is started. ble.on('stateChange', callback(state)); can be used to register for state change events.

Example

  1. var name = 'name';
  2. var serviceUuids = ['fffffffffffffffffffffffffffffff0']
  3. ble.startAdvertising(name, serviceUuids[, callback(error)]);

ble.stopAdvertising(callback)

  • callback {Function} Error handler.
    • error {Error}

Stops advertising.

ble.setServices(services[, callback])

  • services {Array[PrimaryService]}
  • callback {Function} Error handler.
    • error {Error}

Sets the primary services available on the peripheral.

Class: Descriptor

Descriptors are defined attributes that describe a characteristic value.

new Descriptor(options)

  • options {Object}
    • uuid: {string} A Universally Unique ID (UUID) is a 16 or 128-bit hex value used to identify the type of every attribute.
    • value {string|Buffer}

Example

  1. var descriptor = new Descriptor({
  2. uuid: '2901',
  3. value: 'value'
  4. });

Class: Characteristic

Characteristics are defined attribute types that contain a single logical value.

new Characteristic(options)

  • options {Object}
    • uuid: {string} A Universally Unique ID (UUID) is a 16 or 128-bit hex value used to identify the type of every attribute.
    • properties {Array[string]} Can be a combination of ‘read’, ‘write’, ‘writeWithoutResponse’, ‘notify’ and ‘indicate’.
    • secure {Array[string]} Enables security for properties, can be a combination of ‘read’, ‘write’, ‘writeWithoutResponse’, ‘notify’ and ‘indicate’.
    • value {Buffer}
    • descriptors {Array[Descriptor]}
    • onReadRequest {Function} Read request handler. (optional)
      • offset {number} (0x0000 - 0xffff)
      • callback {Function}
        • result {Characteristic.RESULT_*}
        • data {Buffer}
    • onWriteRequest {Function} Write request handler. (optional)
      • data {Buffer}
      • offset {number} (0x0000 - 0xffff)
      • withoutResponse {boolean}
      • callback {Function}
        • result {Characteristic.RESULT_*}
    • onSubscribe {Function} Notify/indicate subscribe handler. (optional)
      • maxValueSize {number} Maximum data size.
      • updateValueCallback {Function}
    • onUnsubscribe {Function} Notify/indicate unsubscribe handler. (optional)
    • onNotify {Function} Notify sent handler. (optional)
    • onIndicate {Function} Indicate confirmation received handler. (optional) Returns: {Characteristic}

Example

  1. var characteristic = new Characteristic({
  2. uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit
  3. properties: ['read', 'write'],
  4. secure: [],
  5. value: null,
  6. descriptors: [descriptor],
  7. onReadRequest: null,
  8. onWriteRequest: null,
  9. onSubscribe: null,
  10. onUnsubscribe: null,
  11. onNotify: null,
  12. onIndicate: null
  13. });

Characteristic.RESULT_SUCCESS

Characteristic.RESULT_INVALID_OFFSET

Characteristic.RESULT_INVALID_ATTRIBUTE_LENGTH

Characteristic.RESULT_UNLIKELY_ERROR

Class: PrimaryService

PrimaryService is a collection of characteristics and relationships to other services that encapsulate the behavior of part of a device.

new PrimaryService(options)

  • options {Object}
    • uuid {string} A Universally Unique ID (UUID) is a 16 or 128-bit hex value used to identify the type of every attribute.
    • characteristics {Array[Characteristic]}

Example

  1. var primaryService = new PrimaryService({
  2. uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit
  3. characteristics: [
  4. // see Characteristic for data type
  5. ]
  6. });