Created
October 17, 2019 19:05
-
-
Save jhays/7f5a5dc37e683b8ddc8aa72614df5a05 to your computer and use it in GitHub Desktop.
A sample BLE peripheral service and characteristic for use with Node.js and bleno.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* characteristic.js | |
* A simple custom BLE peripheral characteristic for use with Node.js and bleno. | |
* This characteristic supports read, write, and notify properties. | |
* Julian Hays - 10/14/19 | |
*/ | |
var util = require('util'); | |
var bleno = require('bleno-mac'); //or 'bleno-mac' if you are using that | |
var BlenoCharacteristic = bleno.Characteristic; | |
var CustomCharacteristic = function() { | |
CustomCharacteristic.super_.call(this, { | |
uuid: 'fd758b93-0bfa-4c52-8af0-85845a74a606', | |
properties: ['read', 'write', 'notify'] | |
}); | |
this._value = new Buffer(0); | |
this._updateValueCallback = null; | |
}; | |
util.inherits(CustomCharacteristic, BlenoCharacteristic); | |
module.exports = CustomCharacteristic; | |
CustomCharacteristic.prototype.onReadRequest = function (offset, callback) { | |
console.log('CustomCharacteristic onReadRequest'); | |
var data = new Buffer(1); | |
data.writeUInt8(42, 0); | |
callback(this.RESULT_SUCCESS, data); | |
}; | |
CustomCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) { | |
this._value = data; | |
console.log('CustomCharacteristic - onWriteRequest: value = ' + this._value.toString('hex')); | |
callback(this.RESULT_SUCCESS); | |
}; | |
var isSubscribed = false | |
var notifyInterval = 5 //seconds | |
function delayedNotification(callback) { | |
setTimeout(function() { | |
if (isSubscribed) { | |
var data = Buffer(3); | |
var now = new Date(); | |
data.writeUInt8(now.getHours(), 0); | |
data.writeUInt8(now.getMinutes(), 1); | |
data.writeUInt8(now.getSeconds(), 2); | |
callback(data); | |
delayedNotification(callback); | |
} | |
}, notifyInterval * 1000); | |
} | |
CustomCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) { | |
console.log('CustomCharacteristic - onSubscribe'); | |
isSubscribed = true; | |
delayedNotification(updateValueCallback); | |
this._updateValueCallback = updateValueCallback; | |
}; | |
CustomCharacteristic.prototype.onUnsubscribe = function() { | |
console.log('CustomCharacteristic - onUnsubscribe'); | |
isSubscribed = false; | |
this._updateValueCallback = null; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* service.js | |
* A simple custom BLE peripheral service for use with Node.js and bleno. | |
* Julian Hays - 10/14/19 | |
*/ | |
var bleno = require('bleno-mac') ; | |
var BlenoPrimaryService = bleno.PrimaryService; | |
bleno.on('stateChange', function(state) { | |
console.log('on -> stateChange: ' + state); | |
if (state === 'poweredOn') { | |
console.log("request startAdvertising"); | |
bleno.startAdvertising('CustomService', ['27cf08c1-076a-41af-becd-02ed6f6109b9']); | |
} else { | |
console.log("request stopAdvertising"); | |
bleno.stopAdvertising(); | |
} | |
}); | |
var CustomCharacteristic = require('./characteristic'); | |
bleno.on('advertisingStart', function(error) { | |
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success')); | |
if (!error) { | |
bleno.setServices([ | |
new BlenoPrimaryService({ | |
uuid: '27cf08c1-076a-41af-becd-02ed6f6109b9', | |
characteristics: [ | |
new CustomCharacteristic() | |
] | |
}) | |
]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing the code and guide link.
Your code and step-by-step guide is very well explained.
How can we show multiple characteristics? Adding new is not working (see below code example)
characteristics: [ new CustomCharacteristic(), new NewCustomCharacteristic(), // Add new here ]
help me to add multiple services and/or characteristics.