Last active
December 1, 2015 19:21
-
-
Save don/01b41297554378235519 to your computer and use it in GitHub Desktop.
Bleno with Eddystone beacon
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
var bleno = require('bleno'); | |
var util = require('util'); | |
var os = require('os'); | |
var eddystone = require('eddystone-beacon'); | |
var BlenoPrimaryService = bleno.PrimaryService; | |
var BlenoCharacteristic = bleno.Characteristic; | |
// Characteristic | |
var EchoCharacteristic = function() { | |
EchoCharacteristic.super_.call(this, { | |
uuid: 'ec0e', | |
properties: ['read', 'write', 'notify'], | |
value: null | |
}); | |
this._value = new Buffer(0); | |
this._updateValueCallback = null; | |
}; | |
util.inherits(EchoCharacteristic, BlenoCharacteristic); | |
EchoCharacteristic.prototype.onReadRequest = function(offset, callback) { | |
console.log('EchoCharacteristic - onReadRequest: value = ' + this._value.toString('hex')); | |
callback(this.RESULT_SUCCESS, this._value); | |
}; | |
EchoCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) { | |
this._value = data; | |
console.log('EchoCharacteristic - onWriteRequest: value = ' + this._value.toString('hex')); | |
if (this._updateValueCallback) { | |
console.log('EchoCharacteristic - onWriteRequest: notifying'); | |
this._updateValueCallback(this._value); | |
} | |
callback(this.RESULT_SUCCESS); | |
}; | |
EchoCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) { | |
console.log('EchoCharacteristic - onSubscribe'); | |
this._updateValueCallback = updateValueCallback; | |
}; | |
EchoCharacteristic.prototype.onUnsubscribe = function() { | |
console.log('EchoCharacteristic - onUnsubscribe'); | |
this._updateValueCallback = null; | |
// Bleno doesn't fire 'disconnect' on OS X | |
// Assume the client disconnected & start advertising beacon again | |
// on Linux, bleno.on('disconnect', callback) is be better (see below) | |
if (os.platform() === 'darwin') { | |
startBeacon(); | |
} | |
}; | |
// Sevice | |
console.log('bleno - echo'); | |
bleno.on('stateChange', function(state) { | |
console.log('on -> stateChange: ' + state); | |
if (state === 'poweredOn') { | |
bleno.startAdvertising('echo', ['ec00']); | |
startBeacon(); | |
} else { | |
bleno.stopAdvertising(); | |
} | |
}); | |
bleno.on('advertisingStart', function(error) { | |
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success')); | |
if (!error) { | |
bleno.setServices([ | |
new BlenoPrimaryService({ | |
uuid: 'ec00', | |
characteristics: [ | |
new EchoCharacteristic() | |
] | |
}) | |
]); | |
} | |
}); | |
// Beacon | |
function startBeacon() { | |
console.log("Starting beacon."); | |
eddystone.advertiseUrl('http://example.com/echo'); | |
} | |
bleno.on('accept', function() { | |
console.log("Accepting connection. Stopping beacon."); | |
eddystone.stop(); | |
}); | |
// disconnect is only fired on Linux | |
// see EchoCharacteristic.onUnsubscribe for the OS X hack | |
bleno.on('disconnect', function() { | |
console.log("Client disconnected ..."); | |
startBeacon(); | |
}) |
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
{ | |
"name": "bleno-with-beacon", | |
"version": "1.0.0", | |
"description": "Example with Bleno and Eddystone beacon", | |
"main": "main.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Don Coleman <don.coleman@gmail.com>", | |
"license": "ISC", | |
"dependencies": { | |
"bleno": "^0.3.3", | |
"eddystone-beacon": "^1.0.4", | |
"util": "^0.10.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment