Skip to content

Commit

Permalink
Allow Fatbeacon to serve large files
Browse files Browse the repository at this point in the history
This change allows the Fatbeacon to serve large files, as opposed to
one message that is less than the mtu size. The central will call
the same characteristic until it receives a packet size smaller
than the mtu. The peripheral will slice the message and serve the
next slice when the central askes for it.
  • Loading branch information
iankchristie committed Jan 20, 2017
1 parent cb852f9 commit 45b3892
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
9 changes: 7 additions & 2 deletions nodejs/FatBeaconPeripheral/FatBeacon.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var bleno = require('bleno');
var eddystoneBeacon = require('eddystone-beacon');
var webpageCharacteristic = require('./webpageCharacteristic');
var fs = require('fs');

var SERVICE_UUID = 'ae5946d4-e587-4ba8-b6a5-a97cca6affd3';

Expand Down Expand Up @@ -54,8 +55,12 @@ AdvertisementData.makeUrlBuffer = function(name) {
/*********************************************************/

var characteristic = new webpageCharacteristic();
var data = new Buffer.from('Hello World of FatBeacon!');
characteristic.onWriteRequest(data, 0, null, null);

fs.readFile("./html/fatBeaconDefault.html", function(err, data) {
if(err) throw err;
characteristic.onWriteRequest(data, 0, null, null);
console.log(service);
});

var service = new bleno.PrimaryService({
uuid: SERVICE_UUID,
Expand Down
60 changes: 60 additions & 0 deletions nodejs/FatBeaconPeripheral/html/fatBeaconDefault.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="shortcut icon" type="image/x-icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAABC1BMVEX///8+gsM/gsM/gsM/gsQ/gsM/gsM/gsM+gcM/gsM/gsM/gsM/gsM+gsM/gsM/gsM/gsM/gsM9gMI/gsM/gsM/gsM+gcM/gsM/gsM/gsM/gsM/gsM/gsM+gcM/gsM9gcI5f8I/gsM/gsM/gsU8f8E8gMI/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM/gsM+gsM/gcM/gsM/gsM/gsM+gcM/gsM/gsM/gsM+gsM+gsM/gsM/gsM9gMM9gME/gsM9gMI8gcM+gcM/gsM+gMI+gMM/gsM9gcI/gcM+gcM9gMI/gsM+gsM8f8M8gME7gME9gcJAg8T///8dQjXcAAAAV3RSTlMBF4XX/f3VgxdL7bVTI0lB81kLZ6FbCeFpH9nNfx/fBwPJlwMDA6c96eu/XWVfvblrx7OjF/c1F6Ut9R11qzkfLZ3BCQc/BwsLkQ0NjwshIwWpBQUFBQXrCtlNAAAAAWJLR0QAiAUdSAAAAAd0SU1FB+AIEREoJpDxKAkAAADMSURBVBjTY2AAAkYmZhZWNnYOBijg5OLm4eXl4ebig/D5BQSFhEVEhIVEBfhBfDFxCUkpaRkZaWZZCTl5oIAom4KikjIDg4qSoiqbKFBAjV1dQwysVFOdXQRIa2kLc0MM49bR1QNS+gaChhABRUEDIyDFZGxiagbim5maWzABaUsraxtbBjs7BlsbeysHkIyjk7OLq4CAq5u7kyNYq4enl7pAeLiAq7ePL9TtfkA+UMQf5heGADWQQGAQXIAhODA83DaEAQmEqgWGQVgAlbAZM4elADkAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTYtMDgtMTdUMTc6NDA6MzgtMDQ6MDDPiIKeAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE2LTA4LTE3VDE3OjQwOjM4LTA0OjAwvtU6IgAAAABJRU5ErkJggg==">
<title>FatBeacon demo</title>
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
color: #444;
background: #f5f5f5;
}
.header {
display: flex;
background: #3F82C4;
height: 100px;
}
.card {
background: #fff;
padding: 20px;
margin: 30px 30px 0px 30px;
border: 1px solid #ccc;
box-shadow: 0px 0px 5px #aaa;
}
p {
font-size: 15px;
}
a {
color: #3F82C4;
}
h1 {
margin: 35px 0px 0px 10px;
display: inline-block;
font-size: 18px;
color: #fff;
}
svg {
margin-left: 30px;
display: inline-block;
width: 50px;
}
</style>
</head>
<body>
<div class="header">
<svg viewBox="0 0 171 202"><g fill="#fff"><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>
<h1>Welcome to FatBeacon!</h1>
</div>
<div class="card">
<p>A FatBeacon is an experimental beacon type for offline web pages. A FatBeacon does two things:</p>
<ul>
<li>It advertises the name of the device</li>
<li>Runs a <a href="https://developer.bluetooth.org/TechnologyOverview/pages/gatt.aspx">GATT</a> service that offers up a simple web page.</li>
</ul>
<p>When you select a FatBeacon from the list of nearby beacons, the app connects to it and downloads to page to your phone before displaying it. This means you can interact with a FatBeacon with no data connectivity at all.</p>
</div>
</body>
</html>
15 changes: 13 additions & 2 deletions nodejs/FatBeaconPeripheral/webpageCharacteristic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ var WebpageCharacteristic = function() {

this._value = Buffer.alloc(0);
this._updateValueCallback = null;

this._mtuSize = 251; // 5 less than the specified mtu
this._start = 0;
this._end = this._mtuSize;
};

util.inherits(WebpageCharacteristic, bleno.Characteristic);

WebpageCharacteristic.prototype.onReadRequest = function(offset, callback) {
console.log(`Reading - ${this._value}`);
callback(this.RESULT_SUCCESS, this._value);
var tempBuffer = this._value.slice(this._start, this._end);
callback(this.RESULT_SUCCESS, tempBuffer);

this._start = this._end;
this._end = this._end + this._mtuSize;
if(this._start >= this._value.length) {
this._start = 0;
this._end = this._mtuSize;
}
};

WebpageCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
Expand Down

0 comments on commit 45b3892

Please sign in to comment.