From f1c6c6300df57dc0fde7877820c4302a6c16f55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Wed, 21 Feb 2024 16:44:00 +0100 Subject: [PATCH 1/2] expose 3 additional device properties + fix missing disconnect event --- src/Device.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) 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 From a2628830e395c15820a9d5464fb52ac5af4d3b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Wed, 21 Feb 2024 22:05:39 +0100 Subject: [PATCH 2/2] fix race condition when starting notifications: the first device initiated notifications were lost --- src/GattCharacteristic.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 () {