A node.js module for implementing BLE (Bluetooth low energy) peripherals.
Need a BLE central module? See noble.
Note: Mac OS X and Linux are currently the only supported OSes.
- install Xcode
- 10.9 or later
- Kernel version 3.6 or above
libbluetooth-dev
sudo apt-get install bluetooth bluez-utils libbluetooth-dev
See Configure Intel Edison for Bluetooth LE (Smart) Development
npm install bleno
var bleno = require('bleno');
See examples folder for code examples.
var name = 'name';
var serviceUuids = ['fffffffffffffffffffffffffffffff0']
bleno.startAdvertising(name, serviceUuids[, callback(error)]);
Note:: there are limits on the name and service UUID's
- name
- maximum 26 bytes
- service UUID's
- 1 128-bit service UUID
- 1 128-bit service UUID + 2 16-bit service UUID's
- 7 16-bit service UUID
var uuid = 'e2c56db5dffb48d2b060d0f5a71096e0';
var major = 0; // 0x0000 - 0xffff
var minor = 0; // 0x0000 - 0xffff
var measuredPower = -59; // -128 - 127
bleno.startAdvertisingIBeacon(uuid, major, minor, measuredPower[, callback(error)]);
Notes::
- OS X:
- in iBeacon mode your peripheral is non-connectable!
var scanData = new Buffer(...); // maximum 31 bytes
var advertisementData = new Buffer(...); // maximum 31 bytes
bleno.startAdvertisingWithEIRData(advertisementData[, scanData, callback(error)]);
- For EIR format section Bluetooth Core Specification sections and 8 and 18 for more information the data format.
bleno.stopAdvertising([callback]);
var services = [
... // see PrimaryService for data type
];
bleno.setServices(services[, callback(error)]);
bleno.disconnect(); // Linux only
bleno.updateRssi([callback(error, rssi)]); // not available in OS X 10.9
var PrimaryService = bleno.PrimaryService;
var primaryService = new PrimaryService({
uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit
characteristics: [
// see Characteristic for data type
]
});
var Characteristic = bleno.Characteristic;
var characteristic = new Characteristic({
uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit
properties: [ ... ], // can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify'
secure: [ ... ], // enable security for properties, can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify', 'indicate'
value: null, // optional static value, must be of type Buffer
descriptors: [
// see Descriptor for data type
],
onReadRequest: null, // optional read request handler, function(offset, callback) { ... }
onWriteRequest: null, // optional write request handler, function(data, offset, withoutResponse, callback) { ...}
onSubscribe: null, // optional notify/indicate subscribe handler, function(maxValueSize, updateValueCallback) { ...}
onUnsubscribe: null, // optional notify/indicate unsubscribe handler, function() { ...}
onNotify: null // optional notify sent handler, function() { ...}
onIndicate: null // optional indicate confirmation received handler, function() { ...}
});
- Characteristic.RESULT_SUCCESS
- Characteristic.RESULT_INVALID_OFFSET
- Characteristic.RESULT_INVALID_ATTRIBUTE_LENGTH
- Characteristic.RESULT_UNLIKELY_ERROR
Can specify read request handler via constructor options or by extending Characteristic and overriding onReadRequest.
Parameters to handler are
offset
(0x0000 - 0xffff)callback
callback
must be called with result and data (of type Buffer
) - can be async.
var result = Characteristic.RESULT_SUCCESS;
var data = new Buffer( ... );
callback(result, data);
Can specify write request handler via constructor options or by extending Characteristic and overriding onWriteRequest.
Parameters to handler are
data
(Buffer)offset
(0x0000 - 0xffff)withoutResponse
(true | false)callback
.
callback
must be called with result code - can be async.
var result = Characteristic.RESULT_SUCCESS;
callback(result);
Can specify notify subscribe handler via constructor options or by extending Characteristic and overriding onSubscribe.
Parameters to handler are
maxValueSize
(maximum data size)updateValueCallback
(callback to call when value has changed)
Can specify notify unsubscribe handler via constructor options or by extending Characteristic and overriding onUnsubscribe.
Call the updateValueCallback
callback (see Notify subcribe), with an argument of type Buffer
Can specify notify sent handler via constructor options or by extending Characteristic and overriding onNotify.
var Descriptor = bleno.Descriptor;
var descriptor = new Descriptor({
uuid: '2901',
value: 'value' // static value, must be of type Buffer or string if set
});
state = <"unknown" | "resetting" | "unsupported" | "unauthorized" | "poweredOff" | "poweredOn">
bleno.on('stateChange', callback(state));
bleno.on('advertisingStart', callback(error));
bleno.on('advertisingStartError', callback(error));
bleno.on('advertisingStop', callback);
bleno.on('servicesSet', callback(error));
bleno.on('servicesSetError', callback(error));
bleno.on('accept', callback(clientAddress)); // not available on OS X 10.9
bleno.on('disconnect', callback(clientAddress)); // Linux only
bleno.on('rssiUpdate', callback(rssi)); // not available on OS X 10.9
Run the following command in the directory you ran npm install
from:
find -path '*bleno*Release/hci-ble' -exec sudo setcap cap_net_raw+eip '{}' \;
This grants bleno's hci-ble
binary cap_net_raw
privileges, so it can start/stop BLE advertising.
Note: The above command requires setcap
to be installed, it can be installed using the following:
- apt:
sudo apt-get install libcap2-bin
- yum:
su -c \'yum install libcap2-bin\'
hci0
is used by default to override set the BLENO_HCI_DEVICE_ID
environment variable to the interface number.
Example, specify hci1
:
sudo BLENO_HCI_DEVICE_ID=1 node <your file>.js
By default bleno uses the hostname (require('os').hostname()
) as the value for the device name (0x2a00) characterisic, to match the behaviour of OS X.
A custom device name can be specified by setting the BLENO_DEVICE_NAME
enviroment variable:
sudo BLENO_DEVICE_NAME="custom device name" node <your file>.js
-
Mac OS X:
Adapter state (unknown | reseting | unsupported | unauthorized | off | on)AdvertisementstartAdvertisingnameservice UUID's
startAdvertisingIBeaconstopAdvertising
ServicesUUIDCharacteristicsUUIDpropertiesread (static, dynamic)writewrite without responsenotify (subscribe, unsubscribe, value changed)- broadcast (not possible)
indicate- secure (not functioning, OS X issues)
- read
- write
DescriptorsUUIDread (static)- write (not possible)
- Included Services (maybe ?)
- error handling
-
Linux
Adapter state (unsupported | unauthorized | off | on)AdvertisementstartAdvertisingnameservice UUID's
startAdvertisingIBeaconstopAdvertising
ServicesUUIDCharacteristicsUUIDpropertiesread (static, dynamic)writewrite without responsenotify (subscribe, unsubscribe, value changed)- broadcast (maybe ?)
indicatesecurereadwrite
DescriptorsUUIDread (static)- write (maybe ?)
- Included Services (maybe ?)
- error handling
- Tools
- LightBlue for iOS/OS X
- nRF Master Control Panel (BLE) for Android
- hcitool and
gatttool
by BlueZ for Linux
Copyright (C) 2015 Sandeep Mistry [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.