/** * * User: Martin Donnelly * Date: 2016-07-06 * Time: 13:37 * */ var spawn = require('child_process').spawn; var EventEmitter = require('events'); var busEmitter = new EventEmitter(); var util = require('util'); var logger = require('log4js').getLogger(); var bluetest = module.exports = function(options) { 'use strict'; var core = {}; core.connectGatt = function() { logger.info('trying to connect using gatttool'); var gatt = spawn('gatttool -I'); gatt.on('exit', function(code) { logger.debug('gatt exit code', code); }); }; core.init = function(options) { var _mac; var tool_path = ''; var hcidev = 'hvi0'; //If () if (typeof options.mac !== 'undefined') { _mac = options.mac; } else { console.log('You need to pass a mac address.'); process.exit(1); } logger.info('Working with:' , options); var hciconfig = spawn(tool_path + 'hciconfig', [hcidev, 'up']); hciconfig.on('exit', function(code) { logger.debug('code',code); if (code !== 0) { // Could not get the device UP, maybe due to permissions, should run with sudo. busEmitter.emit('error','hciconfig: failed to bring up device ' + hcidev + '. Try running with sudo.'); return; } else { core.connectGatt(); } }); }; core.init(options); }; util.inherits(bluetest, EventEmitter);