UNPKG

@abandonware/noble

Version:

A Node.js BLE (Bluetooth Low Energy) central library.

73 lines (60 loc) 1.6 kB
const events = require('events'); const util = require('util'); const descriptors = require('./descriptors.json'); function Descriptor (noble, peripheralId, serviceUuid, characteristicUuid, uuid) { this._noble = noble; this._peripheralId = peripheralId; this._serviceUuid = serviceUuid; this._characteristicUuid = characteristicUuid; this.uuid = uuid; this.name = null; this.type = null; const descriptor = descriptors[uuid]; if (descriptor) { this.name = descriptor.name; this.type = descriptor.type; } } util.inherits(Descriptor, events.EventEmitter); Descriptor.prototype.toString = function () { return JSON.stringify({ uuid: this.uuid, name: this.name, type: this.type }); }; const readValue = function (callback) { if (callback) { this.once('valueRead', data => { callback(null, data); }); } this._noble.readValue( this._peripheralId, this._serviceUuid, this._characteristicUuid, this.uuid ); }; Descriptor.prototype.readValue = readValue; Descriptor.prototype.readValueAsync = util.promisify(readValue); const writeValue = function (data, callback) { if (!(data instanceof Buffer)) { throw new Error('data must be a Buffer'); } if (callback) { this.once('valueWrite', () => { callback(null); }); } this._noble.writeValue( this._peripheralId, this._serviceUuid, this._characteristicUuid, this.uuid, data ); }; Descriptor.prototype.writeValue = writeValue; Descriptor.prototype.writeValueAsync = util.promisify(writeValue); module.exports = Descriptor;