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