ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

The following shows BLE module APIs available for each platform.

  Linux
(Ubuntu)
Raspbian
(Raspberry Pi)
NuttX
(STM32F4-Discovery)
TizenRT
(Artik053)
ble.startAdvertising O O X X
ble.stopAdvertising O O X X
ble.setServices O O X X

BLE - Bluetooth Low Energy

Event: ‘advertisingStart’

Emitted when advertisement starts.

Example

var ble = require('ble');
ble.on('advertisingStart', function(error) {
  console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
  if (!error) {
    ble.setServices([
        // service data
    ]);
  }
});

Event: ‘stateChange’

Emitted when adapter state is changed.

Example

var ble = require('ble');
ble.on('stateChange', function(state){
  console.log('onStateChange: ' + state);

  if (state == 'poweredOn') {
    ble.startAdvertising('iotjs', ['data'], function(err) {
    });
  } else {
    ble.stopAdvertising(function(err) {
    });
  }
});

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

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

var name = 'name';
var serviceUuids = ['fffffffffffffffffffffffffffffff0']

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

ble.stopAdvertising(callback)

Stops advertising.

ble.setServices(services[, callback])

Sets the primary services available on the peripheral.

Class: Descriptor

Descriptors are defined attributes that describe a characteristic value.

new Descriptor(options)

Example

var descriptor = new Descriptor({
    uuid: '2901',
    value: 'value'
});

Class: Characteristic

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

new Characteristic(options)

Example

var characteristic = new Characteristic({
  uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit
  properties: ['read', 'write'],
  secure: [],
  value: null,
  descriptors: [descriptor],
  onReadRequest: null,
  onWriteRequest: null,
  onSubscribe: null,
  onUnsubscribe: null,
  onNotify: null,
  onIndicate: null
});

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)

Example

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