@stoprocent/noble 1.9.2-16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +11 -0
- package/.eslintrc.js +25 -0
- package/.github/FUNDING.yml +2 -0
- package/.github/workflows/fediverse-action.yml +16 -0
- package/.github/workflows/nodepackage.yml +77 -0
- package/.github/workflows/npm-publish.yml +26 -0
- package/.github/workflows/prebuild.yml +65 -0
- package/.nycrc.json +4 -0
- package/CHANGELOG.md +119 -0
- package/LICENSE +20 -0
- package/MAINTAINERS.md +1 -0
- package/README.md +833 -0
- package/assets/noble-logo.png +0 -0
- package/assets/noble-logo.svg +13 -0
- package/binding.gyp +19 -0
- package/codecov.yml +5 -0
- package/examples/advertisement-discovery.js +65 -0
- package/examples/cache-gatt-discovery.js +198 -0
- package/examples/cache-gatt-reconnect.js +164 -0
- package/examples/echo.js +104 -0
- package/examples/enter-exit.js +78 -0
- package/examples/peripheral-explorer-async.js +133 -0
- package/examples/peripheral-explorer.js +225 -0
- package/examples/pizza/README.md +15 -0
- package/examples/pizza/central.js +194 -0
- package/examples/pizza/pizza.js +60 -0
- package/index.d.ts +203 -0
- package/index.js +6 -0
- package/lib/characteristic.js +161 -0
- package/lib/characteristics.json +449 -0
- package/lib/descriptor.js +72 -0
- package/lib/descriptors.json +47 -0
- package/lib/distributed/bindings.js +326 -0
- package/lib/hci-socket/acl-stream.js +60 -0
- package/lib/hci-socket/bindings.js +788 -0
- package/lib/hci-socket/crypto.js +74 -0
- package/lib/hci-socket/gap.js +432 -0
- package/lib/hci-socket/gatt.js +809 -0
- package/lib/hci-socket/hci-status.json +71 -0
- package/lib/hci-socket/hci.js +1264 -0
- package/lib/hci-socket/signaling.js +76 -0
- package/lib/hci-socket/smp.js +140 -0
- package/lib/hci-uart/bindings.js +569 -0
- package/lib/hci-uart/hci-serial-parser.js +70 -0
- package/lib/hci-uart/hci.js +1336 -0
- package/lib/mac/binding.gyp +26 -0
- package/lib/mac/bindings.js +11 -0
- package/lib/mac/src/ble_manager.h +41 -0
- package/lib/mac/src/ble_manager.mm +435 -0
- package/lib/mac/src/callbacks.cc +222 -0
- package/lib/mac/src/callbacks.h +84 -0
- package/lib/mac/src/napi_objc.h +12 -0
- package/lib/mac/src/napi_objc.mm +50 -0
- package/lib/mac/src/noble_mac.h +34 -0
- package/lib/mac/src/noble_mac.mm +264 -0
- package/lib/mac/src/objc_cpp.h +26 -0
- package/lib/mac/src/objc_cpp.mm +126 -0
- package/lib/mac/src/peripheral.h +23 -0
- package/lib/manufacture.js +48 -0
- package/lib/noble.js +593 -0
- package/lib/peripheral.js +219 -0
- package/lib/resolve-bindings-web.js +9 -0
- package/lib/resolve-bindings.js +44 -0
- package/lib/service.js +72 -0
- package/lib/services.json +92 -0
- package/lib/webbluetooth/bindings.js +368 -0
- package/lib/websocket/bindings.js +321 -0
- package/lib/win/binding.gyp +23 -0
- package/lib/win/bindings.js +11 -0
- package/lib/win/src/ble_manager.cc +802 -0
- package/lib/win/src/ble_manager.h +77 -0
- package/lib/win/src/callbacks.cc +274 -0
- package/lib/win/src/callbacks.h +33 -0
- package/lib/win/src/napi_winrt.cc +76 -0
- package/lib/win/src/napi_winrt.h +12 -0
- package/lib/win/src/noble_winrt.cc +308 -0
- package/lib/win/src/noble_winrt.h +34 -0
- package/lib/win/src/notify_map.cc +62 -0
- package/lib/win/src/notify_map.h +50 -0
- package/lib/win/src/peripheral.h +23 -0
- package/lib/win/src/peripheral_winrt.cc +296 -0
- package/lib/win/src/peripheral_winrt.h +82 -0
- package/lib/win/src/radio_watcher.cc +125 -0
- package/lib/win/src/radio_watcher.h +61 -0
- package/lib/win/src/winrt_cpp.cc +82 -0
- package/lib/win/src/winrt_cpp.h +11 -0
- package/lib/win/src/winrt_guid.cc +12 -0
- package/lib/win/src/winrt_guid.h +13 -0
- package/misc/nrf52840dk.hex +6921 -0
- package/misc/prj.conf +43 -0
- package/package.json +96 -0
- package/test/lib/characteristic.test.js +791 -0
- package/test/lib/descriptor.test.js +249 -0
- package/test/lib/distributed/bindings.test.js +918 -0
- package/test/lib/hci-socket/acl-stream.test.js +188 -0
- package/test/lib/hci-socket/bindings.test.js +1756 -0
- package/test/lib/hci-socket/crypto.test.js +55 -0
- package/test/lib/hci-socket/gap.test.js +1089 -0
- package/test/lib/hci-socket/gatt.test.js +2392 -0
- package/test/lib/hci-socket/hci.test.js +1891 -0
- package/test/lib/hci-socket/signaling.test.js +94 -0
- package/test/lib/hci-socket/smp.test.js +268 -0
- package/test/lib/manufacture.test.js +77 -0
- package/test/lib/peripheral.test.js +623 -0
- package/test/lib/resolve-bindings.test.js +102 -0
- package/test/lib/service.test.js +195 -0
- package/test/lib/webbluetooth/bindings.test.js +190 -0
- package/test/lib/websocket/bindings.test.js +456 -0
- package/test/noble.test.js +1565 -0
- package/test.js +131 -0
- package/with-bindings.js +5 -0
- package/ws-slave.js +404 -0
package/lib/noble.js
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
const debug = require('debug')('noble');
|
|
2
|
+
|
|
3
|
+
const events = require('events');
|
|
4
|
+
const util = require('util');
|
|
5
|
+
|
|
6
|
+
const Peripheral = require('./peripheral');
|
|
7
|
+
const Service = require('./service');
|
|
8
|
+
const Characteristic = require('./characteristic');
|
|
9
|
+
const Descriptor = require('./descriptor');
|
|
10
|
+
|
|
11
|
+
function Noble (bindings) {
|
|
12
|
+
this.initialized = false;
|
|
13
|
+
|
|
14
|
+
this.address = 'unknown';
|
|
15
|
+
this._state = 'unknown';
|
|
16
|
+
this._bindings = bindings;
|
|
17
|
+
this._peripherals = {};
|
|
18
|
+
this._services = {};
|
|
19
|
+
this._characteristics = {};
|
|
20
|
+
this._descriptors = {};
|
|
21
|
+
this._discoveredPeripheralUUids = [];
|
|
22
|
+
|
|
23
|
+
this._bindings.on('stateChange', this.onStateChange.bind(this));
|
|
24
|
+
this._bindings.on('addressChange', this.onAddressChange.bind(this));
|
|
25
|
+
this._bindings.on('scanParametersSet', this.onScanParametersSet.bind(this));
|
|
26
|
+
this._bindings.on('scanStart', this.onScanStart.bind(this));
|
|
27
|
+
this._bindings.on('scanStop', this.onScanStop.bind(this));
|
|
28
|
+
this._bindings.on('discover', this.onDiscover.bind(this));
|
|
29
|
+
this._bindings.on('connect', this.onConnect.bind(this));
|
|
30
|
+
this._bindings.on('disconnect', this.onDisconnect.bind(this));
|
|
31
|
+
this._bindings.on('rssiUpdate', this.onRssiUpdate.bind(this));
|
|
32
|
+
this._bindings.on('servicesDiscover', this.onServicesDiscover.bind(this));
|
|
33
|
+
this._bindings.on('servicesDiscovered', this.onServicesDiscovered.bind(this));
|
|
34
|
+
this._bindings.on('includedServicesDiscover', this.onIncludedServicesDiscover.bind(this));
|
|
35
|
+
this._bindings.on('characteristicsDiscover', this.onCharacteristicsDiscover.bind(this));
|
|
36
|
+
this._bindings.on('characteristicsDiscovered', this.onCharacteristicsDiscovered.bind(this));
|
|
37
|
+
this._bindings.on('read', this.onRead.bind(this));
|
|
38
|
+
this._bindings.on('write', this.onWrite.bind(this));
|
|
39
|
+
this._bindings.on('broadcast', this.onBroadcast.bind(this));
|
|
40
|
+
this._bindings.on('notify', this.onNotify.bind(this));
|
|
41
|
+
this._bindings.on('descriptorsDiscover', this.onDescriptorsDiscover.bind(this));
|
|
42
|
+
this._bindings.on('valueRead', this.onValueRead.bind(this));
|
|
43
|
+
this._bindings.on('valueWrite', this.onValueWrite.bind(this));
|
|
44
|
+
this._bindings.on('handleRead', this.onHandleRead.bind(this));
|
|
45
|
+
this._bindings.on('handleWrite', this.onHandleWrite.bind(this));
|
|
46
|
+
this._bindings.on('handleNotify', this.onHandleNotify.bind(this));
|
|
47
|
+
this._bindings.on('onMtu', this.onMtu.bind(this));
|
|
48
|
+
|
|
49
|
+
this.on('warning', (message) => {
|
|
50
|
+
if (this.listeners('warning').length === 1) {
|
|
51
|
+
console.warn(`noble: ${message}`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// lazy init bindings on first new listener, should be on stateChange
|
|
56
|
+
this.on('newListener', (event) => {
|
|
57
|
+
if (event === 'stateChange' && !this.initialized) {
|
|
58
|
+
this.initialized = true;
|
|
59
|
+
|
|
60
|
+
process.nextTick(() => {
|
|
61
|
+
this._bindings.init();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// or lazy init bindings if someone attempts to get state first
|
|
67
|
+
Object.defineProperties(this, {
|
|
68
|
+
state: {
|
|
69
|
+
get: function () {
|
|
70
|
+
if (!this.initialized) {
|
|
71
|
+
this.initialized = true;
|
|
72
|
+
|
|
73
|
+
this._bindings.init();
|
|
74
|
+
}
|
|
75
|
+
return this._state;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
util.inherits(Noble, events.EventEmitter);
|
|
82
|
+
|
|
83
|
+
Noble.prototype.onStateChange = function (state) {
|
|
84
|
+
debug(`stateChange ${state}`);
|
|
85
|
+
|
|
86
|
+
this._state = state;
|
|
87
|
+
|
|
88
|
+
this.emit('stateChange', state);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
Noble.prototype.onAddressChange = function (address) {
|
|
92
|
+
debug(`addressChange ${address}`);
|
|
93
|
+
|
|
94
|
+
this.address = address;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
Noble.prototype.setScanParameters = function (interval, window, callback) {
|
|
98
|
+
if (callback) {
|
|
99
|
+
this.once('scanParametersSet', callback);
|
|
100
|
+
}
|
|
101
|
+
this._bindings.setScanParameters(interval, window);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
Noble.prototype.onScanParametersSet = function () {
|
|
105
|
+
debug('scanParametersSet');
|
|
106
|
+
this.emit('scanParametersSet');
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const startScanning = function (serviceUuids, allowDuplicates, callback) {
|
|
110
|
+
if (typeof serviceUuids === 'function') {
|
|
111
|
+
this.emit('warning', 'calling startScanning(callback) is deprecated');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (typeof allowDuplicates === 'function') {
|
|
115
|
+
this.emit('warning', 'calling startScanning(serviceUuids, callback) is deprecated');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const scan = function (state) {
|
|
119
|
+
if (state !== 'poweredOn') {
|
|
120
|
+
const error = new Error(`Could not start scanning, state is ${state} (not poweredOn)`);
|
|
121
|
+
|
|
122
|
+
if (typeof callback === 'function') {
|
|
123
|
+
callback(error);
|
|
124
|
+
} else {
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
if (callback) {
|
|
129
|
+
this.once('scanStart', filterDuplicates => {
|
|
130
|
+
callback(null, filterDuplicates);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
this._discoveredPeripheralUUids = [];
|
|
135
|
+
this._allowDuplicates = allowDuplicates;
|
|
136
|
+
|
|
137
|
+
this._bindings.startScanning(serviceUuids, allowDuplicates);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// if bindings still not init, do it now
|
|
142
|
+
if (!this.initialized) {
|
|
143
|
+
this.initialized = true;
|
|
144
|
+
|
|
145
|
+
this._bindings.init();
|
|
146
|
+
|
|
147
|
+
this.once('stateChange', scan.bind(this));
|
|
148
|
+
} else {
|
|
149
|
+
scan.call(this, this._state);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
Noble.prototype.startScanning = startScanning;
|
|
154
|
+
Noble.prototype.startScanningAsync = function (serviceUUIDs, allowDuplicates) {
|
|
155
|
+
return util.promisify((callback) => this.startScanning(serviceUUIDs, allowDuplicates, callback))();
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
Noble.prototype.onScanStart = function (filterDuplicates) {
|
|
159
|
+
debug('scanStart');
|
|
160
|
+
this.emit('scanStart', filterDuplicates);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const stopScanning = function (callback) {
|
|
164
|
+
if (callback) {
|
|
165
|
+
this.once('scanStop', callback);
|
|
166
|
+
}
|
|
167
|
+
if (this._bindings && this.initialized) {
|
|
168
|
+
this._bindings.stopScanning();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
Noble.prototype.stopScanning = stopScanning;
|
|
173
|
+
Noble.prototype.stopScanningAsync = util.promisify(stopScanning);
|
|
174
|
+
|
|
175
|
+
Noble.prototype.onScanStop = function () {
|
|
176
|
+
debug('scanStop');
|
|
177
|
+
this.emit('scanStop');
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
Noble.prototype.reset = function () {
|
|
181
|
+
this._bindings.reset();
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
Noble.prototype.onDiscover = function (uuid, address, addressType, connectable, advertisement, rssi, scannable) {
|
|
185
|
+
let peripheral = this._peripherals[uuid];
|
|
186
|
+
|
|
187
|
+
if (!peripheral) {
|
|
188
|
+
peripheral = new Peripheral(this, uuid, address, addressType, connectable, advertisement, rssi, scannable);
|
|
189
|
+
|
|
190
|
+
this._peripherals[uuid] = peripheral;
|
|
191
|
+
this._services[uuid] = {};
|
|
192
|
+
this._characteristics[uuid] = {};
|
|
193
|
+
this._descriptors[uuid] = {};
|
|
194
|
+
} else {
|
|
195
|
+
// "or" the advertisment data with existing
|
|
196
|
+
for (const i in advertisement) {
|
|
197
|
+
if (advertisement[i] !== undefined) {
|
|
198
|
+
peripheral.advertisement[i] = advertisement[i];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
peripheral.connectable = connectable;
|
|
203
|
+
peripheral.scannable = scannable;
|
|
204
|
+
peripheral.rssi = rssi;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const previouslyDiscoverd = (this._discoveredPeripheralUUids.indexOf(uuid) !== -1);
|
|
208
|
+
|
|
209
|
+
if (!previouslyDiscoverd) {
|
|
210
|
+
this._discoveredPeripheralUUids.push(uuid);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (this._allowDuplicates || !previouslyDiscoverd || (!scannable && !connectable)) {
|
|
214
|
+
this.emit('discover', peripheral);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
Noble.prototype.connect = function (peripheralUuid, parameters) {
|
|
219
|
+
this._bindings.connect(peripheralUuid, parameters);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
Noble.prototype.onConnect = function (peripheralUuid, error) {
|
|
223
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
224
|
+
|
|
225
|
+
if (peripheral) {
|
|
226
|
+
peripheral.state = error ? 'error' : 'connected';
|
|
227
|
+
peripheral.emit('connect', error);
|
|
228
|
+
} else {
|
|
229
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} connected!`);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
Noble.prototype.cancelConnect = function (peripheralUuid, parameters) {
|
|
234
|
+
this._bindings.cancelConnect(peripheralUuid, parameters);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
Noble.prototype.disconnect = function (peripheralUuid) {
|
|
238
|
+
this._bindings.disconnect(peripheralUuid);
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
Noble.prototype.onDisconnect = function (peripheralUuid, reason) {
|
|
242
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
243
|
+
|
|
244
|
+
if (peripheral) {
|
|
245
|
+
peripheral.state = 'disconnected';
|
|
246
|
+
peripheral.emit('disconnect', reason);
|
|
247
|
+
} else {
|
|
248
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} disconnected!`);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
Noble.prototype.updateRssi = function (peripheralUuid) {
|
|
253
|
+
this._bindings.updateRssi(peripheralUuid);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
Noble.prototype.onRssiUpdate = function (peripheralUuid, rssi) {
|
|
257
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
258
|
+
|
|
259
|
+
if (peripheral) {
|
|
260
|
+
peripheral.rssi = rssi;
|
|
261
|
+
|
|
262
|
+
peripheral.emit('rssiUpdate', rssi);
|
|
263
|
+
} else {
|
|
264
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} RSSI update!`);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/// add an array of service objects (as retrieved via the servicesDiscovered event)
|
|
269
|
+
Noble.prototype.addServices = function (peripheralUuid, services) {
|
|
270
|
+
const servObjs = [];
|
|
271
|
+
|
|
272
|
+
for (let i = 0; i < services.length; i++) {
|
|
273
|
+
const o = this.addService(peripheralUuid, services[i]);
|
|
274
|
+
servObjs.push(o);
|
|
275
|
+
}
|
|
276
|
+
return servObjs;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/// service is a ServiceObject { uuid, startHandle, endHandle,..}
|
|
280
|
+
Noble.prototype.addService = function (peripheralUuid, service) {
|
|
281
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
282
|
+
|
|
283
|
+
// pass on to lower layers (gatt)
|
|
284
|
+
if (this._bindings.addService) {
|
|
285
|
+
this._bindings.addService(peripheralUuid, service);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (!peripheral.services) {
|
|
289
|
+
peripheral.services = [];
|
|
290
|
+
}
|
|
291
|
+
// allocate internal service object and return
|
|
292
|
+
const serv = new Service(this, peripheralUuid, service.uuid);
|
|
293
|
+
|
|
294
|
+
this._services[peripheralUuid][service.uuid] = serv;
|
|
295
|
+
this._characteristics[peripheralUuid][service.uuid] = {};
|
|
296
|
+
this._descriptors[peripheralUuid][service.uuid] = {};
|
|
297
|
+
|
|
298
|
+
peripheral.services.push(serv);
|
|
299
|
+
|
|
300
|
+
return serv;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/// callback receiving a list of service objects from the gatt layer
|
|
304
|
+
Noble.prototype.onServicesDiscovered = function (peripheralUuid, services) {
|
|
305
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
306
|
+
|
|
307
|
+
if (peripheral) { peripheral.emit('servicesDiscovered', peripheral, services); } // pass on to higher layers
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
Noble.prototype.discoverServices = function (peripheralUuid, uuids) {
|
|
311
|
+
this._bindings.discoverServices(peripheralUuid, uuids);
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
Noble.prototype.onServicesDiscover = function (peripheralUuid, serviceUuids) {
|
|
315
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
316
|
+
|
|
317
|
+
if (peripheral) {
|
|
318
|
+
const services = [];
|
|
319
|
+
|
|
320
|
+
for (let i = 0; i < serviceUuids.length; i++) {
|
|
321
|
+
const serviceUuid = serviceUuids[i];
|
|
322
|
+
const service = new Service(this, peripheralUuid, serviceUuid);
|
|
323
|
+
|
|
324
|
+
this._services[peripheralUuid][serviceUuid] = service;
|
|
325
|
+
this._characteristics[peripheralUuid][serviceUuid] = {};
|
|
326
|
+
this._descriptors[peripheralUuid][serviceUuid] = {};
|
|
327
|
+
|
|
328
|
+
services.push(service);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
peripheral.services = services;
|
|
332
|
+
|
|
333
|
+
peripheral.emit('servicesDiscover', services);
|
|
334
|
+
} else {
|
|
335
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} services discover!`);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
Noble.prototype.discoverIncludedServices = function (peripheralUuid, serviceUuid, serviceUuids) {
|
|
340
|
+
this._bindings.discoverIncludedServices(peripheralUuid, serviceUuid, serviceUuids);
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
Noble.prototype.onIncludedServicesDiscover = function (peripheralUuid, serviceUuid, includedServiceUuids) {
|
|
344
|
+
const service = this._services[peripheralUuid][serviceUuid];
|
|
345
|
+
|
|
346
|
+
if (service) {
|
|
347
|
+
service.includedServiceUuids = includedServiceUuids;
|
|
348
|
+
|
|
349
|
+
service.emit('includedServicesDiscover', includedServiceUuids);
|
|
350
|
+
} else {
|
|
351
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid} included services discover!`);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
/// add characteristics to the peripheral; returns an array of initialized Characteristics objects
|
|
356
|
+
Noble.prototype.addCharacteristics = function (peripheralUuid, serviceUuid, characteristics) {
|
|
357
|
+
// first, initialize gatt layer:
|
|
358
|
+
if (this._bindings.addCharacteristics) {
|
|
359
|
+
this._bindings.addCharacteristics(peripheralUuid, serviceUuid, characteristics);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const service = this._services[peripheralUuid][serviceUuid];
|
|
363
|
+
if (!service) {
|
|
364
|
+
this.emit('warning', `unknown service ${peripheralUuid}, ${serviceUuid} characteristics discover!`);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const characteristics_ = [];
|
|
369
|
+
for (let i = 0; i < characteristics.length; i++) {
|
|
370
|
+
const characteristicUuid = characteristics[i].uuid;
|
|
371
|
+
|
|
372
|
+
const characteristic = new Characteristic(
|
|
373
|
+
this,
|
|
374
|
+
peripheralUuid,
|
|
375
|
+
serviceUuid,
|
|
376
|
+
characteristicUuid,
|
|
377
|
+
characteristics[i].properties
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
this._characteristics[peripheralUuid][serviceUuid][characteristicUuid] = characteristic;
|
|
381
|
+
this._descriptors[peripheralUuid][serviceUuid][characteristicUuid] = {};
|
|
382
|
+
|
|
383
|
+
characteristics_.push(characteristic);
|
|
384
|
+
}
|
|
385
|
+
service.characteristics = characteristics_;
|
|
386
|
+
return characteristics_;
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
Noble.prototype.onCharacteristicsDiscovered = function (peripheralUuid, serviceUuid, characteristics) {
|
|
390
|
+
const service = this._services[peripheralUuid][serviceUuid];
|
|
391
|
+
|
|
392
|
+
service.emit('characteristicsDiscovered', characteristics);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
Noble.prototype.discoverCharacteristics = function (peripheralUuid, serviceUuid, characteristicUuids) {
|
|
396
|
+
this._bindings.discoverCharacteristics(peripheralUuid, serviceUuid, characteristicUuids);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
Noble.prototype.onCharacteristicsDiscover = function (peripheralUuid, serviceUuid, characteristics) {
|
|
400
|
+
const service = this._services[peripheralUuid][serviceUuid];
|
|
401
|
+
|
|
402
|
+
if (service) {
|
|
403
|
+
const characteristics_ = [];
|
|
404
|
+
|
|
405
|
+
for (let i = 0; i < characteristics.length; i++) {
|
|
406
|
+
const characteristicUuid = characteristics[i].uuid;
|
|
407
|
+
|
|
408
|
+
const characteristic = new Characteristic(
|
|
409
|
+
this,
|
|
410
|
+
peripheralUuid,
|
|
411
|
+
serviceUuid,
|
|
412
|
+
characteristicUuid,
|
|
413
|
+
characteristics[i].properties
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
this._characteristics[peripheralUuid][serviceUuid][characteristicUuid] = characteristic;
|
|
417
|
+
this._descriptors[peripheralUuid][serviceUuid][characteristicUuid] = {};
|
|
418
|
+
|
|
419
|
+
characteristics_.push(characteristic);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
service.characteristics = characteristics_;
|
|
423
|
+
|
|
424
|
+
service.emit('characteristicsDiscover', characteristics_);
|
|
425
|
+
} else {
|
|
426
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid} characteristics discover!`);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
Noble.prototype.read = function (peripheralUuid, serviceUuid, characteristicUuid) {
|
|
431
|
+
this._bindings.read(peripheralUuid, serviceUuid, characteristicUuid);
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
Noble.prototype.onRead = function (peripheralUuid, serviceUuid, characteristicUuid, data, isNotification) {
|
|
435
|
+
const characteristic = this._characteristics[peripheralUuid][serviceUuid][characteristicUuid];
|
|
436
|
+
|
|
437
|
+
if (characteristic) {
|
|
438
|
+
characteristic.emit('data', data, isNotification);
|
|
439
|
+
|
|
440
|
+
characteristic.emit('read', data, isNotification); // for backwards compatbility
|
|
441
|
+
} else {
|
|
442
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid} read!`);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
Noble.prototype.write = function (peripheralUuid, serviceUuid, characteristicUuid, data, withoutResponse) {
|
|
447
|
+
this._bindings.write(peripheralUuid, serviceUuid, characteristicUuid, data, withoutResponse);
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
Noble.prototype.onWrite = function (peripheralUuid, serviceUuid, characteristicUuid) {
|
|
451
|
+
const characteristic = this._characteristics[peripheralUuid][serviceUuid][characteristicUuid];
|
|
452
|
+
|
|
453
|
+
if (characteristic) {
|
|
454
|
+
characteristic.emit('write');
|
|
455
|
+
} else {
|
|
456
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid} write!`);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
Noble.prototype.broadcast = function (peripheralUuid, serviceUuid, characteristicUuid, broadcast) {
|
|
461
|
+
this._bindings.broadcast(peripheralUuid, serviceUuid, characteristicUuid, broadcast);
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
Noble.prototype.onBroadcast = function (peripheralUuid, serviceUuid, characteristicUuid, state) {
|
|
465
|
+
const characteristic = this._characteristics[peripheralUuid][serviceUuid][characteristicUuid];
|
|
466
|
+
|
|
467
|
+
if (characteristic) {
|
|
468
|
+
characteristic.emit('broadcast', state);
|
|
469
|
+
} else {
|
|
470
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid} broadcast!`);
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
Noble.prototype.notify = function (peripheralUuid, serviceUuid, characteristicUuid, notify) {
|
|
475
|
+
this._bindings.notify(peripheralUuid, serviceUuid, characteristicUuid, notify);
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
Noble.prototype.onNotify = function (peripheralUuid, serviceUuid, characteristicUuid, state) {
|
|
479
|
+
const characteristic = this._characteristics[peripheralUuid][serviceUuid][characteristicUuid];
|
|
480
|
+
|
|
481
|
+
if (characteristic) {
|
|
482
|
+
characteristic.emit('notify', state);
|
|
483
|
+
} else {
|
|
484
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid} notify!`);
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
Noble.prototype.discoverDescriptors = function (peripheralUuid, serviceUuid, characteristicUuid) {
|
|
489
|
+
this._bindings.discoverDescriptors(peripheralUuid, serviceUuid, characteristicUuid);
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
Noble.prototype.onDescriptorsDiscover = function (peripheralUuid, serviceUuid, characteristicUuid, descriptors) {
|
|
493
|
+
const characteristic = this._characteristics[peripheralUuid][serviceUuid][characteristicUuid];
|
|
494
|
+
|
|
495
|
+
if (characteristic) {
|
|
496
|
+
const descriptors_ = [];
|
|
497
|
+
|
|
498
|
+
for (let i = 0; i < descriptors.length; i++) {
|
|
499
|
+
const descriptorUuid = descriptors[i];
|
|
500
|
+
|
|
501
|
+
const descriptor = new Descriptor(
|
|
502
|
+
this,
|
|
503
|
+
peripheralUuid,
|
|
504
|
+
serviceUuid,
|
|
505
|
+
characteristicUuid,
|
|
506
|
+
descriptorUuid
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
this._descriptors[peripheralUuid][serviceUuid][characteristicUuid][descriptorUuid] = descriptor;
|
|
510
|
+
|
|
511
|
+
descriptors_.push(descriptor);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
characteristic.descriptors = descriptors_;
|
|
515
|
+
|
|
516
|
+
characteristic.emit('descriptorsDiscover', descriptors_);
|
|
517
|
+
} else {
|
|
518
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid} descriptors discover!`);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
Noble.prototype.readValue = function (peripheralUuid, serviceUuid, characteristicUuid, descriptorUuid) {
|
|
523
|
+
this._bindings.readValue(peripheralUuid, serviceUuid, characteristicUuid, descriptorUuid);
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
Noble.prototype.onValueRead = function (peripheralUuid, serviceUuid, characteristicUuid, descriptorUuid, data) {
|
|
527
|
+
const descriptor = this._descriptors[peripheralUuid][serviceUuid][characteristicUuid][descriptorUuid];
|
|
528
|
+
|
|
529
|
+
if (descriptor) {
|
|
530
|
+
descriptor.emit('valueRead', data);
|
|
531
|
+
} else {
|
|
532
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid}, ${descriptorUuid} value read!`);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
Noble.prototype.writeValue = function (peripheralUuid, serviceUuid, characteristicUuid, descriptorUuid, data) {
|
|
537
|
+
this._bindings.writeValue(peripheralUuid, serviceUuid, characteristicUuid, descriptorUuid, data);
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
Noble.prototype.onValueWrite = function (peripheralUuid, serviceUuid, characteristicUuid, descriptorUuid) {
|
|
541
|
+
const descriptor = this._descriptors[peripheralUuid][serviceUuid][characteristicUuid][descriptorUuid];
|
|
542
|
+
|
|
543
|
+
if (descriptor) {
|
|
544
|
+
descriptor.emit('valueWrite');
|
|
545
|
+
} else {
|
|
546
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid}, ${serviceUuid}, ${characteristicUuid}, ${descriptorUuid} value write!`);
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
Noble.prototype.readHandle = function (peripheralUuid, handle) {
|
|
551
|
+
this._bindings.readHandle(peripheralUuid, handle);
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
Noble.prototype.onHandleRead = function (peripheralUuid, handle, data) {
|
|
555
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
556
|
+
|
|
557
|
+
if (peripheral) {
|
|
558
|
+
peripheral.emit(`handleRead${handle}`, data);
|
|
559
|
+
} else {
|
|
560
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} handle read!`);
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
Noble.prototype.writeHandle = function (peripheralUuid, handle, data, withoutResponse) {
|
|
565
|
+
this._bindings.writeHandle(peripheralUuid, handle, data, withoutResponse);
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
Noble.prototype.onHandleWrite = function (peripheralUuid, handle) {
|
|
569
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
570
|
+
|
|
571
|
+
if (peripheral) {
|
|
572
|
+
peripheral.emit(`handleWrite${handle}`);
|
|
573
|
+
} else {
|
|
574
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} handle write!`);
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
Noble.prototype.onHandleNotify = function (peripheralUuid, handle, data) {
|
|
579
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
580
|
+
|
|
581
|
+
if (peripheral) {
|
|
582
|
+
peripheral.emit('handleNotify', handle, data);
|
|
583
|
+
} else {
|
|
584
|
+
this.emit('warning', `unknown peripheral ${peripheralUuid} handle notify!`);
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
Noble.prototype.onMtu = function (peripheralUuid, mtu) {
|
|
589
|
+
const peripheral = this._peripherals[peripheralUuid];
|
|
590
|
+
if (peripheral && 'mtu' in peripheral && mtu) peripheral.mtu = mtu;
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
module.exports = Noble;
|