From b9d7355b22bbe31fc57c6b3c34e23692d7ed06ac Mon Sep 17 00:00:00 2001 From: Joshua May Date: Thu, 4 Oct 2018 00:28:27 +0200 Subject: [PATCH] Allow bindings to be injected to Bleno instance --- index.js | 3 ++- lib/bleno.js | 15 +-------------- lib/resolve-bindings.js | 13 +++++++++++++ 3 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 lib/resolve-bindings.js diff --git a/index.js b/index.js index 0a299ad8..49b23cd7 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ var Bleno = require('./lib/bleno'); +var bindings = require('./lib/resolve-bindings')(); -module.exports = new Bleno(); +module.exports = new Bleno(bindings); diff --git a/lib/bleno.js b/lib/bleno.js index 0ec8b18e..3b145d92 100644 --- a/lib/bleno.js +++ b/lib/bleno.js @@ -1,7 +1,6 @@ var debug = require('debug')('bleno'); var events = require('events'); -var os = require('os'); var util = require('util'); var UuidUtil = require('./uuid-util'); @@ -10,19 +9,7 @@ var PrimaryService = require('./primary-service'); var Characteristic = require('./characteristic'); var Descriptor = require('./descriptor'); -var bindings = null; - -var platform = os.platform(); - -if (platform === 'darwin') { - bindings = require('./mac/bindings'); -} else if (platform === 'linux' || platform === 'freebsd' || platform === 'win32' || platform === 'android') { - bindings = require('./hci-socket/bindings'); -} else { - throw new Error('Unsupported platform'); -} - -function Bleno() { +function Bleno(bindings) { this.initialized = false; this.platform = 'unknown'; this.state = 'unknown'; diff --git a/lib/resolve-bindings.js b/lib/resolve-bindings.js new file mode 100644 index 00000000..db47164f --- /dev/null +++ b/lib/resolve-bindings.js @@ -0,0 +1,13 @@ +var os = require('os'); + +module.exports = function() { + var platform = os.platform(); + + if (platform === 'darwin') { + return require('./mac/bindings'); + } else if (platform === 'linux' || platform === 'freebsd' || platform === 'win32' || platform === 'android') { + return require('./hci-socket/bindings'); + } else { + throw new Error('Unsupported platform'); + } +}; \ No newline at end of file