-
Notifications
You must be signed in to change notification settings - Fork 21
/
adapter.ts
420 lines (359 loc) · 18.7 KB
/
adapter.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Node Web Bluetooth
* Copyright (c) 2017 Rob Moran
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { platform } from "os";
import { EventEmitter } from "events";
import { getCanonicalUUID } from "./helpers";
import { BluetoothDevice } from "./device";
import { BluetoothRemoteGATTService } from "./service";
import { BluetoothRemoteGATTDescriptor } from "./descriptor";
import { BluetoothRemoteGATTCharacteristic } from "./characteristic";
import * as noble from "@abandonware/noble";
/**
* @hidden
*/
export interface Adapter extends EventEmitter {
getEnabled: (completeFn: (enabled: boolean) => void) => void;
startScan: (serviceUUIDs: Array<string>, foundFn: (device: Partial<BluetoothDevice>) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;
stopScan: (errorFn?: (errorMsg: string) => void) => void;
connect: (handle: string, connectFn: () => void, disconnectFn: () => void, errorFn?: (errorMsg: string) => void) => void;
disconnect: (handle: string, errorFn?: (errorMsg: string) => void) => void;
discoverServices: (handle: string, serviceUUIDs: Array<string>, completeFn: (services: Array<Partial<BluetoothRemoteGATTService>>) => void, errorFn?: (errorMsg: string) => void) => void;
discoverIncludedServices: (handle: string, serviceUUIDs: Array<string>, completeFn: (services: Array<Partial<BluetoothRemoteGATTService>>) => void, errorFn?: (errorMsg: string) => void) => void;
discoverCharacteristics: (handle: string, characteristicUUIDs: Array<string>, completeFn: (characteristics: Array<Partial<BluetoothRemoteGATTCharacteristic>>) => void, errorFn?: (errorMsg: string) => void) => void;
discoverDescriptors: (handle: string, descriptorUUIDs: Array<string>, completeFn: (descriptors: Array<Partial<BluetoothRemoteGATTDescriptor>>) => void, errorFn?: (errorMsg: string) => void) => void;
readCharacteristic: (handle: string, completeFn: (value: DataView) => void, errorFn?: (errorMsg: string) => void) => void;
writeCharacteristic: (handle: string, value: DataView, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;
enableNotify: (handle: string, notifyFn: () => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;
disableNotify: (handle: string, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;
readDescriptor: (handle: string, completeFn: (value: DataView) => void, errorFn?: (errorMsg: string) => void) => void;
writeDescriptor: (handle: string, value: DataView, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;
}
/**
* @hidden
*/
export class NobleAdapter extends EventEmitter implements Adapter {
public static EVENT_ENABLED: string = "enabledchanged";
private deviceHandles: {} = {};
private serviceHandles: {} = {};
private characteristicHandles: {} = {};
private descriptorHandles: {} = {};
private charNotifies: {} = {};
private discoverFn: (device: noble.Peripheral) => void = null;
private initialised: boolean = false;
private enabled: boolean = false;
private os: string = platform();
constructor() {
super();
this.enabled = this.state;
noble.on("stateChange", () => {
if (this.enabled !== this.state) {
this.enabled = this.state;
this.emit(NobleAdapter.EVENT_ENABLED, this.enabled);
}
});
}
private get state(): boolean {
return (noble.state === "poweredOn");
}
private init(completeFn: () => any): void {
if (this.initialised) return completeFn();
noble.on("discover", deviceInfo => {
if (this.discoverFn) this.discoverFn(deviceInfo);
});
this.initialised = true;
completeFn();
}
private checkForError(errorFn, continueFn?, delay?: number) {
return function(error) {
if (error) errorFn(error);
else if (typeof continueFn === "function") {
const args = [].slice.call(arguments, 1);
if (delay === null) continueFn.apply(this, args);
else setTimeout(() => continueFn.apply(this, args), delay);
}
};
}
private bufferToDataView(buffer: Buffer): DataView {
// Buffer to ArrayBuffer
const arrayBuffer = new Uint8Array(buffer).buffer;
return new DataView(arrayBuffer);
}
private dataViewToBuffer(dataView: DataView): Buffer {
// DataView to TypedArray
const typedArray = new Uint8Array(dataView.buffer);
return new Buffer(typedArray);
}
private validDevice(deviceInfo: noble.Peripheral, serviceUUIDs: Array<string>): boolean {
if (serviceUUIDs.length === 0) {
// Match any device
return true;
}
if (!deviceInfo.advertisement.serviceUuids) {
// No advertised services, no match
return false;
}
const advertisedUUIDs = deviceInfo.advertisement.serviceUuids.map(serviceUUID => {
return getCanonicalUUID(serviceUUID);
});
return serviceUUIDs.some(serviceUUID => {
// An advertised UUID matches our search UUIDs
return (advertisedUUIDs.indexOf(serviceUUID) >= 0);
});
}
private deviceToBluetoothDevice(deviceInfo): Partial<BluetoothDevice> {
const deviceID = (deviceInfo.address && deviceInfo.address !== "unknown") ? deviceInfo.address : deviceInfo.id;
const serviceUUIDs = [];
if (deviceInfo.advertisement.serviceUuids) {
deviceInfo.advertisement.serviceUuids.forEach(serviceUUID => {
serviceUUIDs.push(getCanonicalUUID(serviceUUID));
});
}
const manufacturerData = new Map();
if (deviceInfo.advertisement.manufacturerData) {
// First 2 bytes are 16-bit company identifier
const company = deviceInfo.advertisement.manufacturerData.readUInt16LE(0);
// Remove company ID
const buffer = deviceInfo.advertisement.manufacturerData.slice(2);
manufacturerData.set(("0000" + company.toString(16)).slice(-4), this.bufferToDataView(buffer));
}
const serviceData = new Map();
if (deviceInfo.advertisement.serviceData) {
deviceInfo.advertisement.serviceData.forEach(serviceAdvert => {
serviceData.set(getCanonicalUUID(serviceAdvert.uuid), this.bufferToDataView(serviceAdvert.data));
});
}
return {
id: deviceID,
name: deviceInfo.advertisement.localName,
_serviceUUIDs: serviceUUIDs,
adData: {
rssi: deviceInfo.rssi,
txPower: deviceInfo.advertisement.txPowerLevel,
serviceData: serviceData,
manufacturerData: manufacturerData
}
};
}
public getEnabled(completeFn: (enabled: boolean) => void) {
function stateCB() {
completeFn(this.state);
}
if (noble.state === "unknown" || noble.state === "poweredOff") {
// tslint:disable-next-line:no-string-literal
noble["once"]("stateChange", stateCB.bind(this));
} else {
stateCB.call(this);
}
}
public startScan(serviceUUIDs: Array<string>, foundFn: (device: Partial<BluetoothDevice>) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
this.discoverFn = deviceInfo => {
if (this.validDevice(deviceInfo, serviceUUIDs)) {
const device = this.deviceToBluetoothDevice(deviceInfo);
if (!this.deviceHandles[device.id]) {
this.deviceHandles[device.id] = deviceInfo;
// Only call the found function the first time we find a valid device
foundFn(device);
}
}
};
this.init(() => {
this.deviceHandles = {};
function stateCB() {
if (this.state === true) {
// Noble doesn't correctly match short and canonical UUIDs on Linux, so we need to check ourselves
// Continually scan to pick up all advertised UUIDs
noble.startScanning([], true, this.checkForError(errorFn, completeFn));
} else {
errorFn("adapter not enabled");
}
}
if (noble.state === "unknown" || noble.state === "poweredOff") {
// tslint:disable-next-line:no-string-literal
noble["once"]("stateChange", stateCB.bind(this));
} else {
stateCB.call(this);
}
});
}
public stopScan(_errorFn?: (errorMsg: string) => void): void {
this.discoverFn = null;
noble.stopScanning();
}
public connect(handle: string, connectFn: () => void, disconnectFn: () => void, errorFn?: (errorMsg: string) => void): void {
const baseDevice = this.deviceHandles[handle];
baseDevice.removeAllListeners("connect");
baseDevice.removeAllListeners("disconnect");
baseDevice.once("connect", connectFn);
baseDevice.once("disconnect", () => {
this.serviceHandles = {};
this.characteristicHandles = {};
this.descriptorHandles = {};
this.charNotifies = {};
disconnectFn();
});
baseDevice.connect(this.checkForError(errorFn));
}
public disconnect(handle: string, errorFn?: (errorMsg: string) => void): void {
const baseDevice = this.deviceHandles[handle];
baseDevice.disconnect(this.checkForError(errorFn));
}
public discoverServices(handle: string, serviceUUIDs: Array<string>, completeFn: (services: Array<Partial<BluetoothRemoteGATTService>>) => void, errorFn?: (errorMsg: string) => void): void {
const baseDevice = this.deviceHandles[handle];
baseDevice.discoverServices([], this.checkForError(errorFn, services => {
const discovered = [];
services.forEach(serviceInfo => {
const serviceUUID = getCanonicalUUID(serviceInfo.uuid);
if (serviceUUIDs.length === 0 || serviceUUIDs.indexOf(serviceUUID) >= 0) {
if (!this.serviceHandles[serviceUUID]) this.serviceHandles[serviceUUID] = serviceInfo;
discovered.push({
uuid: serviceUUID,
primary: true
});
}
});
completeFn(discovered);
}));
}
public discoverIncludedServices(handle: string, serviceUUIDs: Array<string>, completeFn: (services: Array<Partial<BluetoothRemoteGATTService>>) => void, errorFn?: (errorMsg: string) => void): void {
const serviceInfo = this.serviceHandles[handle];
serviceInfo.discoverIncludedServices([], this.checkForError(errorFn, services => {
const discovered = [];
services.forEach(service => {
const serviceUUID = getCanonicalUUID(service.uuid);
if (serviceUUIDs.length === 0 || serviceUUIDs.indexOf(serviceUUID) >= 0) {
if (!this.serviceHandles[serviceUUID]) this.serviceHandles[serviceUUID] = service;
discovered.push({
uuid: serviceUUID,
primary: false
});
}
}, this);
completeFn(discovered);
}));
}
public discoverCharacteristics(handle: string, characteristicUUIDs: Array<string>, completeFn: (characteristics: Array<Partial<BluetoothRemoteGATTCharacteristic>>) => void, errorFn?: (errorMsg: string) => void): void {
const serviceInfo = this.serviceHandles[handle];
serviceInfo.discoverCharacteristics([], this.checkForError(errorFn, characteristics => {
const discovered = [];
characteristics.forEach(characteristicInfo => {
const charUUID = getCanonicalUUID(characteristicInfo.uuid);
if (characteristicUUIDs.length === 0 || characteristicUUIDs.indexOf(charUUID) >= 0) {
if (!this.characteristicHandles[charUUID]) this.characteristicHandles[charUUID] = characteristicInfo;
discovered.push({
uuid: charUUID,
properties: {
broadcast: (characteristicInfo.properties.indexOf("broadcast") >= 0),
read: (characteristicInfo.properties.indexOf("read") >= 0),
writeWithoutResponse: (characteristicInfo.properties.indexOf("writeWithoutResponse") >= 0),
write: (characteristicInfo.properties.indexOf("write") >= 0),
notify: (characteristicInfo.properties.indexOf("notify") >= 0),
indicate: (characteristicInfo.properties.indexOf("indicate") >= 0),
authenticatedSignedWrites: (characteristicInfo.properties.indexOf("authenticatedSignedWrites") >= 0),
reliableWrite: (characteristicInfo.properties.indexOf("reliableWrite") >= 0),
writableAuxiliaries: (characteristicInfo.properties.indexOf("writableAuxiliaries") >= 0)
}
});
characteristicInfo.on("data", (data, isNotification) => {
if (isNotification === true && typeof this.charNotifies[charUUID] === "function") {
const dataView = this.bufferToDataView(data);
this.charNotifies[charUUID](dataView);
}
});
}
}, this);
completeFn(discovered);
}));
}
public discoverDescriptors(handle: string, descriptorUUIDs: Array<string>, completeFn: (descriptors: Array<Partial<BluetoothRemoteGATTDescriptor>>) => void, errorFn?: (errorMsg: string) => void): void {
const characteristicInfo = this.characteristicHandles[handle];
characteristicInfo.discoverDescriptors(this.checkForError(errorFn, descriptors => {
const discovered = [];
descriptors.forEach(descriptorInfo => {
const descUUID = getCanonicalUUID(descriptorInfo.uuid);
if (descriptorUUIDs.length === 0 || descriptorUUIDs.indexOf(descUUID) >= 0) {
const descHandle = characteristicInfo.uuid + "-" + descriptorInfo.uuid;
if (!this.descriptorHandles[descHandle]) this.descriptorHandles[descHandle] = descriptorInfo;
discovered.push({
uuid: descUUID
});
}
}, this);
completeFn(discovered);
}));
}
public readCharacteristic(handle: string, completeFn: (value: DataView) => void, errorFn?: (errorMsg: string) => void): void {
this.characteristicHandles[handle].read(this.checkForError(errorFn, data => {
const dataView = this.bufferToDataView(data);
completeFn(dataView);
}));
}
public writeCharacteristic(handle: string, value: DataView, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
const buffer = this.dataViewToBuffer(value);
const characteristic = this.characteristicHandles[handle];
// writeWithoutResponse and authenticatedSignedWrites don't require a response
const withoutResponse = characteristic.properties.indexOf("writeWithoutResponse") >= 0
|| characteristic.properties.indexOf("authenticatedSignedWrites") >= 0;
// Add a small delay for writing without response when not on MacOS
const delay = (this.os !== "darwin" && withoutResponse) ? 25 : null;
characteristic.write(buffer, withoutResponse, this.checkForError(errorFn, completeFn, delay));
}
public enableNotify(handle: string, notifyFn: (value: DataView) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
if (this.charNotifies[handle]) {
this.charNotifies[handle] = notifyFn;
return completeFn();
}
this.characteristicHandles[handle].once("notify", state => {
if (state !== true) return errorFn("notify failed to enable");
this.charNotifies[handle] = notifyFn;
completeFn();
});
this.characteristicHandles[handle].notify(true, this.checkForError(errorFn));
}
public disableNotify(handle: string, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
if (!this.charNotifies[handle]) {
return completeFn();
}
this.characteristicHandles[handle].once("notify", state => {
if (state !== false) return errorFn("notify failed to disable");
if (this.charNotifies[handle]) delete this.charNotifies[handle];
completeFn();
});
this.characteristicHandles[handle].notify(false, this.checkForError(errorFn));
}
public readDescriptor(handle: string, completeFn: (value: DataView) => void, errorFn?: (errorMsg: string) => void): void {
this.descriptorHandles[handle].readValue(this.checkForError(errorFn, data => {
const dataView = this.bufferToDataView(data);
completeFn(dataView);
}));
}
public writeDescriptor(handle: string, value: DataView, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
const buffer = this.dataViewToBuffer(value);
this.descriptorHandles[handle].writeValue(buffer, this.checkForError(errorFn, completeFn));
}
}
/**
* @hidden
*/
export const adapter = new NobleAdapter();