Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 94cb9a8

Browse files
authored
Merge pull request #885 from iankchristie/GATTStart
Gatt start
2 parents 64ded73 + cb852f9 commit 94cb9a8

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

nodejs/FatBeaconPeripheral/FatBeacon.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
var bleno = require('bleno');
22
var eddystoneBeacon = require('eddystone-beacon');
3+
var webpageCharacteristic = require('./webpageCharacteristic');
4+
5+
var SERVICE_UUID = 'ae5946d4-e587-4ba8-b6a5-a97cca6affd3';
36

47
/***********************Altering Library Functions**************************/
58
/**
69
* In order to broadcast in the fatbeacon format, we override several
710
* functions in the advertisement-data module of the eddystone-beacon library.
811
*/
9-
var AdvertisementData =
12+
var AdvertisementData =
1013
require('eddystone-beacon/lib/util/advertisement-data');
1114
var Eir = require('eddystone-beacon/lib/util/eir');
1215

1316
var FAT_BEACON_FRAME_TYPE = 0x0e;
1417
var MAX_URL_LENGTH = 18;
15-
var SERVICE_UUID = 'feaa';
18+
var ADVERTISING_HEADER_UUID = 'feaa';
1619

1720
/**
1821
* this patch ensures that the correct Fatbeacon eir flag (0x06) is added
@@ -21,8 +24,8 @@ var SERVICE_UUID = 'feaa';
2124
AdvertisementData.makeEirData = function(serviceData) {
2225
var eir = new Eir();
2326
eir.addFlags(0x06);
24-
eir.add16BitCompleteServiceList([SERVICE_UUID]);
25-
eir.addServiceData(SERVICE_UUID, serviceData);
27+
eir.add16BitCompleteServiceList([ADVERTISING_HEADER_UUID]);
28+
eir.addServiceData(ADVERTISING_HEADER_UUID, serviceData);
2629
return eir.buffer();
2730
}
2831

@@ -33,7 +36,7 @@ AdvertisementData.makeEirData = function(serviceData) {
3336
*/
3437
AdvertisementData.makeUrlBuffer = function(name) {
3538
console.log(`AdvertisementData.makeUrlBuffer called with: ${name}`);
36-
39+
3740
var encodedName = Buffer.from(name);
3841
if (encodedName.length > MAX_URL_LENGTH) {
3942
throw new Error(`Encoded Name must be less than ${MAX_URL_LENGTH} bytes.` +
@@ -50,5 +53,28 @@ AdvertisementData.makeUrlBuffer = function(name) {
5053

5154
/*********************************************************/
5255

56+
var characteristic = new webpageCharacteristic();
57+
var data = new Buffer.from('Hello World of FatBeacon!');
58+
characteristic.onWriteRequest(data, 0, null, null);
59+
60+
var service = new bleno.PrimaryService({
61+
uuid: SERVICE_UUID,
62+
characteristics: [
63+
characteristic
64+
]
65+
});
66+
67+
bleno.once('advertisingStart', function(err) {
68+
69+
if(err) {
70+
throw err;
71+
}
72+
73+
console.log('on - advertisingStart');
74+
bleno.setServices([
75+
service
76+
]);
77+
});
78+
5379
// Start Advertising name.
5480
eddystoneBeacon.advertiseUrl('New Fatbeacon');
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var util = require('util');
2+
var bleno = require('bleno');
3+
var CHARACTERISTIC_UUID = 'd1a517f0-2499-46ca-9ccc-809bc1c966fa';
4+
5+
var WebpageCharacteristic = function() {
6+
WebpageCharacteristic.super_.call(this, {
7+
uuid: CHARACTERISTIC_UUID,
8+
properties: ['read'],
9+
value: null
10+
});
11+
12+
this._value = Buffer.alloc(0);
13+
this._updateValueCallback = null;
14+
};
15+
16+
util.inherits(WebpageCharacteristic, bleno.Characteristic);
17+
18+
WebpageCharacteristic.prototype.onReadRequest = function(offset, callback) {
19+
console.log(`Reading - ${this._value}`);
20+
callback(this.RESULT_SUCCESS, this._value);
21+
};
22+
23+
WebpageCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
24+
this._value = data;
25+
26+
console.log(`Writing - ${this._value}`);
27+
28+
if (this._updateValueCallback) {
29+
console.log('WebpageCharacteristic - onWriteRequest: notifying');
30+
31+
this._updateValueCallback(this._value);
32+
}
33+
34+
if(callback) {
35+
callback(this.RESULT_SUCCESS);
36+
}
37+
};
38+
39+
WebpageCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) {
40+
console.log('WebpageCharacteristic - onSubscribe');
41+
this._updateValueCallback = updateValueCallback;
42+
};
43+
44+
WebpageCharacteristic.prototype.onUnsubscribe = function() {
45+
console.log('WebpageCharacteristic - onUnsubscribe');
46+
this._updateValueCallback = null;
47+
};
48+
49+
module.exports = WebpageCharacteristic;

0 commit comments

Comments
 (0)