From 062feb3134b5c02587c9fddcd208e2f23e2037a7 Mon Sep 17 00:00:00 2001 From: seizu Date: Fri, 13 Sep 2024 13:29:58 +0200 Subject: [PATCH] GattServer.getUUIDbyHandle added --- src/GattServer.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/GattServer.js b/src/GattServer.js index 47dc160..ee30af3 100644 --- a/src/GattServer.js +++ b/src/GattServer.js @@ -54,6 +54,38 @@ class GattServer { throw new Error('Service not available') } + + /** + * Find the service UUID and characteristic UUID based on a characteristic handle + * @param {number} charHandle + * @returns {} + */ + async getUUIDbyHandle(charHandle) { + const handle = charHandle - 1; + const handleStr = 'char' + handle.toString(16).padStart(4, '0'); + + for (const key in this.dbus._matchRules) { + if (key.includes(handleStr)) { + const match = key.match(/service[0-9a-fA-F]+/); + if (match) { + const service = match[0]; + const services = this._services; + for (const skey in services) { + if (services[skey].service === service) { + const characteristics = services[skey]._characteristics; + for (const ckey in characteristics) { + if (characteristics[ckey].characteristic === handleStr) { + return { 'service': skey, 'char': ckey }; + } + } + return null; + } + } + } + } + } + return null; + } } module.exports = GattServer