node-ble
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "node-ble", | ||
"description": "Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"repository": "https://github.com/chrvadala/node-ble.git", | ||
@@ -41,5 +41,5 @@ "author": "chrvadala", | ||
"@babel/plugin-proposal-class-properties": "^7.12.1", | ||
"@babel/plugin-proposal-decorators": "^7.12.1", | ||
"@types/jest": "^26.0.19", | ||
"@types/node": "^14.14.14", | ||
"@babel/plugin-proposal-decorators": "^7.12.12", | ||
"@types/jest": "^26.0.20", | ||
"@types/node": "^14.14.22", | ||
"dotenv": "^8.2.0", | ||
@@ -56,3 +56,4 @@ "jest": "^26.6.3", | ||
] | ||
} | ||
}, | ||
"funding": "https://www.paypal.me/chrvadala/25" | ||
} |
@@ -169,3 +169,3 @@ # node-ble | ||
| `Promise<Buffer> readValue(Number offset = 0)` | Issues a request to read the value of the characteristic and returns the value if the operation was successful. | | ||
| `Promise<void> writeValue(Buffer buffer, Number offset = 0)` | Issues a request to write the value of the characteristic. | | ||
| `Promise<void> writeValue(Buffer buffer, Number | WriteValueOptions options = {})` | Issues a request to write the value of the characteristic. Default options `{ offset: 0, type: 'reliable' }`. | | ||
| `Promise<void> startNotifications()` | Starts a notification session from this characteristic if it supports value notifications or indications. | | ||
@@ -195,2 +195,3 @@ | `Promise<void> stopNotifications()` | This method will cancel any previous StartNotify transaction. | | ||
- **1.4** - Upgrades deps | ||
- **1.5** - Adds write options configuration `async writeValue (value, optionsOrOffset = {})` [#20](https://github.com/chrvadala/node-ble/pull/20); Upgrades deps | ||
@@ -200,2 +201,3 @@ ## Contributors | ||
- [pascalopitz](https://github.com/pascalopitz) | ||
- [lupol](https://github.com/lupol) | ||
@@ -202,0 +204,0 @@ ## Run tests |
@@ -36,12 +36,17 @@ const EventEmitter = require('events') | ||
async writeValue (value, offset = 0) { | ||
async writeValue (value, optionsOrOffset = {}) { | ||
if (!Buffer.isBuffer(value)) { | ||
throw new Error('Only buffers can be wrote') | ||
} | ||
const options = { | ||
offset: buildTypedValue('uint16', offset), | ||
type: buildTypedValue('string', 'reliable') | ||
const options = typeof optionsOrOffset === 'number' ? { offset: optionsOrOffset } : optionsOrOffset | ||
const mergedOptions = Object.assign({ offset: 0, type: 'reliable' }, options) | ||
const callOptions = { | ||
offset: buildTypedValue('uint16', mergedOptions.offset), | ||
type: buildTypedValue('string', mergedOptions.type) | ||
} | ||
const { data } = value.toJSON() | ||
await this.helper.callMethod('WriteValue', data, options) | ||
await this.helper.callMethod('WriteValue', data, callOptions) | ||
} | ||
@@ -48,0 +53,0 @@ |
@@ -9,3 +9,3 @@ import events = require('events'); | ||
readValue(offset?: number): Promise<Buffer>; | ||
writeValue(buffer: Buffer, offset?: number): Promise<void>; | ||
writeValue(buffer: Buffer, optionsOrOffset?: number | WriteValueOptions): Promise<void>; | ||
startNotifications(): Promise<void>; | ||
@@ -79,2 +79,7 @@ stopNotifications(): Promise<void>; | ||
}; | ||
interface WriteValueOptions { | ||
offset?: number; | ||
type?: 'reliable' | 'request' | 'command'; | ||
} | ||
} | ||
@@ -81,0 +86,0 @@ |
@@ -19,2 +19,3 @@ /* global test, expect, jest */ | ||
}) | ||
const buildTypedValue = require('../src/buildTypedValue') | ||
const GattCharacteristic = require('../src/GattCharacteristic') | ||
@@ -39,7 +40,23 @@ const dbus = Symbol('dbus') | ||
const characteristic = new GattCharacteristic(dbus, 'hci0', 'dev_00_00_00_00_00_00', 'characteristic0006', 'char008') | ||
const writeValueOptions = (offset = 0, type = 'reliable') => { | ||
return { offset: buildTypedValue('uint16', offset), type: buildTypedValue('string', type) } | ||
} | ||
await expect(characteristic.writeValue('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)) | ||
await expect(characteristic.writeValue(Buffer.from('hello'))).resolves.toBeUndefined() | ||
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), expect.anything()) | ||
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions()) | ||
await expect(characteristic.writeValue(Buffer.from('hello'), { type: 'command' })).resolves.toBeUndefined() | ||
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(0, 'command')) | ||
await expect(characteristic.writeValue(Buffer.from('hello'), { offset: 9, type: 'request' })).resolves.toBeUndefined() | ||
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(9, 'request')) | ||
await expect(characteristic.writeValue(Buffer.from('hello'), 'incorrect argument')).resolves.toBeUndefined() | ||
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions()) | ||
characteristic.helper.callMethod.mockResolvedValueOnce([255, 100, 0]) | ||
@@ -46,0 +63,0 @@ await expect(characteristic.readValue()).resolves.toEqual(Buffer.from([255, 100, 0])) |
57636
1176
270