diff --git a/changelog.md b/changelog.md index af8fa99..092ef3e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,11 @@ +# 0.4.2 + +### New Features +* Add callback function to constructor to catch noble errors. + +### Bug Fixes +* Fix #19 + # 0.4.1 ### New Features diff --git a/examples/ganglionServer/ganglionServer.js b/examples/ganglionServer/ganglionServer.js index e60afa0..b218cc0 100644 --- a/examples/ganglionServer/ganglionServer.js +++ b/examples/ganglionServer/ganglionServer.js @@ -1,10 +1,19 @@ const Ganglion = require('../../index').Ganglion; const k = require('../../openBCIConstants'); const verbose = true; -var ganglion = new Ganglion({ - // debug: true, +let ganglion = new Ganglion({ + // debug: true; , + nobleAutoStart: false, sendCounts: true, verbose: verbose +}, (error) => { + if (error) { + console.log(error); + } else { + if (verbose) { + console.log('Ganglion initialize completed'); + } + } }); function errorFunc (err) { @@ -101,6 +110,7 @@ function exitHandler (options, err) { ganglion.removeAllListeners('message'); ganglion.removeAllListeners('impedance'); ganglion.removeAllListeners('close'); + ganglion.removeAllListeners('error'); ganglion.removeAllListeners('ganglionFound'); ganglion.removeAllListeners('ready'); ganglion.destroyNoble(); diff --git a/openBCIGanglion.js b/openBCIGanglion.js index 8f9c797..fdd0490 100644 --- a/openBCIGanglion.js +++ b/openBCIGanglion.js @@ -1,7 +1,7 @@ 'use strict'; const EventEmitter = require('events').EventEmitter; const _ = require('lodash'); -const noble = require('noble'); +let noble; const util = require('util'); // Local imports const ganglionSample = require('./openBCIGanglionSample'); @@ -26,7 +26,7 @@ const _options = { /** * @description The initialization method to call first, before any other method. - * @param options (optional) - Board optional configurations. + * @param options {object} (optional) - Board optional configurations. * - `debug` {Boolean} - Print out a raw dump of bytes sent and received. (Default `false`) * * - `nobleAutoStart` {Boolean} - Automatically initialize `noble`. Subscribes to blue tooth state changes and such. @@ -59,13 +59,19 @@ const _options = { * setting and this sample rate will be used. (Default is `250`) * * - `verbose` {Boolean} - Print out useful debugging events. (Default `false`) - * + * @param callback {function} (optional) - A callback function used to determine if the noble module was able to be started. + * This can be very useful on Windows when there is no compatible BLE device found. * @constructor * @author AJ Keller (@pushtheworldllc) */ -function Ganglion (options) { +function Ganglion (options, callback) { if (!(this instanceof Ganglion)) { - return new Ganglion(options); + return new Ganglion(options, callback); + } + + if (options instanceof Function) { + callback = options; + options = {}; } options = (typeof options !== 'function') && options || {}; @@ -128,10 +134,17 @@ function Ganglion (options) { this.manualDisconnect = false; /** Initializations */ - if (this.options.nobleAutoStart) this._nobleInit(); // It get's the noble going for (var i = 0; i < 3; i++) { this._decompressedSamples[i] = [0, 0, 0, 0]; } + + try { + noble = require('noble'); + if (this.options.nobleAutoStart) this._nobleInit(); // It get's the noble going + if (callback) callback(); + } catch (e) { + if (callback) callback(e); + } } // This allows us to use the emitter class freely outside of the module @@ -594,8 +607,10 @@ Ganglion.prototype._disconnected = function () { * @private */ Ganglion.prototype._nobleDestroy = function () { - noble.removeAllListeners(k.OBCINobleEmitterStateChange); - noble.removeAllListeners(k.OBCINobleEmitterDiscover); + if (noble) { + noble.removeAllListeners(k.OBCINobleEmitterStateChange); + noble.removeAllListeners(k.OBCINobleEmitterDiscover); + } }; Ganglion.prototype._nobleConnect = function (peripheral) { diff --git a/package.json b/package.json index 3e73b95..ef2d43a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openbci-ganglion", - "version": "0.4.1", + "version": "0.4.2", "description": "The official Node.js SDK for the OpenBCI Ganglion Biosensor Board.", "main": "index.js", "scripts": { diff --git a/test/openBCIGanglion-test.js b/test/openBCIGanglion-test.js index 642232b..781a720 100644 --- a/test/openBCIGanglion-test.js +++ b/test/openBCIGanglion-test.js @@ -15,6 +15,21 @@ const clone = require('clone'); chai.use(chaiAsPromised); chai.use(sinonChai); +describe('#ganglion-constructor', function () { + it('should callback if only callback used', function (done) { + const cb = (err) => { + done(err); + }; + const ganglion_cb = new Ganglion(cb); + }); + it('should callback if options and callback', function (done) { + const cb = (err) => { + done(err); + }; + const ganglion_cb = new Ganglion({}, cb); + }); +}); + describe('#ganglion', function () { const mockProperties = { nobleAutoStart: false,