diff --git a/src/Device.js b/src/Device.js index 23154c6..36060cb 100644 --- a/src/Device.js +++ b/src/Device.js @@ -81,6 +81,30 @@ class Device extends EventEmitter { return this.helper.prop('Connected') } + /** + * Service advertisement data. Keys are the UUIDs in string format followed by its byte array value. + * @returns {object} + */ + async getServiceAdvertisementData () { + return this.helper.prop('ServiceData') + } + + /** + * Manufacturer specific advertisement data. Keys are 16 bits Manufacturer ID followed by its byte array value. + * @returns {object} + */ + async getManufacturerAdvertisementData () { + return this.helper.prop('ManufacturerData') + } + + /** + * List of 128-bit UUIDs that represents the available remote services. + * @returns {string[]} + */ + async getServiceUUIDs () { + return this.helper.prop('UUIDs') + } + /** * This method will connect to the remote device */ @@ -103,9 +127,10 @@ class Device extends EventEmitter { if ('Connected' in propertiesChanged) { const { value } = propertiesChanged.Connected if (value) { - this.emit('connect', { connected: true }) + this.emit('connect', { connected: true, deviceUuid: this.device }) } else { - this.emit('disconnect', { connected: false }) + this.emit('disconnect', { connected: false, deviceUuid: this.device }) + this.helper.removeAllListeners('PropertiesChanged') // might be improved } } } @@ -119,7 +144,6 @@ class Device extends EventEmitter { */ async disconnect () { await this.helper.callMethod('Disconnect') - this.helper.removeListeners() } /** @@ -150,6 +174,7 @@ class Device extends EventEmitter { * @event Device#connect * @type {object} * @property {boolean} connected - Indicates current connection status. + * @property {string} deviceUuid - The UUID of the device freshly connected */ /** @@ -158,6 +183,7 @@ class Device extends EventEmitter { * @event Device#disconnect * @type {object} * @property {boolean} connected - Indicates current connection status. + * @property {string} deviceUuid - The UUID of the device freshly disconnected */ module.exports = Device diff --git a/src/GattCharacteristic.js b/src/GattCharacteristic.js index db0cd44..ed49ad6 100644 --- a/src/GattCharacteristic.js +++ b/src/GattCharacteristic.js @@ -105,8 +105,6 @@ class GattCharacteristic extends EventEmitter { * It emits valuechanged event when receives a notification. */ async startNotifications () { - await this.helper.callMethod('StartNotify') - const cb = (propertiesChanged) => { if ('Value' in propertiesChanged) { const { value } = propertiesChanged.Value @@ -115,6 +113,10 @@ class GattCharacteristic extends EventEmitter { } this.helper.on('PropertiesChanged', cb) + + // Call StartNotify after the listener is attached in order not to lose + // the first notifications of the device. + await this.helper.callMethod('StartNotify') } async stopNotifications () {