Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Fat Beacon support and an example #42

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions examples/fatBeacon/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Fat Beacon Demo</title>
<meta charset='UTF-8'>
<meta name='description' content='Fat Beacon Demo'/>
</head>
<body>
<h1>HelloWorld</h1>
<svg xmlns="http://www.w3.org/2000/svg" width="171" height="202"><g fill="#3F82C4" fill-rule="evenodd"><path d="M141.2 85.3c0-31-25-56-56-56-30.7 0-55.8 25-55.8 56 0 17 7.8 32.5 20 42.8l10-10c-9.8-7.6-16-19.4-16-32.7 0-23.2 18.8-42 42-42 23 0 41.8 18.8 41.8 42 0 13.3-6.2 25-16 32.8l10 10c12.2-10.2 20-25.6 20-42.7"/><path d="M14 85.3C14 46 46 14 85.3 14s71.3 32 71.3 71.3c0 21.4-9.5 40.6-24.5 53.7l10 10c17.6-15.7 28.6-38.4 28.6-63.7 0-47-38.2-85.3-85.3-85.3C38.3 0 0 38.2 0 85.3c0 25.3 11 48 28.5 63.6l10-10C23.5 126 14 106.7 14 85.3"/><path d="M89.2 200.3c-2 2-5.5 2-7.6 0l-35.8-35.8c-2-2-2-5.5 0-7.6l35.8-36c2-2 5.5-2 7.6 0l35.8 36c2 2 2 5.4 0 7.5l-35.8 35.8z"/></g></svg>
</body>
</html>
12 changes: 12 additions & 0 deletions examples/fatBeacon/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Simplest way to create a Eddystone-FatBeacon

// might want to include something like this https://www.npmjs.com/package/html-minify

var eddystoneBeacon = require('./../../index');
var fs = require('fs');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding this in a separate file and using the fs module to read the contents?

fs.readFile('index.html', function(err, data){
if (!err) {
eddystoneBeacon.advertiseFatBeacon('Fat Beacon Demo', {html: data});
}
});
60 changes: 60 additions & 0 deletions lib/HTMLCharacteristic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var bleno = require('bleno');
var util = require('util');

function HTMLCharacteristic(html) {
this._currentMTU = 0;
this._queueOffset = 0;
this._html = html;
this._buffer = new Buffer(this._html, 'utf8');
bleno.Characteristic.call(this, {
uuid: 'd1a517f0249946ca9ccc809bc1c966fa',
properties: ['read'],
descriptors: [
new bleno.Descriptor({
uuid: '2901',
value: 'HTML'
})
]
});

bleno.on('mtuChange', function(mtu) {
this._currentMTU = mtu;
}.bind(this));

bleno.on('disconnect', function(){
this._queueOffset = 0;
}.bind(this));
}

util.inherits(HTMLCharacteristic,bleno.Characteristic);

HTMLCharacteristic.prototype.onReadRequest = function(offset, callback) {
if (this._queueOffset < this._buffer.length) {
var transfer = this._currentMTU - 5;
var end = this._queueOffset + transfer >= this._buffer.length ? this._buffer.length: this._queueOffset + transfer;
var slice = this._buffer.slice(this._queueOffset, end);
callback(this.RESULT_SUCCESS, slice);
this._queueOffset = end;
} else if (this._queueOffset === this._buffer.length) {
callback(this.RESULT_SUCCESS, new Buffer());
this._queueOffset++;
}

}

module.exports = HTMLCharacteristic;
42 changes: 42 additions & 0 deletions lib/beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var bleno = require('bleno');

var AdvertisementData = require('./util/advertisement-data');

var HTMLCharacteristic = require('./HTMLCharacteristic');

var TICK_INTERVAL = 100; // ms
var DEFAULT_TX_POWER_LEVEL = -21; // dBm

Expand All @@ -19,6 +21,7 @@ function Beacon() {
this._temperature = -128;
this._secCnt = 0;
this._advCnt = 0;
this._html = null;

setInterval(this._tick.bind(this), TICK_INTERVAL);
}
Expand All @@ -45,6 +48,38 @@ Beacon.prototype.advertiseUrl = function(url, options) {
this._advertiseWhenPoweredOn();
};

Beacon.prototype.advertiseFatBeacon = function(title, options) {

this._parseOptions(options);

var htmlCharacteristic = new HTMLCharacteristic(this._html);
var fatBeaconService = new bleno.PrimaryService({
uuid: 'ae5946d4e5874ba8b6a5a97cca6affd3',
characteristics: [htmlCharacteristic]
});

var services = [fatBeaconService];

if (options.services) {
services = services.concat(options.services);
}

bleno.once('advertisingStart', function(err) {
if (err) {
throw err;
}

bleno.setServices(services);
});

this._advertisementData = AdvertisementData.makeFatBeaconBuffer(title, this._txPowerLevel);
this._removeFlagsIfOsX();

this._mainAdvertisementData = this._advertisementData;

this._advertiseWhenPoweredOn();
};

Beacon.prototype.advertiseTlm = function() {
this._advertisementData = AdvertisementData.makeTlmBuffer(this._batteryVoltage, this._temperature, this._advCnt, this._secCnt);

Expand Down Expand Up @@ -76,6 +111,13 @@ Beacon.prototype._parseOptions = function(options) {
this._parseTxPowerLevelOption(options.txPowerLevel);
this._parseTlmOptions(options);
this._parseNameOption(options.name);
this._parseFatBeaconOption(options.html);
};

Beacon.prototype._parseFatBeaconOption = function(html) {
if (html) {
this._html = html;
}
};

Beacon.prototype._parseNameOption = function(name) {
Expand Down
16 changes: 15 additions & 1 deletion lib/util/advertisement-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ var makeUrlBuffer = function (url, txPowerLevel) {
return makeEddystoneBuffer(URL_FRAME_TYPE, data);
};

var makeFatBeaconBuffer = function (title, txPowerLevel) {
var txPowerLevelData = makeTxPowerLevelBuffer(txPowerLevel);

var data = Buffer.concat([
txPowerLevelData,
new Buffer([0x0E]),
new Buffer(title)
]);

return makeEddystoneBuffer(URL_FRAME_TYPE, data);
};


var makeTlmBuffer = function (vBatt, temp, advCnt, secCnt) {
var tlmData = new Buffer(13);

Expand Down Expand Up @@ -99,5 +112,6 @@ var hexStringIdToBuffer = function(hexStringId) {
module.exports = {
makeUidBuffer: makeUidBuffer,
makeUrlBuffer: makeUrlBuffer,
makeTlmBuffer: makeTlmBuffer
makeTlmBuffer: makeTlmBuffer,
makeFatBeaconBuffer: makeFatBeaconBuffer
};