forked from abandonware/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacteristic.js
161 lines (132 loc) · 3.69 KB
/
characteristic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const events = require('events');
const util = require('util');
const characteristics = require('./characteristics.json');
function Characteristic (noble, peripheralId, serviceUuid, uuid, properties) {
this._noble = noble;
this._peripheralId = peripheralId;
this._serviceUuid = serviceUuid;
this.uuid = uuid;
this.name = null;
this.type = null;
this.properties = properties;
this.descriptors = null;
const characteristic = characteristics[uuid];
if (characteristic) {
this.name = characteristic.name;
this.type = characteristic.type;
}
}
util.inherits(Characteristic, events.EventEmitter);
Characteristic.prototype.toString = function () {
return JSON.stringify({
uuid: this.uuid,
name: this.name,
type: this.type,
properties: this.properties
});
};
const read = function (callback) {
if (callback) {
const onRead = (data, isNotification) => {
// only call the callback if 'read' event and non-notification
// 'read' for non-notifications is only present for backwards compatbility
if (!isNotification) {
// remove the listener
this.removeListener('read', onRead);
// call the callback
callback(null, data);
}
};
this.on('read', onRead);
}
this._noble.read(
this._peripheralId,
this._serviceUuid,
this.uuid
);
};
Characteristic.prototype.read = read;
Characteristic.prototype.readAsync = util.promisify(read);
const write = function (data, withoutResponse, callback) {
if (process.title !== 'browser') {
const allowedTypes = [
Buffer,
Uint8Array,
Uint16Array,
Uint32Array
];
if (!allowedTypes.some((allowedType) => data instanceof allowedType)) {
throw new Error(`data must be a ${allowedTypes.map((allowedType) => allowedType.name).join(' or ')}`);
}
}
if (callback) {
this.once('write', () => {
callback(null);
});
}
this._noble.write(
this._peripheralId,
this._serviceUuid,
this.uuid,
data,
withoutResponse
);
};
Characteristic.prototype.write = write;
Characteristic.prototype.writeAsync = util.promisify(write);
const broadcast = function (broadcast, callback) {
if (callback) {
this.once('broadcast', () => {
callback(null);
});
}
this._noble.broadcast(
this._peripheralId,
this._serviceUuid,
this.uuid,
broadcast
);
};
Characteristic.prototype.broadcast = broadcast;
Characteristic.prototype.broadcastAsync = util.promisify(broadcast);
// deprecated in favour of subscribe/unsubscribe
const notify = function (notify, callback) {
if (callback) {
this.once('notify', () => {
callback(null);
});
}
this._noble.notify(
this._peripheralId,
this._serviceUuid,
this.uuid,
notify
);
};
Characteristic.prototype.notify = notify;
Characteristic.prototype.notifyAsync = util.promisify(notify);
const subscribe = function (callback) {
this.notify(true, callback);
};
Characteristic.prototype.subscribe = subscribe;
Characteristic.prototype.subscribeAsync = util.promisify(subscribe);
const unsubscribe = function (callback) {
this.notify(false, callback);
};
Characteristic.prototype.unsubscribe = unsubscribe;
Characteristic.prototype.unsubscribeAsync = util.promisify(unsubscribe);
const discoverDescriptors = function (callback) {
if (callback) {
this.once('descriptorsDiscover', descriptors => {
callback(null, descriptors);
});
}
this._noble.discoverDescriptors(
this._peripheralId,
this._serviceUuid,
this.uuid
);
};
Characteristic.prototype.discoverDescriptors = discoverDescriptors;
Characteristic.prototype.discoverDescriptorsAsync = util.promisify(discoverDescriptors);
module.exports = Characteristic;