From ae2a3aa6c57eb341736ac6f927cf536da6a6a8c7 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Wed, 6 Jul 2016 16:15:40 +0100 Subject: [PATCH] continuing --- lib/index.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 9d9ff6b..1101c66 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,7 @@ var cp = require('child_process'); var spawn = cp.spawn; var fork = cp.fork; +var spawnP = require('child-process-promise').spawn; var EventEmitter = require('events'); var busEmitter = new EventEmitter(); @@ -32,6 +33,51 @@ var bluetest = module.exports = function(options) { core.gatt.stdin.write('primary\n'); }; + core.connectGattP = function() { + core.gatt = spawn('gatttool',['-I']); + var child = core.gatt.childProcess; + + child.on('error', function(err) { + logger.error('child',err); + }); + child.on('exit', function(code) { + + logger.debug('child','gatt exit code', code); + }); + + child.on('message', function(m) { + logger.info('child',m); + }); + + child.stdout.on('data', (data) => { + + logger.info('child',`stdout: ${data}`); + + if (data.indexOf('Connection successful') > -1) { + + busEmitter.emit('child','connected'); + } + }); + + child.stderr.on('data', (data) => { + logger.info('child',`stderr: ${data}`); + }); + + child.on('close', (code) => { + logger.warn('child',`child process exited with code ${code}`); + }); + + + core.gatt.then(function () { + console.log('[spawn] done!'); + }) + .catch(function (err) { + console.error('[spawn] ERROR: ', err); + }); + + + }; + core.connectGatt = function() { logger.info('trying to connect using gatttool'); core.gatt = spawn('gatttool',['-I']); @@ -105,7 +151,7 @@ var bluetest = module.exports = function(options) { } else { - core.connectGatt(); + core.connectGattP(); }