This commit is contained in:
Marcus Westin 2019-12-15 17:42:57 +01:00
parent 9a84afada0
commit 8ed90c1668

View file

@ -1,4 +1,4 @@
const noble = require('@abandonware/noble'); const noble = require('@icanos/noble');
const crypto = require('crypto'); const crypto = require('crypto');
const xor = require('buffer-xor'); const xor = require('buffer-xor');
const _ = require('lodash'); const _ = require('lodash');
@ -57,7 +57,7 @@ class PlejdService extends EventEmitter {
ping: null ping: null
}; };
this._wireEvents(); this.wireEvents();
} }
scan() { scan() {
@ -81,6 +81,11 @@ class PlejdService extends EventEmitter {
} }
connect(uuid = null) { connect(uuid = null) {
if (this.state === STATE_CONNECTING) {
console.log('warning: currently connecting to a device, please wait...');
return;
}
if (!uuid) { if (!uuid) {
this.device = Object.values(this.devices)[0]; this.device = Object.values(this.devices)[0];
} }
@ -92,9 +97,21 @@ class PlejdService extends EventEmitter {
} }
} }
logger('connecting to ' + this.device.id + ' with addr ' + this.device.address + ' and rssi ' + this.device.rssi);
this.state = STATE_CONNECTING;
this.device.connect(this.onDeviceConnected); this.device.connect(this.onDeviceConnected);
} }
disconnect() {
logger('disconnect()');
if (this.state !== STATE_CONNECTED) {
return;
}
this.device.disconnect();
}
authenticate() { authenticate() {
logger('authenticate()'); logger('authenticate()');
const self = this; const self = this;
@ -337,3 +354,5 @@ class PlejdService extends EventEmitter {
return buffer return buffer
} }
} }
module.exports = PlejdService;