UNPKG

1.98 kBJavaScriptView Raw
1var events = require('events');
2var util = require('util');
3var characteristics = require('./characteristics.json');
4
5function Characteristic(peripheral, uuid, handle, value) {
6 this._peripheral = peripheral;
7
8 this.uuid = uuid;
9 this.handle = handle;
10 this.descriptors = {};
11 this.name = null;
12 this.type = null;
13 this.value = value;
14
15 if (uuid) {
16 this.setUUID(uuid);
17 }
18}
19
20util.inherits(Characteristic, events.EventEmitter);
21
22Characteristic.prototype.toString = function() {
23 return JSON.stringify({
24 uuid: this.uuid,
25 name: this.name,
26 type: this.type,
27 handle : this.handle,
28 value : this.value,
29 });
30}
31
32Characteristic.prototype.setUUID = function(uuid) {
33 this.uuid = uuid;
34
35 var characteristic = characteristics[uuid.toString()];
36 if (characteristic) {
37 this.name = characteristic.name;
38 this.type = characteristic.type;
39 }
40}
41
42Characteristic.prototype.read = function(callback) {
43 this._peripheral._controller.read(this, callback);
44};
45
46Characteristic.prototype.write = function(data, callback) {
47 this._peripheral._controller.write(this, data, callback);
48};
49
50Characteristic.prototype.startNotifications = function(callback) {
51 this._peripheral._controller.startNotifications(this, callback);
52}
53
54Characteristic.prototype.stopNotifications = function(callback) {
55 this._peripheral._controller.stopNotifications(this, callback);
56}
57
58Characteristic.prototype.startIndications = function(callback) {
59 this._peripheral._controller.startNotifications(this, callback);
60}
61
62Characteristic.prototype.stopIndications = function(callback) {
63 this._peripheral._controller.stopIndications(this, callback);
64}
65
66Characteristic.prototype.confirmIndication = function(callback) {
67 this._peripheral._controller.confirmIndication(this, callback);
68}
69Characteristic.prototype.discoverAllDescriptors = function(callback) {
70 this._peripheral._controller.discoverDescriptorsOfCharacteristic(this, callback);
71};
72// }
73
74module.exports = Characteristic;