-
Notifications
You must be signed in to change notification settings - Fork 238
/
ble.d.ts
732 lines (630 loc) · 19.3 KB
/
ble.d.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
* Copyright (c) 2022 Richard Lea
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/
type ManufacturerSpec = {
/**
* The `identifier` property is a number corresponding to the
* _Company Identifier Code_.
*/
identifier: string | number,
/**
* The `data` property is an array of numbers corresponding
* to additional manufacturer specific data.
*/
data: number[],
};
declare module "bleclient" {
import { GAP } from "gap";
import { Client, Characteristic } from "gatt";
import { IOCapability } from "sm";
type gapScanFilterPolicies =
typeof GAP.ScanFilterPolicy.NONE |
typeof GAP.ScanFilterPolicy.NOT_RESOLVED_DIRECTED |
typeof GAP.ScanFilterPolicy.WHITELIST |
typeof GAP.ScanFilterPolicy.WHITELIST_NOT_RESOLVED_DIRECTED;
/**
* The `BLEClient` class provides access to the BLE client features.
*/
export class BLEClient {
securityParameters: {
encryption?: boolean,
bonding?: boolean,
mitm?: boolean,
ioCapability?: IOCapability,
};
/**
* Applications must wait for the `onReady` callback before calling
* other `BLEClient` functions
*/
onReady(): void
/**
* The `startScanning` function enables scanning for nearby peripherals.
*
* @param params If params is not provided, the default scan properties are used.
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#startscanningparams
*/
startScanning(params?: {
active: boolean,
duplicates: boolean,
filterPolicy: gapScanFilterPolicies,
interval: number,
window: number,
}): void;
/**
* Called when one or more times for each peripheral device discovered.
* @param device
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#ondiscovereddevice
*/
onDiscovered(device: Client): void;
/**
* Disables scanning for nearby peripherals.
*/
stopScanning(): void;
/**
* Initiates a connection request between the `BLEClient` and a target peripheral `device`.
* @param device
*/
connect(device: Client): void;
onConnected(device: Client): void;
onDisconnected(): void;
onCharacteristicNotification(characteristic: Characteristic, buffer: number[]): void;
}
export { BLEClient as default };
}
declare module "bleserver" {
import { GAP } from "gap";
import { Bytes } from "btutils";
import { Characteristic, Client } from "gatt";
import { IOCapability } from "sm";
type gapScanFilterPolicies =
typeof GAP.ScanFilterPolicy.NONE |
typeof GAP.ScanFilterPolicy.NOT_RESOLVED_DIRECTED |
typeof GAP.ScanFilterPolicy.WHITELIST |
typeof GAP.ScanFilterPolicy.WHITELIST_NOT_RESOLVED_DIRECTED;
type AdvertisementData = {
/**
* The advertised flags value.
*/
flags?: number;
/**
* Array of UUID objects corresponding to _Incomplete List
* of 16 bit Service UUIDs_
*/
incompleteUUID16List?: Bytes[],
/**
* Array of UUID objects corresponding to _Complete List of
* 16 bit Service UUIDs_
*/
completeUUID16List?: Bytes[],
/**
* Array of UUID objects corresponding to _Incomplete List of
* 128 bit Service UUIDs_
*/
incompleteUUID128List?: Bytes[],
/**
* Array of UUID objects corresponding to _Complete List of
* 128 bit Service UUIDs_
*/
completeUUID128List?: Bytes[],
/**
* String corresponding to the _Shortened Local Name_.
*/
shortName?: string,
/**
* String corresponding to the _Complete Local Name_.
*/
completeName?: string,
/**
* Object corresponding to the _Manufacturer Specific Data_.
*/
manufacturerSpecific?: ManufacturerSpec,
/**
* Number corresponding to the TX Power Level.
*/
txPowerLevel?: number,
/**
* Object corresponding to the _Slave Connection Interval Range_.
*/
connectionInterval?: {
/**
* A number corresponding to the minimum connection interval value.
*/
intervalMin: number,
/**
* A number corresponding to the maximum connection interval value.
*/
intervalMax: number,
},
/**
* Array of UUID objects corresponding to the _List of 16 bit Service
* Solicitation UUIDs_.
*/
solicitationUUID16List?: Bytes[],
/**
* Array of UUID objects corresponding to the _List of 128 bit Service
* Solicitation UUIDs_.
*/
solicitationUUID128List?: Bytes[],
/**
* Object corresponding to the _Service Data - 16 bit UUID_.
*/
serviceDataUUID16?: {
/**
* An object corresponding to the 16-bit Service UUID.
*/
uuid: Bytes,
/**
* An array of numbers corresponding to additional service data.
*/
data: number[],
},
/**
* Object corresponding to the _Service Data - 128 bit UUID_.
*/
serviceDataUUID128?: {
/**
* An object corresponding to the 128-bit Service UUID.
*/
uuid: Bytes,
/**
* An array of numbers corresponding to additional service data.
*/
data: number[],
},
/**
* Number corresponding to the _Appearance_.
*/
appearance?: number,
/**
* Address object corresponding to the _Public Target Address_.
*/
publicAddress?: Bytes,
/**
* Address object corresponding to the _Random Target Address_.
*/
randomAddress?: Bytes,
/**
* Number corresponding to the _Advertising Interval_.
*/
advertisingInterval?: number,
/**
* Number corresponding to the _LE Role_.
*/
role?: number,
/**
* String corresponding to the _Uniform Resource Identifier_.
*/
uri?: string,
};
/**
* Provides access to the BLE server features.
*/
export class BLEServer {
/**
* Configures the device security requirements and I/O capabilities.
*/
set securityParameters(params: {
encryption?: boolean,
bonding?: boolean,
mitm?: boolean,
ioCapability?: IOCapability,
});
/**
* set/get the Bluetooth peripheral device name.
*/
deviceName: string;
/**
* The Bluetooth peripheral's local address as a
* `Bytes` object.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#localaddress
*/
readonly localAddress: Bytes;
/**
* Applications must wait for the `onReady` callback
* before calling other `BLEServer` functions.
*/
onReady(): void;
/**
* Starts broadcasting advertisement and scan response packets.
* The function is also used to configure discoverable and connectable modes.
*
* @param params
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#startadvertisingparams
*/
startAdvertising(params: {
advertisingData: AdvertisementData,
connectable?: boolean,
discoverable?: boolean,
fast?: boolean,
filterPolicy?: gapScanFilterPolicies,
scanResponseData?: any,
}): void;
/**
* Stops broadcasting Bluetooth advertisements.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#stopadvertising
*/
stopAdvertising(): void;
/**
* Send a characteristic value change notification to the connected client.
*
* @param characteristic
* @param value
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#notifyvaluecharacteristic-value
*/
notifyValue(characteristic: Characteristic, value: any): void;
/**
* Called when a client enables notifications on the `characteristic`.
*
* @param characteristic
*/
onCharacteristicNotifyEnabled(characteristic: Characteristic): void;
/**
* Called when a client writes a service characteristic value on demand.
*
* @param characteristic
* @param value
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#oncharacteristicwrittencharacteristic-value
*/
onCharacteristicWritten(characteristic: Characteristic, value: any): void;
/**
* Called when a client reads a service characteristic value on demand.
*
* @param characteristic
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#oncharacteristicreadcharacteristic
*/
onCharacteristicRead(characteristic: Characteristic): Uint8Array | number[] | number | void;
/**
* Terminate the BLE client connection.
*/
disconnect(): void;
/**
* Called when a client connects to the `BLEServer`.
*
* @param device
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#ondisconnecteddevice-1
*/
onConnected(device: Client): void;
/**
* Called when the client connection is closed.
*
* @param device
*/
onDisconnected(device: Client): void;
/**
* Terminate any BLE client connection and release all BLE resources.
*/
close(): void;
}
export { BLEServer as default };
}
declare module "btutils" {
export interface Bytes extends ArrayBuffer {
/**
* The `equals` function returns `true` if the instance
* ArrayBuffer data equals the data contained in `buffer`.
*
* @param bytes
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#equalsbuffer
*/
equals(bytes: Bytes): boolean;
}
/**
* Provides Accessor functions to read common advertisement and
* scan response data types as JavaScript properties.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#class-advertisement
*/
export interface Advertisement {
/**
* Contains the raw advertisement data bytes.
*/
buffer: ArrayBuffer;
/**
* The advertised complete local name.
*/
completeName: string;
/**
* The advertised shortened local name.
*/
shortName: string;
/**
* An object containing the advertised manufacturer specific data.
*/
manufacturerSpecific: ManufacturerSpec;
/**
* The advertised flags value.
*/
flags: number;
/**
* The advertised complete 16-bit UUID list.
*/
completeUUID16List: Array<string | number>;
/**
* The advertised incomplete 16-bit UUID list.
*/
incompleteUUID16List: Array<string | number>;
}
/**
* Convert a Bluetooth UUID expressed as a hex string to a `Bytes` instance.
* @param strings
*/
export function uuid(...strings: string[]): Bytes;
/**
* Convert a Bluetooth address expressed as a hex string to a `Bytes` instance.
* @param strings
*/
export function address(...strings: string[]): Bytes;
}
declare module "gatt" {
import { Bytes, Advertisement } from "btutils";
import { GAP } from "gap";
type getAddressType = (typeof GAP.AddressType);
export interface Client {
connection: number,
address: Bytes,
addressType: getAddressType,
scanResponse: Advertisement,
rssi: number,
/**
* Read the connected peripheral's signal strength.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#readrssi
*/
readRSSI(): void;
/**
* Discover a single GATT primary service by UUID.
*
* @param uuid
*/
discoverPrimaryService(uuid: Bytes): void;
/**
* Discover all the peripheral's GATT primary services.
* Discovered services are returned to the `onServices` callback.
*/
discoverAllPrimaryServices(): void;
/**
* Finds and returns the service identified by `uuid`.
*
* @param uuid
*/
findServiceByUUID(uuid: Bytes): void;
/**
* Called when service discovery completes.
* If `findServiceByUUID` was called to find a single service,
* the `services` array contains the single service found.
*
* @param services
*/
onServices(services: Service[]): void;
/**
* Terminates the peripheral function.
*/
close(): void;
}
/**
* A single Peripheral GATT service.
*/
export interface Service {
connection: number;
uuid: Bytes;
start: number;
end: number;
characteristics: Characteristic[];
/**
* Discover all the service characteristics. Discovered characteristics
* are returned to the `onCharacteristics` callback.
*/
discoverAllCharacteristics(): void;
/**
* Discover a single service characteristic by UUID.
*
* @param uuid
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#discovercharacteristicuuid
*/
discoverCharacteristic(uuid: Bytes): void;
/**
* Called when characteristic discovery completes.
* If `findCharacteristicByUUID` was called to find a single characteristic,
* the `characteristics` array contains the single characteristic found.
*
* @param characteristics
*/
onCharacteristics(characteristics: Characteristic[]): void;
}
/**
* A single service Characteristic.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#class-characteristic
*/
export interface Characteristic {
connection: number;
uuid: Bytes;
service: Service;
handle: number;
name: string;
type: string;
descriptors: Descriptor[];
/**
* Discover all the characteristic's descriptors.
* Discovered descriptors are returned to the `onDescriptors` callback.
*/
discoverAllDescriptors(): void;
/**
* Called when descriptor discovery completes.
*
* @param descriptors
*/
onDescriptors(descriptors: Descriptor[]): void;
/**
* Enable characteristic value change notifications.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#enablenotifications
*/
disableNotifications(): void;
}
/**
* A single Characteristic descriptor.
*
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#class-descriptor
*/
export interface Descriptor {
connection: number;
uuid: string;
characteristic: Characteristic;
handle: number;
name: string;
type: string;
/**
* Read a descriptor value on demand.
*
* @param auth
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#readvalueauth-1
*/
readValue(auth?: number): void;
}
}
declare module "connection" {
export class Connection {
disconnect(): void;
}
}
declare module "gap" {
const enum gapAddressTypes {
PUBLIC = 0,
RANDOM = 1,
RPA_PUBLIC = 2,
RPA_RANDOM = 3,
}
export const GAP: {
readonly SCAN_FAST_INTERVAL: 0x0030, // TGAP(scan_fast_interval) 30ms to 60ms
readonly SCAN_FAST_WINDOW: 0x0030, // TGAP(scan_fast_window) 30ms
readonly SCAN_SLOW_INTERVAL1: 0x0800, // TGAP(scan_slow_interval1) 1.28s
readonly SCAN_SLOW_WINDOW1: 0x0012, // TGAP(scan_slow_window1) 11.25ms
readonly SCAN_SLOW_INTERVAL2: 0x1000, // TGAP(scan_slow_interval2) 2.56s
readonly SCAN_SLOW_WINDOW2: 0x0024, // TGAP(scan_slow_window2) 22.5ms
readonly ADV_FAST_INTERVAL1: { // TGAP(adv_fast_interval1) 30ms to 60ms
min: 0x0030,
max: 0x0060
},
readonly ADV_FAST_INTERVAL2: { // TGAP(adv_fast_interval2) 100ms to 150ms
min: 0x00A0,
max: 0x00F0,
},
readonly ADV_SLOW_INTERVAL: { // TGAP(adv_slow_interval) 1s to 1.2s
min: 0x0640,
max: 0x0780,
},
readonly ADType: {
/* Service UUID */
INCOMPLETE_UUID16_LIST: 0x02,
COMPLETE_UUID16_LIST: 0x03,
INCOMPLETE_UUID128_LIST: 0x06,
COMPLETE_UUID128_LIST: 0x07,
/* Local Name */
SHORTENED_LOCAL_NAME: 0x08,
COMPLETE_LOCAL_NAME: 0x09,
/* Flags */
FLAGS: 0x01,
/* Manufacturer Specific Data */
MANUFACTURER_SPECIFIC_DATA: 0xFF,
/* TX Power Level */
TX_POWER_LEVEL: 0x0A,
SLAVE_CONNECTION_INTERVAL_RANGE: 0x12,
/* Service Solicitation */
SOLICITATION_UUID16_LIST: 0x14,
SOLICITATION_UUID128_LIST: 0x15,
/* Service Data */
SERVICE_DATA_UUID16: 0x16,
SERVICE_DATA_UUID128: 0x21,
/* Appearance */
APPEARANCE: 0x19,
/* Public Target Address */
PUBLIC_TARGET_ADDRESS: 0x17,
/* Random Target Address */
RANDOM_TARGET_ADDRESS: 0x18,
/* Advertising Interval */
ADVERTISING_INTERVAL: 0x1A,
/* LE Bluetooth Device Address */
LE_BLUETOOTH_DEVICE_ADDRESS: 0x1B,
/* LE Role */
LE_ROLE: 0x1C,
/* URI */
URI: 0x24,
},
MAX_AD_LENGTH: 31,
ADFlag: {
LE_LIMITED_DISCOVERABLE_MODE: 0x01,
LE_GENERAL_DISCOVERABLE_MODE: 0x02,
NO_BR_EDR: 0x04,
LE_BR_EDR_CONTROLLER: 0x08,
LE_BR_EDR_HOST: 0x10,
},
AddressType: typeof gapAddressTypes,
ScanFilterPolicy: {
NONE: 0,
WHITELIST: 1,
NOT_RESOLVED_DIRECTED: 2,
WHITELIST_NOT_RESOLVED_DIRECTED: 3,
},
AdvFilterPolicy: {
NONE: 0,
WHITELIST_SCANS: 1,
WHITELIST_CONNECTIONS: 2,
WHITELIST_SCANS_CONNECTIONS: 3
},
}
export { GAP as default };
}
declare module "sm" {
import { GAP } from "gap";
type getAddressType = (typeof GAP.AddressType);
/**
* @url https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#class-sm
*/
export const enum IOCapability {
NoInputNoOutput = 0,
DisplayOnly = 1,
KeyboardOnly = 2,
KeyboardDisplay = 3,
DisplayYesNo = 4,
}
/**
* Provides objects used to configure BLE client and server security requirements
* and device capabilities.
*/
export class SM {
/**
* Delete all bonding information and encryption keys
* from persistent storage.
*
* https://github.com/Moddable-OpenSource/moddable/blob/public/documentation/network/ble/ble.md#deleteallbondings
*/
static deleteAllBondings(): void;
/**
* Delete stored bonding information for the peer device with the provided
* `address` and `addressType`.
*
* @param address Contains peer device Bluetooth address
* @param addressType Peer device Bluetooth address type.
*/
static deleteBonding(address: ArrayBuffer, addressType: getAddressType): void;
}
}