From b8329f80a5b2bacf6cfe12b5ab8d3295be7a9fea Mon Sep 17 00:00:00 2001 From: Taka Kojima Date: Fri, 27 Jan 2017 18:21:45 -0800 Subject: [PATCH 1/4] Add support for bleno.setDeviceName() --- lib/bleno.js | 4 ++++ lib/hci-socket/bindings.js | 5 +++++ lib/hci-socket/gatt.js | 7 ++++++- lib/mac/bindings.js | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/bleno.js b/lib/bleno.js index 0ec8b18e..4052863b 100644 --- a/lib/bleno.js +++ b/lib/bleno.js @@ -201,6 +201,10 @@ Bleno.prototype.onAdvertisingStop = function() { this.emit('advertisingStop'); }; +Bleno.prototype.setDeviceName = function(name) { + return this._bindings.setDeviceName(name); +}; + Bleno.prototype.setServices = function(services, callback) { if (callback) { this.once('servicesSet', callback); diff --git a/lib/hci-socket/bindings.js b/lib/hci-socket/bindings.js index 75c27908..f6c1ee0e 100644 --- a/lib/hci-socket/bindings.js +++ b/lib/hci-socket/bindings.js @@ -49,6 +49,11 @@ BlenoBindings.prototype.stopAdvertising = function() { this._gap.stopAdvertising(); }; + +BlenoBindings.prototype.setDeviceName = function(mtu) { + this._gatt.setDeviceName(mtu); +}; + BlenoBindings.prototype.setServices = function(services) { this._gatt.setServices(services); diff --git a/lib/hci-socket/gatt.js b/lib/hci-socket/gatt.js index 818e2176..d1c61960 100644 --- a/lib/hci-socket/gatt.js +++ b/lib/hci-socket/gatt.js @@ -67,6 +67,7 @@ var Gatt = function() { this.maxMtu = 256; this._mtu = 23; this._preparedWriteRequest = null; + this._deviceName = process.env.BLENO_DEVICE_NAME || os.hostname(); this.setServices([]); @@ -76,8 +77,12 @@ var Gatt = function() { util.inherits(Gatt, events.EventEmitter); +Gatt.prototype.setDeviceName = function(name) { + this._deviceName = name; +}; + Gatt.prototype.setServices = function(services) { - var deviceName = process.env.BLENO_DEVICE_NAME || os.hostname(); + var deviceName = this._deviceName; // base services and characteristics var allServices = [ diff --git a/lib/mac/bindings.js b/lib/mac/bindings.js index 4dbad676..f7fe04ba 100644 --- a/lib/mac/bindings.js +++ b/lib/mac/bindings.js @@ -157,6 +157,10 @@ blenoBindings.on('kCBMsgId17', function(args) { this.emit('advertisingStop'); }); +blenoBindings.setDeviceName = function (name) { + console.warn('bleno does not support setDeviceName() on macOS/OSX'); +}; + blenoBindings.setServices = function(services) { this.sendCBMsg(12, null); // remove all services From 25bb27e6104a07ead2d778ef72a4707e39c37023 Mon Sep 17 00:00:00 2001 From: Taka Kojima Date: Fri, 27 Jan 2017 18:22:29 -0800 Subject: [PATCH 2/4] Add support for bleno.setMaxMtu and BLENO_MAX_MTU env var --- lib/bleno.js | 4 ++++ lib/hci-socket/bindings.js | 3 +++ lib/hci-socket/gatt.js | 17 +++++++++++------ lib/mac/bindings.js | 4 ++++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/bleno.js b/lib/bleno.js index 4052863b..2647aae8 100644 --- a/lib/bleno.js +++ b/lib/bleno.js @@ -201,6 +201,10 @@ Bleno.prototype.onAdvertisingStop = function() { this.emit('advertisingStop'); }; +Bleno.prototype.setMaxMtu = function(mtu) { + return this._bindings.setMaxMtu(mtu); +}; + Bleno.prototype.setDeviceName = function(name) { return this._bindings.setDeviceName(name); }; diff --git a/lib/hci-socket/bindings.js b/lib/hci-socket/bindings.js index f6c1ee0e..8f0b7edb 100644 --- a/lib/hci-socket/bindings.js +++ b/lib/hci-socket/bindings.js @@ -49,6 +49,9 @@ BlenoBindings.prototype.stopAdvertising = function() { this._gap.stopAdvertising(); }; +BlenoBindings.prototype.setMaxMtu = function(mtu) { + this._gatt.setMaxMtu(mtu); +}; BlenoBindings.prototype.setDeviceName = function(mtu) { this._gatt.setDeviceName(mtu); diff --git a/lib/hci-socket/gatt.js b/lib/hci-socket/gatt.js index d1c61960..d631330f 100644 --- a/lib/hci-socket/gatt.js +++ b/lib/hci-socket/gatt.js @@ -64,8 +64,8 @@ var ATT_ECODE_INSUFF_RESOURCES = 0x11; var ATT_CID = 0x0004; var Gatt = function() { - this.maxMtu = 256; this._mtu = 23; + this._maxMtu = process.env.BLENO_MAX_MTU || 256; this._preparedWriteRequest = null; this._deviceName = process.env.BLENO_DEVICE_NAME || os.hostname(); @@ -77,6 +77,10 @@ var Gatt = function() { util.inherits(Gatt, events.EventEmitter); +Gatt.prototype.setMaxMtu = function(mtu) { + this._maxMtu = mtu; +}; + Gatt.prototype.setDeviceName = function(name) { this._deviceName = name; }; @@ -380,13 +384,14 @@ Gatt.prototype.handleMtuRequest = function(request) { if (mtu < 23) { mtu = 23; - } else if (mtu > this.maxMtu) { - mtu = this.maxMtu; + } else if (mtu > this._maxMtu) { + mtu = this._maxMtu; } - this._mtu = mtu; - - this.emit('mtuChange', this._mtu); + if (this._mtu !== mtu) { + this._mtu = mtu; + this.emit('mtuChange', this._mtu); + } var response = new Buffer(3); diff --git a/lib/mac/bindings.js b/lib/mac/bindings.js index f7fe04ba..9cbdb1e2 100644 --- a/lib/mac/bindings.js +++ b/lib/mac/bindings.js @@ -157,6 +157,10 @@ blenoBindings.on('kCBMsgId17', function(args) { this.emit('advertisingStop'); }); +blenoBindings.setMaxMtu = function(mtu) { + console.warn('bleno does not support setMaxMtu() on macOS/OSX'); +}; + blenoBindings.setDeviceName = function (name) { console.warn('bleno does not support setDeviceName() on macOS/OSX'); }; From f6cf2998d961b3e6b9a76704672ccd65ef576d4c Mon Sep 17 00:00:00 2001 From: Taka Kojima Date: Thu, 25 May 2017 19:13:55 -0700 Subject: [PATCH 3/4] unref setTimeout() so that it doesn't keep the process open --- lib/hci-socket/hci.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/hci-socket/hci.js b/lib/hci-socket/hci.js index 6a8a509b..10009e21 100644 --- a/lib/hci-socket/hci.js +++ b/lib/hci-socket/hci.js @@ -134,7 +134,7 @@ Hci.prototype.pollIsDevUp = function() { this._isDevUp = isDevUp; } - setTimeout(this.pollIsDevUp.bind(this), 1000); + setTimeout(this.pollIsDevUp.bind(this), 1000).unref(); }; Hci.prototype.initDev = function() { From d1e2a84fbd9ba162199a5a906c69a34e9906dea2 Mon Sep 17 00:00:00 2001 From: Michal Masek Date: Mon, 22 May 2017 15:53:42 +0200 Subject: [PATCH 4/4] If device is offline, reset socket when device is powered on --- lib/hci-socket/hci.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/hci-socket/hci.js b/lib/hci-socket/hci.js index 10009e21..d4b047d5 100644 --- a/lib/hci-socket/hci.js +++ b/lib/hci-socket/hci.js @@ -125,8 +125,15 @@ Hci.prototype.pollIsDevUp = function() { if (this._isDevUp !== isDevUp) { if (isDevUp) { + if (this._isDevUp === false) { + this._isDevUp = isDevUp + this._socket.stop() + this._socket = new BluetoothHciSocket(); + this.init() + } this.setSocketFilter(); this.initDev(); + this.emit('stateChange', 'poweredOn'); } else { this.emit('stateChange', 'poweredOff'); }