diff --git a/plejd/ble.js b/plejd/ble.js index 34701ed..7845910 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -1,4 +1,4 @@ -const noble = require('@abandonware/noble'); +const noble = require('@icanos/noble'); const crypto = require('crypto'); const xor = require('buffer-xor'); const _ = require('lodash'); @@ -57,7 +57,7 @@ class PlejdService extends EventEmitter { ping: null }; - this._wireEvents(); + this.wireEvents(); } scan() { @@ -81,6 +81,11 @@ class PlejdService extends EventEmitter { } connect(uuid = null) { + if (this.state === STATE_CONNECTING) { + console.log('warning: currently connecting to a device, please wait...'); + return; + } + if (!uuid) { 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); } + disconnect() { + logger('disconnect()'); + if (this.state !== STATE_CONNECTED) { + return; + } + + this.device.disconnect(); + } + authenticate() { logger('authenticate()'); const self = this; @@ -336,4 +353,6 @@ class PlejdService extends EventEmitter { return buffer } -} \ No newline at end of file +} + +module.exports = PlejdService; \ No newline at end of file