From de89205a49f53ba2eff820ef733c9a285df41ce8 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 9 Sep 2015 15:44:43 +0200 Subject: [PATCH 1/2] check platform via bleno in case were behind a websocket --- lib/beacon.js | 38 ++++++++++++++++++++++---------------- package.json | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/beacon.js b/lib/beacon.js index c32d933..aa07f36 100644 --- a/lib/beacon.js +++ b/lib/beacon.js @@ -23,28 +23,29 @@ function Beacon() { } Beacon.prototype.advertiseUid = function(namespaceId, instanceId, options) { + var self = this; this._parseOptions(options); this._advertisementData = AdvertisementData.makeUidBuffer(namespaceId, instanceId, this._txPowerLevel); - this._removeFlagsIfOsX(); - - this._mainAdvertisementData = this._advertisementData; - - this._advertiseWhenPoweredOn(); + this._removeFlagsIfOsX(function(){ + self._mainAdvertisementData = self._advertisementData; + self._advertiseWhenPoweredOn(); + }); }; Beacon.prototype.advertiseUrl = function(url, options) { + var self = this; this._parseOptions(options); this._advertisementData = AdvertisementData.makeUrlBuffer(url, this._txPowerLevel); - this._removeFlagsIfOsX(); - - this._mainAdvertisementData = this._advertisementData; - - this._advertiseWhenPoweredOn(); + this._removeFlagsIfOsX(function(){ + self._mainAdvertisementData = self._advertisementData; + self._advertiseWhenPoweredOn(); + }); }; Beacon.prototype.advertiseTlm = function() { + var self = this; this._advertisementData = AdvertisementData.makeTlmBuffer(this._batteryVoltage, this._temperature, this._advCnt, this._secCnt); if (this._tlmPeriod === 0) { @@ -52,8 +53,9 @@ Beacon.prototype.advertiseTlm = function() { this._tlmCount = 1; } - this._removeFlagsIfOsX(); - this._advertiseWhenPoweredOn(); + this._removeFlagsIfOsX(function(){ + self._advertiseWhenPoweredOn(); + }); }; Beacon.prototype.setBatteryVoltage = function(batteryVoltage) { @@ -124,10 +126,14 @@ Beacon.prototype._advertise = function() { } }; -Beacon.prototype._removeFlagsIfOsX = function() { - if (os.platform() === 'darwin') { - this._advertisementData = this._advertisementData.slice(3); - } +Beacon.prototype._removeFlagsIfOsX = function(callback) { + var self = this; + bleno.platform(function(error, platform){ + if (platform === 'darwin') { + self._advertisementData = self._advertisementData.slice(3); + callback(); + } + }); }; Beacon.prototype._tick = function() { diff --git a/package.json b/package.json index cfa0fae..d353a82 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ ], "license": "Apache-2.0", "dependencies": { - "bleno": "~0.1.13", + "bleno": "git+https://github.com/jacobrosenthal/bleno#websocket-platformcheck", "eddystone-url-encoding": "^1.0.0" }, "devDependencies": { From 6b99c40c2c1a27f6dc1e38e6a20cbd41c363388b Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Thu, 10 Sep 2015 21:43:04 +0200 Subject: [PATCH 2/2] dont strip flags, just dont add them in the first place --- lib/beacon.js | 30 ++++++++++-------------------- lib/util/advertisement-data.js | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/lib/beacon.js b/lib/beacon.js index aa07f36..0f7b7bd 100644 --- a/lib/beacon.js +++ b/lib/beacon.js @@ -26,8 +26,8 @@ Beacon.prototype.advertiseUid = function(namespaceId, instanceId, options) { var self = this; this._parseOptions(options); - this._advertisementData = AdvertisementData.makeUidBuffer(namespaceId, instanceId, this._txPowerLevel); - this._removeFlagsIfOsX(function(){ + bleno.platform(function(error, platform){ + self._advertisementData = AdvertisementData.makeUidBuffer(namespaceId, instanceId, self._txPowerLevel, platform); self._mainAdvertisementData = self._advertisementData; self._advertiseWhenPoweredOn(); }); @@ -37,8 +37,8 @@ Beacon.prototype.advertiseUrl = function(url, options) { var self = this; this._parseOptions(options); - this._advertisementData = AdvertisementData.makeUrlBuffer(url, this._txPowerLevel); - this._removeFlagsIfOsX(function(){ + bleno.platform(function(error, platform){ + self._advertisementData = AdvertisementData.makeUrlBuffer(url, self._txPowerLevel, platform); self._mainAdvertisementData = self._advertisementData; self._advertiseWhenPoweredOn(); }); @@ -46,14 +46,14 @@ Beacon.prototype.advertiseUrl = function(url, options) { Beacon.prototype.advertiseTlm = function() { var self = this; - this._advertisementData = AdvertisementData.makeTlmBuffer(this._batteryVoltage, this._temperature, this._advCnt, this._secCnt); - if (this._tlmPeriod === 0) { - this._tlmPeriod = 1; - this._tlmCount = 1; - } + bleno.platform(function(error, platform){ + self._advertisementData = AdvertisementData.makeTlmBuffer(self._batteryVoltage, self._temperature, self._advCnt, self._secCnt, platform); - this._removeFlagsIfOsX(function(){ + if (self._tlmPeriod === 0) { + self._tlmPeriod = 1; + self._tlmCount = 1; + } self._advertiseWhenPoweredOn(); }); }; @@ -126,16 +126,6 @@ Beacon.prototype._advertise = function() { } }; -Beacon.prototype._removeFlagsIfOsX = function(callback) { - var self = this; - bleno.platform(function(error, platform){ - if (platform === 'darwin') { - self._advertisementData = self._advertisementData.slice(3); - callback(); - } - }); -}; - Beacon.prototype._tick = function() { this._secCnt++; diff --git a/lib/util/advertisement-data.js b/lib/util/advertisement-data.js index f541224..eaa9794 100644 --- a/lib/util/advertisement-data.js +++ b/lib/util/advertisement-data.js @@ -11,7 +11,7 @@ var TLM_VERSION = 0x00; var MAX_URL_LENGTH = 18; -var makeUidBuffer = function (namespaceId, instanceId, txPowerLevel) { +var makeUidBuffer = function (namespaceId, instanceId, txPowerLevel, platform) { var namespaceIdData = hexStringIdToBuffer(namespaceId); var instanceUidData = hexStringIdToBuffer(instanceId); var rfu = new Buffer([0x00, 0x00]); @@ -32,10 +32,10 @@ var makeUidBuffer = function (namespaceId, instanceId, txPowerLevel) { rfu ]); - return makeEddystoneBuffer(UID_FRAME_TYPE, data); + return makeEddystoneBuffer(UID_FRAME_TYPE, data, platform); }; -var makeUrlBuffer = function (url, txPowerLevel) { +var makeUrlBuffer = function (url, txPowerLevel, platform) { var encodedUrl = encode(url); var txPowerLevelData = makeTxPowerLevelBuffer(txPowerLevel); @@ -49,10 +49,10 @@ var makeUrlBuffer = function (url, txPowerLevel) { encodedUrl ]); - return makeEddystoneBuffer(URL_FRAME_TYPE, data); + return makeEddystoneBuffer(URL_FRAME_TYPE, data, platform); }; -var makeTlmBuffer = function (vBatt, temp, advCnt, secCnt) { +var makeTlmBuffer = function (vBatt, temp, advCnt, secCnt, platform) { var tlmData = new Buffer(13); tlmData.writeUInt8(TLM_VERSION, 0); @@ -62,11 +62,11 @@ var makeTlmBuffer = function (vBatt, temp, advCnt, secCnt) { tlmData.writeUInt32BE(advCnt, 5); tlmData.writeUInt32BE(secCnt, 9); - return makeEddystoneBuffer(TLM_FRAME_TYPE, tlmData); + return makeEddystoneBuffer(TLM_FRAME_TYPE, tlmData, platform); }; -var makeEddystoneBuffer = function(flags, data) { +var makeEddystoneBuffer = function(flags, data, platform) { var header = new Buffer(1); header.writeUInt8(flags, 0); @@ -78,7 +78,9 @@ var makeEddystoneBuffer = function(flags, data) { var eir = new Eir(); - eir.addFlags(0x06); + if(platform !== 'darwin'){ + eir.addFlags(0x06); + } eir.add16BitCompleteServiceList([SERVICE_UUID]); eir.addServiceData(SERVICE_UUID, serviceData);