Skip to content

Commit

Permalink
add writeValueWithoutResponse and writeValueWithResponse methods (#…
Browse files Browse the repository at this point in the history
…47)

* add `writeValueWithoutResponse` and `writeValueWithResponse` methods

* add unit tests for `writeValueWithResponse` and `writeValueWithResponse`

* update api.md
  • Loading branch information
derwehr authored Nov 12, 2022
1 parent 78ebe01 commit e41aaf5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ GattCharacteristic class interacts with a GATT characteristic.
* [.isNotifying()](#GattCharacteristic+isNotifying) ⇒ <code>boolean</code>
* [.readValue([offset])](#GattCharacteristic+readValue) ⇒ <code>Buffer</code>
* [.writeValue(value, [optionsOrOffset])](#GattCharacteristic+writeValue)
* [.writeValueWithoutResponse(value, [offset])](#GattCharacteristic+writeValueWithoutResponse) ⇒ <code>Promise</code>
* [.writeValueWithResponse(value, [offset])](#GattCharacteristic+writeValueWithResponse) ⇒ <code>Promise</code>
* [.startNotifications()](#GattCharacteristic+startNotifications)
* ["valuechanged"](#GattCharacteristic+event_valuechanged)

Expand Down Expand Up @@ -400,6 +402,30 @@ Write the value of the characteristic.
| [optionsOrOffset.offset] | <code>number</code> | <code>0</code> | Starting offset. |
| [optionsOrOffset.type] | [<code>WritingMode</code>](#WritingMode) | <code>reliable</code> | Writing mode |

<a name="GattCharacteristic+writeValueWithoutResponse"></a>

### gattCharacteristic.writeValueWithoutResponse(value, [offset]) ⇒ <code>Promise</code>
Write the value of the characteristic without waiting for the response.

**Kind**: instance method of [<code>GattCharacteristic</code>](#GattCharacteristic)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | <code>Buffer</code> | | Buffer containing the characteristic value. |
| [offset] | <code>number</code> | <code>0</code> | Starting offset. |

<a name="GattCharacteristic+writeValueWithResponse"></a>

### gattCharacteristic.writeValueWithResponse(value, [offset]) ⇒ <code>Promise</code>
Write the value of the characteristic and wait for the response.

**Kind**: instance method of [<code>GattCharacteristic</code>](#GattCharacteristic)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | <code>Buffer</code> | | Buffer containing the characteristic value. |
| [offset] | <code>number</code> | <code>0</code> | Starting offset. |

<a name="GattCharacteristic+startNotifications"></a>

### gattCharacteristic.startNotifications()
Expand Down
20 changes: 20 additions & 0 deletions src/GattCharacteristic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions test/GattCharacteristic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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]))
})
Expand Down

0 comments on commit e41aaf5

Please sign in to comment.