bluetest/lib/index.js
Martin Donnelly eb7e1e02f1 continuing
2016-07-06 14:23:38 +01:00

87 lines
1.5 KiB
JavaScript

/**
*
* 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 = {
mac:''
};
core.connectGatt = function() {
logger.info('trying to connect using gatttool');
var gatt = spawn('gatttool',['-I']);
gatt.on('error', function(err) {
logger.error(err);
});
gatt.on('exit', function(code) {
logger.debug('gatt exit code', code);
});
logger.info('Trying to connect to ', core.mac);
//gatt.send();
};
core.init = function(options) {
var tool_path = '';
var hcidev = 'hvi0';
//If ()
if (typeof options.mac !== 'undefined') {
core.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);