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 |
callback
{Function}- error {Error}
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
]);
}
});
callback
{Function}state
{String} Can be 'unknown', 'resetting', 'unsupported', 'unauthorized', 'poweredOff' or 'poweredOn'.
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) {
});
}
});
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
var name = 'name';
var serviceUuids = ['fffffffffffffffffffffffffffffff0']
ble.startAdvertising(name, serviceUuids[, callback(error)]);
callback
{Function} Error handler.error
{Error}
Stops advertising.
services
{Array[PrimaryService]}callback
{Function} Error handler.error
{Error}
Sets the primary services available on the peripheral.
Descriptors are defined attributes that describe a characteristic value.
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
var descriptor = new Descriptor({
uuid: '2901',
value: 'value'
});
Characteristics are defined attribute types that contain a single logical value.
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
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
});
PrimaryService is a collection of characteristics and relationships to other services that encapsulate the behavior of part of a device.
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
var primaryService = new PrimaryService({
uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit
characteristics: [
// see Characteristic for data type
]
});