Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add writeValueWithoutResponse and writeValueWithResponse methods #47

Merged
merged 3 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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