From 760f77728d5d32100e69cd06637e33d68e81b6f6 Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Thu, 1 Jun 2017 11:41:53 +0100 Subject: [PATCH 1/4] hacky first attempt: wait till all devices are found during scan, then stop scanning and start connecting to peripherals --- lib/adaptors/ble.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/adaptors/ble.js b/lib/adaptors/ble.js index b67d97e..09043a1 100644 --- a/lib/adaptors/ble.js +++ b/lib/adaptors/ble.js @@ -5,6 +5,8 @@ var util = require("util"), var ble; +var spheros = 0; + function initBLE() { var isChrome = typeof chrome !== "undefined"; @@ -85,8 +87,17 @@ Adaptor.prototype.open = function open(callback) { // connect to peripheral using noble ble.on("discover", function(peripheral) { if (peripheral.id === self.uuid) { - ble.stopScanning(); - self._connectPeripheral(peripheral, callback); + spheros--; + + var _flagFindAllSpheros = setInterval(function() { + if (spheros === 0) { + clearInterval(_flagFindAllSpheros); + + ble.stopScanning(); + self._connectPeripheral(peripheral, callback); + + } + }, 100); } }); From 97c7b9275efb87ed44d26243c3f9ac5b1cca74fd Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Thu, 1 Jun 2017 16:23:40 +0100 Subject: [PATCH 2/4] ... --- lib/adaptors/ble.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/adaptors/ble.js b/lib/adaptors/ble.js index 09043a1..5010adc 100644 --- a/lib/adaptors/ble.js +++ b/lib/adaptors/ble.js @@ -17,6 +17,7 @@ function initBLE() { console.error("Browser-based BLE interface is not yet supported."); } else { ble = require("noble"); + spheros++; } } catch (error) { ble = null; From e954c2e978dcf4f13bbef38a46e7d8259e0044b0 Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Thu, 1 Jun 2017 17:03:08 +0100 Subject: [PATCH 3/4] stop scanning only before connecting and restart later --- lib/adaptors/ble.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/adaptors/ble.js b/lib/adaptors/ble.js index 5010adc..db2ccab 100644 --- a/lib/adaptors/ble.js +++ b/lib/adaptors/ble.js @@ -5,7 +5,7 @@ var util = require("util"), var ble; -var spheros = 0; +var peripherals = 0; function initBLE() { var isChrome = typeof chrome !== "undefined"; @@ -17,7 +17,7 @@ function initBLE() { console.error("Browser-based BLE interface is not yet supported."); } else { ble = require("noble"); - spheros++; + peripherals++; } } catch (error) { ble = null; @@ -88,17 +88,17 @@ Adaptor.prototype.open = function open(callback) { // connect to peripheral using noble ble.on("discover", function(peripheral) { if (peripheral.id === self.uuid) { - spheros--; - - var _flagFindAllSpheros = setInterval(function() { - if (spheros === 0) { - clearInterval(_flagFindAllSpheros); - ble.stopScanning(); - self._connectPeripheral(peripheral, callback); + ble.stopScanning(); + self._connectPeripheral(peripheral, function(){ + + peripherals--; - } - }, 100); + if (peripherals > 0) { + ble.startScanning(); + } + callback(); + }); } }); From 44771431635e74a018aaf90c3abc2faec4266a82 Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Fri, 16 Jun 2017 17:20:17 +0100 Subject: [PATCH 4/4] leave the hacky counter, only start scanning after connection --- lib/adaptors/ble.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/adaptors/ble.js b/lib/adaptors/ble.js index db2ccab..a1aae53 100644 --- a/lib/adaptors/ble.js +++ b/lib/adaptors/ble.js @@ -5,8 +5,6 @@ var util = require("util"), var ble; -var peripherals = 0; - function initBLE() { var isChrome = typeof chrome !== "undefined"; @@ -17,7 +15,6 @@ function initBLE() { console.error("Browser-based BLE interface is not yet supported."); } else { ble = require("noble"); - peripherals++; } } catch (error) { ble = null; @@ -89,16 +86,11 @@ Adaptor.prototype.open = function open(callback) { ble.on("discover", function(peripheral) { if (peripheral.id === self.uuid) { - ble.stopScanning(); - self._connectPeripheral(peripheral, function(){ - - peripherals--; - - if (peripherals > 0) { - ble.startScanning(); - } - callback(); - }); + ble.stopScanning(); + self._connectPeripheral(peripheral, function() { + ble.startScanning(); + callback(); + }); } });