From 736149bffcbca9ff523fab46d7aa0de08d830c6f Mon Sep 17 00:00:00 2001 From: derwehr Date: Mon, 31 Oct 2022 19:39:57 +0100 Subject: [PATCH 1/3] add `writeValueWithoutResponse` and `writeValueWithResponse` methods --- src/GattCharacteristic.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/GattCharacteristic.js b/src/GattCharacteristic.js index ff52731..db0cd44 100644 --- a/src/GattCharacteristic.js +++ b/src/GattCharacteristic.js @@ -80,6 +80,26 @@ class GattCharacteristic extends EventEmitter { await this.helper.callMethod('WriteValue', data, callOptions) } + /** + * Write the value of the characteristic without waiting for the response. + * @param {Buffer} value - Buffer containing the characteristic value. + * @param {number} [offset = 0] - Starting offset. + * @returns {Promise} + */ + async writeValueWithoutResponse (value, offset = 0) { + return this.writeValue(value, { offset, type: 'command' }) + } + + /** + * Write the value of the characteristic and wait for the response. + * @param {Buffer} value - Buffer containing the characteristic value. + * @param {number} [offset = 0] - Starting offset. + * @returns {Promise} + */ + async writeValueWithResponse (value, offset = 0) { + return this.writeValue(value, { offset, type: 'request' }) + } + /** * Starts a notification session from this characteristic. * It emits valuechanged event when receives a notification. From 0ec18a3afa39ea7c6371a394b60e39ba8c32609a Mon Sep 17 00:00:00 2001 From: derwehr Date: Wed, 2 Nov 2022 09:47:10 +0100 Subject: [PATCH 2/3] add unit tests for `writeValueWithResponse` and `writeValueWithResponse` --- test/GattCharacteristic.spec.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/GattCharacteristic.spec.js b/test/GattCharacteristic.spec.js index 5751287..1304af0 100644 --- a/test/GattCharacteristic.spec.js +++ b/test/GattCharacteristic.spec.js @@ -41,6 +41,8 @@ test('read/write', async () => { } await expect(characteristic.writeValue('not_a_buffer')).rejects.toThrow('Only buffers can be wrote') + await expect(characteristic.writeValueWithResponse('not_a_buffer')).rejects.toThrow('Only buffers can be wrote') + await expect(characteristic.writeValueWithoutResponse('not_a_buffer')).rejects.toThrow('Only buffers can be wrote') await expect(characteristic.writeValue(Buffer.from('hello'), 5)).resolves.toBeUndefined() expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(5)) @@ -57,6 +59,12 @@ test('read/write', async () => { await expect(characteristic.writeValue(Buffer.from('hello'), 'incorrect argument')).resolves.toBeUndefined() expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions()) + await expect(characteristic.writeValueWithResponse(Buffer.from('hello'))).resolves.toBeUndefined() + expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(0, 'request')) + + await expect(characteristic.writeValueWithoutResponse(Buffer.from('hello'))).resolves.toBeUndefined() + expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(0, 'command')) + characteristic.helper.callMethod.mockResolvedValueOnce([255, 100, 0]) await expect(characteristic.readValue()).resolves.toEqual(Buffer.from([255, 100, 0])) }) From 14793860175fd53abd28aab0e17cfbf759e622e5 Mon Sep 17 00:00:00 2001 From: derwehr Date: Wed, 2 Nov 2022 09:47:24 +0100 Subject: [PATCH 3/3] update api.md --- docs/api.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/api.md b/docs/api.md index f7722dc..310d7eb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -354,6 +354,8 @@ GattCharacteristic class interacts with a GATT characteristic. * [.isNotifying()](#GattCharacteristic+isNotifying) ⇒ boolean * [.readValue([offset])](#GattCharacteristic+readValue) ⇒ Buffer * [.writeValue(value, [optionsOrOffset])](#GattCharacteristic+writeValue) + * [.writeValueWithoutResponse(value, [offset])](#GattCharacteristic+writeValueWithoutResponse) ⇒ Promise + * [.writeValueWithResponse(value, [offset])](#GattCharacteristic+writeValueWithResponse) ⇒ Promise * [.startNotifications()](#GattCharacteristic+startNotifications) * ["valuechanged"](#GattCharacteristic+event_valuechanged) @@ -400,6 +402,30 @@ Write the value of the characteristic. | [optionsOrOffset.offset] | number | 0 | Starting offset. | | [optionsOrOffset.type] | [WritingMode](#WritingMode) | reliable | Writing mode | + + +### gattCharacteristic.writeValueWithoutResponse(value, [offset]) ⇒ Promise +Write the value of the characteristic without waiting for the response. + +**Kind**: instance method of [GattCharacteristic](#GattCharacteristic) + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| value | Buffer | | Buffer containing the characteristic value. | +| [offset] | number | 0 | Starting offset. | + + + +### gattCharacteristic.writeValueWithResponse(value, [offset]) ⇒ Promise +Write the value of the characteristic and wait for the response. + +**Kind**: instance method of [GattCharacteristic](#GattCharacteristic) + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| value | Buffer | | Buffer containing the characteristic value. | +| [offset] | number | 0 | Starting offset. | + ### gattCharacteristic.startNotifications()