@stoprocent/noble 1.19.0 → 2.0.0

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.
Files changed (97) hide show
  1. package/README.md +393 -650
  2. package/examples/advertisement-discovery.js +57 -48
  3. package/examples/connect-address.js +59 -34
  4. package/examples/echo.js +59 -69
  5. package/examples/enter-exit.js +55 -49
  6. package/examples/multiple-bindings.js +53 -0
  7. package/examples/peripheral-explorer-async.js +39 -21
  8. package/examples/peripheral-explorer.ts +52 -0
  9. package/index.d.ts +249 -209
  10. package/index.js +4 -1
  11. package/jest.config.js +4 -0
  12. package/lib/characteristic.js +153 -127
  13. package/lib/{win/src/callbacks.h → common/include/Emit.h} +17 -14
  14. package/lib/common/include/Peripheral.h +31 -0
  15. package/lib/common/include/ThreadSafeCallback.h +95 -0
  16. package/lib/{win/src/callbacks.cc → common/src/Emit.cc} +111 -68
  17. package/lib/descriptor.js +57 -54
  18. package/lib/hci-socket/acl-stream.js +2 -4
  19. package/lib/hci-socket/bindings.js +96 -73
  20. package/lib/hci-socket/gap.js +2 -3
  21. package/lib/hci-socket/gatt.js +2 -5
  22. package/lib/hci-socket/hci.js +19 -7
  23. package/lib/hci-socket/signaling.js +2 -3
  24. package/lib/hci-socket/smp.js +2 -3
  25. package/lib/hci-socket/vs.js +1 -0
  26. package/lib/mac/binding.gyp +5 -7
  27. package/lib/mac/bindings.js +1 -3
  28. package/lib/mac/src/ble_manager.h +1 -8
  29. package/lib/mac/src/ble_manager.mm +87 -44
  30. package/lib/mac/src/napi_objc.h +1 -0
  31. package/lib/mac/src/napi_objc.mm +0 -6
  32. package/lib/mac/src/noble_mac.h +5 -3
  33. package/lib/mac/src/noble_mac.mm +99 -57
  34. package/lib/mac/src/objc_cpp.h +3 -2
  35. package/lib/mac/src/objc_cpp.mm +0 -6
  36. package/lib/noble.js +579 -488
  37. package/lib/peripheral.js +171 -174
  38. package/lib/resolve-bindings.js +37 -30
  39. package/lib/service.js +58 -55
  40. package/lib/win/binding.gyp +4 -11
  41. package/lib/win/bindings.js +1 -3
  42. package/lib/win/src/ble_manager.cc +291 -166
  43. package/lib/win/src/ble_manager.h +11 -13
  44. package/lib/win/src/napi_winrt.cc +1 -7
  45. package/lib/win/src/napi_winrt.h +1 -1
  46. package/lib/win/src/noble_winrt.cc +88 -61
  47. package/lib/win/src/noble_winrt.h +5 -3
  48. package/lib/win/src/notify_map.cc +0 -7
  49. package/lib/win/src/notify_map.h +1 -8
  50. package/lib/win/src/peripheral_winrt.cc +29 -11
  51. package/lib/win/src/peripheral_winrt.h +1 -1
  52. package/lib/win/src/radio_watcher.cc +79 -69
  53. package/lib/win/src/radio_watcher.h +30 -11
  54. package/lib/win/src/winrt_cpp.cc +1 -1
  55. package/lib/win/src/winrt_cpp.h +3 -0
  56. package/package.json +14 -17
  57. package/prebuilds/darwin-x64+arm64/@stoprocent+noble.node +0 -0
  58. package/prebuilds/win32-ia32/@stoprocent+noble.node +0 -0
  59. package/prebuilds/win32-x64/@stoprocent+noble.node +0 -0
  60. package/test/lib/characteristic.test.js +202 -322
  61. package/test/lib/descriptor.test.js +62 -95
  62. package/test/lib/hci-socket/acl-stream.test.js +112 -108
  63. package/test/lib/hci-socket/bindings.test.js +576 -365
  64. package/test/lib/hci-socket/hci.test.js +442 -473
  65. package/test/lib/hci-socket/signaling.test.js +45 -48
  66. package/test/lib/hci-socket/smp.test.js +144 -142
  67. package/test/lib/hci-socket/vs.test.js +193 -18
  68. package/test/lib/peripheral.test.js +492 -322
  69. package/test/lib/resolve-bindings.test.js +207 -82
  70. package/test/lib/service.test.js +79 -88
  71. package/test/noble.test.js +381 -1085
  72. package/.editorconfig +0 -11
  73. package/.nycrc.json +0 -4
  74. package/codecov.yml +0 -5
  75. package/examples/cache-gatt-discovery.js +0 -198
  76. package/examples/cache-gatt-reconnect.js +0 -164
  77. package/examples/ext-advertisement-discovery.js +0 -65
  78. package/examples/peripheral-explorer.js +0 -225
  79. package/examples/pizza/central.js +0 -194
  80. package/examples/pizza/pizza.js +0 -60
  81. package/examples/test/test.custom.js +0 -131
  82. package/examples/uart-bind-params.js +0 -28
  83. package/lib/distributed/bindings.js +0 -326
  84. package/lib/mac/src/callbacks.cc +0 -222
  85. package/lib/mac/src/callbacks.h +0 -84
  86. package/lib/mac/src/peripheral.h +0 -23
  87. package/lib/resolve-bindings-web.js +0 -9
  88. package/lib/webbluetooth/bindings.js +0 -368
  89. package/lib/websocket/bindings.js +0 -321
  90. package/lib/win/src/peripheral.h +0 -23
  91. package/test/lib/distributed/bindings.test.js +0 -918
  92. package/test/lib/webbluetooth/bindings.test.js +0 -190
  93. package/test/lib/websocket/bindings.test.js +0 -456
  94. package/test/mocha.setup.js +0 -0
  95. package/with-bindings.js +0 -5
  96. package/with-custom-binding.js +0 -6
  97. package/ws-slave.js +0 -404
@@ -0,0 +1,52 @@
1
+ import { withBindings } from "@stoprocent/noble";
2
+
3
+ const address = process.argv[2]?.toLowerCase() || '11:22:44:55:99:77';
4
+ const noble = withBindings('default');
5
+
6
+ await noble.waitForPoweredOnAsync();
7
+ console.log('noble started');
8
+
9
+ for await (const peripheral of noble.discoverAsync()) {
10
+ if ([peripheral.id, peripheral.address].includes(address) === false) {
11
+ continue;
12
+ }
13
+
14
+ await noble.stopScanningAsync();
15
+ console.log('peripheral', peripheral.address || peripheral.id);
16
+ console.log('discovered', peripheral.advertisement);
17
+
18
+ await peripheral.connectAsync();
19
+ console.log('connected');
20
+
21
+ const { services } = await peripheral.discoverAllServicesAndCharacteristicsAsync();
22
+
23
+ for (const service of services) {
24
+ console.log('Service\x1b[34m', service.uuid, '\x1b[0m');
25
+ for (const characteristic of service.characteristics) {
26
+
27
+ console.log(' | Characteristic\x1b[32m', characteristic.uuid, '\x1b[0m');
28
+ console.log(' | | Type:', characteristic.type);
29
+ console.log(' | | Properties:', characteristic.properties);
30
+
31
+ if (characteristic.properties.includes('read')) {
32
+ let value = await characteristic.readAsync();
33
+ console.log(` | | Value: 0x${value.toString('hex')}`);
34
+ }
35
+
36
+ const descriptors = await characteristic.discoverDescriptorsAsync();
37
+ for (const descriptor of descriptors) {
38
+ console.log(' | | Descriptor\x1b[33m', descriptor.uuid, '\x1b[0m');
39
+ const value = await descriptor.readValueAsync();
40
+ console.log(` | | | Value: 0x${value.toString('hex')}`);
41
+ }
42
+ }
43
+ }
44
+
45
+ await peripheral.disconnectAsync();
46
+ console.log('disconnected');
47
+
48
+ break;
49
+ }
50
+
51
+ noble.stop();
52
+ console.log('noble stopped');
package/index.d.ts CHANGED
@@ -1,222 +1,262 @@
1
- // Type definitions for noble
2
- // Project: https://github.com/sandeepmistry/noble
3
- // Definitions by: Seon-Wook Park <https://github.com/swook>
4
- // Shantanu Bhadoria <https://github.com/shantanubhadoria>
5
- // Luke Libraro <https://github.com/lukel99>
6
- // Dan Chao <https://github.com/bioball>
7
- // Michal Lower <https://github.com/keton>
8
- // Rob Moran <https://github.com/thegecko>
9
- // Clayton Kucera <https://github.com/claytonkucera>
10
- // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11
-
12
1
  /// <reference types="node" />
2
+ declare module '@stoprocent/bluetooth-hci-socket' {
3
+ type DriverType = any; // Fallback type
4
+ interface BindParams {
5
+ [key: string]: any;
6
+ }
7
+ }
13
8
 
14
- import events = require("events");
9
+ declare module '@stoprocent/noble' {
10
+ import { EventEmitter } from 'events';
15
11
 
16
- export interface ConnectOptions {
17
- addressType?: 'public' | 'random';
18
- minInterval?: number;
19
- maxInterval?: number;
20
- latency?: number;
21
- timeout?: number;
22
- }
12
+ export type AdapterState = 'poweredOn' | 'poweredOff' | 'unauthorized' | 'unsupported' | 'unknown' | 'resetting';
13
+
14
+ export type PeripheralState = 'error' | 'connecting' | 'connected' | 'disconnecting' | 'disconnected';
23
15
 
24
- export declare function waitForPoweredOn(timeout?: number): Promise<void>;
25
- /**
26
- * @deprecated
27
- */
28
- export declare function startScanning(callback?: (error?: Error) => void): void;
29
- /**
30
- * @deprecated
31
- */
32
- export declare function startScanning(serviceUUIDs: string[], callback?: (error?: Error) => void): void;
33
- export declare function startScanning(serviceUUIDs?: string[], allowDuplicates?: boolean, callback?: (error?: Error) => void): void;
34
- export declare function startScanningAsync(serviceUUIDs?: string[], allowDuplicates?: boolean): Promise<void>;
35
- export declare function stopScanning(callback?: () => void): void;
36
- export declare function stopScanningAsync(): Promise<void>;
37
- export declare function connect(peripheralUuid: string, options?: ConnectOptions, callback?: (error: Error | undefined, peripheral: Peripheral) => void): void;
38
- export declare function connectAsync(peripheralUuid: string, options?: ConnectOptions): Promise<Peripheral>;
39
- export declare function cancelConnect(peripheralUuid: string, options?: object): void;
40
- export declare function reset(): void;
41
- export declare function stop(): void;
42
-
43
- export declare function setAddress(address: string): void;
44
-
45
- export declare function on(event: "stateChange", listener: (state: string) => void): events.EventEmitter;
46
- export declare function on(event: "scanStart", listener: () => void): events.EventEmitter;
47
- export declare function on(event: "scanStop", listener: () => void): events.EventEmitter;
48
- export declare function on(event: "discover", listener: (peripheral: Peripheral) => void): events.EventEmitter;
49
- export declare function on(event: string, listener: Function): events.EventEmitter;
50
-
51
- export declare function once(event: "stateChange", listener: (state: string) => void): events.EventEmitter;
52
- export declare function once(event: "scanStart", listener: () => void): events.EventEmitter;
53
- export declare function once(event: "scanStop", listener: () => void): events.EventEmitter;
54
- export declare function once(event: "discover", listener: (peripheral: Peripheral) => void): events.EventEmitter;
55
- export declare function once(event: string, listener: Function): events.EventEmitter;
56
-
57
- export declare function removeListener(event: "stateChange", listener: (state: string) => void): events.EventEmitter;
58
- export declare function removeListener(event: "scanStart", listener: () => void): events.EventEmitter;
59
- export declare function removeListener(event: "scanStop", listener: () => void): events.EventEmitter;
60
- export declare function removeListener(event: "discover", listener: (peripheral: Peripheral) => void): events.EventEmitter;
61
- export declare function removeListener(event: string, listener: Function): events.EventEmitter;
62
-
63
- export declare function removeAllListeners(event?: string): events.EventEmitter;
64
-
65
- export var state: "unknown" | "resetting" | "unsupported" | "unauthorized" | "poweredOff" | "poweredOn";
66
- /**
67
- * @deprecated Use `state` instead.
68
- */
69
- export var _state: "unknown" | "resetting" | "unsupported" | "unauthorized" | "poweredOff" | "poweredOn";
70
-
71
- export var _bindings: any;
72
-
73
- export interface ServicesAndCharacteristics {
74
- services: Service[];
75
- characteristics: Characteristic[];
76
- }
16
+ export type PeripheralAddressType = 'public' | 'random';
77
17
 
78
- export declare class Peripheral extends events.EventEmitter {
79
- id: string;
80
- uuid: string;
81
- address: string;
82
- addressType: string;
83
- connectable: boolean;
84
- advertisement: Advertisement;
85
- rssi: number;
86
- mtu: number | null;
87
- services: Service[];
88
- state: 'error' | 'connecting' | 'connected' | 'disconnecting' | 'disconnected';
89
-
90
- connect(callback?: (error: string) => void): void;
91
- connectAsync(): Promise<void>;
92
- disconnect(callback?: () => void): void;
93
- disconnectAsync(): Promise<void>;
94
- updateRssi(callback?: (error: string, rssi: number) => void): void;
95
- updateRssiAsync(): Promise<number>;
96
- discoverServices(): void;
97
- discoverServicesAsync(): Promise<Service[]>;
98
- discoverServices(serviceUUIDs: string[], callback?: (error: string, services: Service[]) => void): void;
99
- discoverServicesAsync(serviceUUIDs: string[]): Promise<Service[]>;
100
- discoverAllServicesAndCharacteristics(callback?: (error: string, services: Service[], characteristics: Characteristic[]) => void): void;
101
- discoverAllServicesAndCharacteristicsAsync(): Promise<ServicesAndCharacteristics>;
102
- discoverSomeServicesAndCharacteristics(serviceUUIDs: string[], characteristicUUIDs: string[], callback?: (error: string, services: Service[], characteristics: Characteristic[]) => void): void;
103
- discoverSomeServicesAndCharacteristicsAsync(serviceUUIDs: string[], characteristicUUIDs: string[]): Promise<ServicesAndCharacteristics>;
104
- cancelConnect(options?: object): void;
105
-
106
- readHandle(handle: number, callback: (error: string, data: Buffer) => void): void;
107
- readHandleAsync(handle: number): Promise<Buffer>;
108
- writeHandle(handle: number, data: Buffer, withoutResponse: boolean, callback: (error: string) => void): void;
109
- writeHandleAsync(handle: number, data: Buffer, withoutResponse: boolean): Promise<void>;
110
- toString(): string;
111
-
112
- on(event: "connect", listener: (error: string) => void): this;
113
- on(event: "disconnect", listener: (error: string) => void): this;
114
- on(event: "rssiUpdate", listener: (rssi: number) => void): this;
115
- on(event: "servicesDiscover", listener: (services: Service[]) => void): this;
116
- on(event: string, listener: Function): this;
117
-
118
- once(event: "connect", listener: (error: string) => void): this;
119
- once(event: "disconnect", listener: (error: string) => void): this;
120
- once(event: "rssiUpdate", listener: (rssi: number) => void): this;
121
- once(event: "servicesDiscover", listener: (services: Service[]) => void): this;
122
- once(event: string, listener: Function): this;
123
- }
18
+ export type PeripheralIdOrAddress = string;
124
19
 
125
- export interface Advertisement {
126
- localName: string;
127
- serviceData: Array<{
128
- uuid: string,
129
- data: Buffer
130
- }>;
131
- txPowerLevel: number;
132
- manufacturerData: Buffer;
133
- serviceUuids: string[];
134
- }
20
+ export type CharacteristicProperty = 'read' | 'write' | 'indicate' | 'notify' | 'writeWithoutResponse';
135
21
 
136
- export declare class Service extends events.EventEmitter {
137
- uuid: string;
138
- name: string;
139
- type: string;
140
- includedServiceUuids: string[];
141
- characteristics: Characteristic[];
142
-
143
- discoverIncludedServices(): void;
144
- discoverIncludedServicesAsync(): Promise<string[]>;
145
- discoverIncludedServices(serviceUUIDs: string[], callback?: (error: string, includedServiceUuids: string[]) => void): void;
146
- discoverIncludedServicesAsync(serviceUUIDs: string[]): Promise<string[]>;
147
- discoverCharacteristics(): void;
148
- discoverCharacteristicsAsync(): Promise<Characteristic[]>;
149
- discoverCharacteristics(characteristicUUIDs: string[], callback?: (error: string, characteristics: Characteristic[]) => void): void;
150
- discoverCharacteristicsAsync(characteristicUUIDs: string[]): Promise<Characteristic[]>;
151
- toString(): string;
152
-
153
- on(event: "includedServicesDiscover", listener: (includedServiceUuids: string[]) => void): this;
154
- on(event: "characteristicsDiscover", listener: (characteristics: Characteristic[]) => void): this;
155
- on(event: string, listener: Function): this;
156
-
157
- once(event: "includedServicesDiscover", listener: (includedServiceUuids: string[]) => void): this;
158
- once(event: "characteristicsDiscover", listener: (characteristics: Characteristic[]) => void): this;
159
- once(event: string, listener: Function): this;
160
- }
22
+ export interface ConnectOptions {
23
+ addressType?: PeripheralAddressType;
24
+ minInterval?: number;
25
+ maxInterval?: number;
26
+ latency?: number;
27
+ timeout?: number;
28
+ }
161
29
 
162
- export declare class Characteristic extends events.EventEmitter {
163
- uuid: string;
164
- name: string;
165
- type: string;
166
- properties: string[];
167
- descriptors: Descriptor[];
168
-
169
- read(callback?: (error: string, data: Buffer) => void): void;
170
- readAsync(): Promise<Buffer>;
171
- write(data: Buffer, withoutResponse: boolean, callback?: (error: string) => void): void;
172
- writeAsync(data: Buffer, withoutResponse: boolean): Promise<void>;
173
- broadcast(broadcast: boolean, callback?: (error: string) => void): void;
174
- broadcastAsync(broadcast: boolean): Promise<void>;
175
- notify(notify: boolean, callback?: (error: string) => void): void;
176
- notifyAsync(notify: boolean): Promise<void>;
177
- discoverDescriptors(callback?: (error: string, descriptors: Descriptor[]) => void): void;
178
- discoverDescriptorsAsync(): Promise<Descriptor[]>;
179
- toString(): string;
180
- subscribe(callback?: (error: string) => void): void;
181
- subscribeAsync(): Promise<void>;
182
- unsubscribe(callback?: (error: string) => void): void;
183
- unsubscribeAsync(): Promise<void>;
184
-
185
- on(event: "read", listener: (data: Buffer, isNotification: boolean) => void): this;
186
- on(event: "write", withoutResponse: boolean, listener: (error: string) => void): this;
187
- on(event: "broadcast", listener: (state: string) => void): this;
188
- on(event: "notify", listener: (state: string) => void): this;
189
- on(event: "data", listener: (data: Buffer, isNotification: boolean) => void): this;
190
- on(event: "descriptorsDiscover", listener: (descriptors: Descriptor[]) => void): this;
191
- on(event: string, listener: Function): this;
192
- on(event: string, option: boolean, listener: Function): this;
193
-
194
- once(event: "read", listener: (data: Buffer, isNotification: boolean) => void): this;
195
- once(event: "write", withoutResponse: boolean, listener: (error: string) => void): this;
196
- once(event: "broadcast", listener: (state: string) => void): this;
197
- once(event: "notify", listener: (state: string) => void): this;
198
- once(event: "data", listener: (data: Buffer, isNotification: boolean) => void): this;
199
- once(event: "descriptorsDiscover", listener: (descriptors: Descriptor[]) => void): this;
200
- once(event: string, listener: Function): this;
201
- once(event: string, option: boolean, listener: Function): this;
202
- }
30
+ export class Noble extends EventEmitter {
31
+
32
+ constructor(bindings: any);
33
+
34
+ readonly state: AdapterState;
35
+ readonly address: string;
36
+
37
+ waitForPoweredOnAsync(timeout?: number): Promise<void>;
38
+ startScanningAsync(serviceUUIDs?: string[], allowDuplicates?: boolean): Promise<void>;
39
+ stopScanningAsync(): Promise<void>;
40
+ discoverAsync(): AsyncGenerator<Peripheral, void, unknown>;
41
+ connectAsync(idOrAddress: PeripheralIdOrAddress, options?: ConnectOptions): Promise<Peripheral>;
42
+
43
+ startScanning(serviceUUIDs?: string[], allowDuplicates?: boolean, callback?: (error?: Error) => void): void;
44
+ stopScanning(callback?: () => void): void;
45
+ connect(idOrAddress: PeripheralIdOrAddress, options?: ConnectOptions, callback?: (error: Error | undefined, peripheral: Peripheral) => void): void;
46
+ cancelConnect(idOrAddress: PeripheralIdOrAddress, options?: object): void;
47
+ reset(): void;
48
+ stop(): void;
49
+ setAddress(address: string): void;
50
+
51
+ on(event: "stateChange", listener: (state: AdapterState) => void): this;
52
+ on(event: "scanStart", listener: () => void): this;
53
+ on(event: "scanStop", listener: () => void): this;
54
+ on(event: "discover", listener: (peripheral: Peripheral) => void): this;
55
+ on(event: string, listener: Function): this;
56
+
57
+ once(event: "stateChange", listener: (state: AdapterState) => void): this;
58
+ once(event: "scanStart", listener: () => void): this;
59
+ once(event: "scanStop", listener: () => void): this;
60
+ once(event: "discover", listener: (peripheral: Peripheral) => void): this;
61
+ once(event: string, listener: Function): this;
62
+
63
+ removeListener(event: "stateChange", listener: (state: AdapterState) => void): this;
64
+ removeListener(event: "scanStart", listener: () => void): this;
65
+ removeListener(event: "scanStop", listener: () => void): this;
66
+ removeListener(event: "discover", listener: (peripheral: Peripheral) => void): this;
67
+ removeListener(event: string, listener: Function): this;
68
+ }
69
+
70
+ export interface ServicesAndCharacteristics {
71
+ services: Service[];
72
+ characteristics: Characteristic[];
73
+ }
74
+
75
+ export interface PeripheralAdvertisement {
76
+ localName: string;
77
+ serviceData: Array<{
78
+ uuid: string,
79
+ data: Buffer
80
+ }>;
81
+ txPowerLevel: number;
82
+ manufacturerData: Buffer;
83
+ serviceUuids: string[];
84
+ serviceSolicitationUuids: string[];
85
+ }
86
+
87
+ export class Peripheral extends EventEmitter {
88
+ readonly id: string;
89
+ readonly address: string;
90
+ readonly addressType: PeripheralAddressType;
91
+ readonly connectable: boolean;
92
+ readonly advertisement: PeripheralAdvertisement;
93
+ readonly rssi: number;
94
+ readonly mtu: number | null;
95
+ readonly services: Service[];
96
+ readonly state: PeripheralState;
97
+
98
+ /** @deprecated Use id instead */
99
+ readonly uuid: string;
100
+
101
+ connectAsync(): Promise<void>;
102
+ disconnectAsync(): Promise<void>;
103
+ updateRssiAsync(): Promise<number>;
104
+ discoverServicesAsync(): Promise<Service[]>;
105
+ discoverServicesAsync(serviceUUIDs: string[]): Promise<Service[]>;
106
+ discoverAllServicesAndCharacteristicsAsync(): Promise<ServicesAndCharacteristics>;
107
+ discoverSomeServicesAndCharacteristicsAsync(serviceUUIDs: string[], characteristicUUIDs: string[]): Promise<ServicesAndCharacteristics>;
108
+ readHandleAsync(handle: number): Promise<Buffer>;
109
+ writeHandleAsync(handle: number, data: Buffer, withoutResponse: boolean): Promise<void>;
110
+
111
+ connect(callback?: (error: Error | undefined) => void): void;
112
+ disconnect(callback?: () => void): void;
113
+ updateRssi(callback?: (error: Error | undefined, rssi: number) => void): void;
114
+ discoverServices(): void;
115
+ discoverServices(serviceUUIDs: string[], callback?: (error: Error | undefined, services: Service[]) => void): void;
116
+ discoverAllServicesAndCharacteristics(callback?: (error: Error | undefined, services: Service[], characteristics: Characteristic[]) => void): void;
117
+ discoverSomeServicesAndCharacteristics(serviceUUIDs: string[], characteristicUUIDs: string[], callback?: (error: Error | undefined, services: Service[], characteristics: Characteristic[]) => void): void;
118
+ readHandle(handle: number, callback: (error: Error | undefined, data: Buffer) => void): void;
119
+ writeHandle(handle: number, data: Buffer, withoutResponse: boolean, callback: (error: Error | undefined) => void): void;
120
+
121
+ cancelConnect(options?: object): void;
122
+ toString(): string;
123
+
124
+ on(event: "connect", listener: (error: Error | undefined) => void): this;
125
+ on(event: "disconnect", listener: (error: Error | undefined) => void): this;
126
+ on(event: "rssiUpdate", listener: (rssi: number) => void): this;
127
+ on(event: "servicesDiscover", listener: (services: Service[]) => void): this;
128
+ on(event: "mtu", listener: (mtu: number) => void): this;
129
+ on(event: string, listener: Function): this;
130
+
131
+ once(event: "connect", listener: (error: Error | undefined) => void): this;
132
+ once(event: "disconnect", listener: (error: Error | undefined) => void): this;
133
+ once(event: "rssiUpdate", listener: (rssi: number) => void): this;
134
+ once(event: "servicesDiscover", listener: (services: Service[]) => void): this;
135
+ once(event: string, listener: Function): this;
136
+ }
137
+
138
+ export class Service extends EventEmitter {
139
+
140
+ readonly uuid: string;
141
+ readonly name: string;
142
+ readonly type: string;
143
+ readonly includedServiceUuids: string[];
144
+ readonly characteristics: Characteristic[];
145
+
146
+ discoverIncludedServicesAsync(): Promise<string[]>;
147
+ discoverIncludedServicesAsync(serviceUUIDs: string[]): Promise<string[]>;
148
+ discoverCharacteristicsAsync(): Promise<Characteristic[]>;
149
+ discoverCharacteristicsAsync(characteristicUUIDs: string[]): Promise<Characteristic[]>;
150
+
151
+ discoverIncludedServices(): void;
152
+ discoverIncludedServices(serviceUUIDs: string[], callback?: (error: Error | undefined, includedServiceUuids: string[]) => void): void;
153
+ discoverCharacteristics(): void;
154
+ discoverCharacteristics(characteristicUUIDs: string[], callback?: (error: Error | undefined, characteristics: Characteristic[]) => void): void;
155
+
156
+ toString(): string;
157
+
158
+ on(event: "includedServicesDiscover", listener: (includedServiceUuids: string[]) => void): this;
159
+ on(event: "characteristicsDiscover", listener: (characteristics: Characteristic[]) => void): this;
160
+ on(event: string, listener: Function): this;
161
+
162
+ once(event: "includedServicesDiscover", listener: (includedServiceUuids: string[]) => void): this;
163
+ once(event: "characteristicsDiscover", listener: (characteristics: Characteristic[]) => void): this;
164
+ once(event: string, listener: Function): this;
165
+ }
166
+
167
+ export class Characteristic extends EventEmitter {
168
+
169
+ readonly uuid: string;
170
+ readonly name: string;
171
+ readonly type: string;
172
+ readonly properties: CharacteristicProperty[];
173
+ readonly descriptors: Descriptor[];
174
+
175
+ readAsync(): Promise<Buffer>;
176
+ writeAsync(data: Buffer, withoutResponse: boolean): Promise<void>;
177
+ broadcastAsync(broadcast: boolean): Promise<void>;
178
+ notifyAsync(notify: boolean): Promise<void>;
179
+ discoverDescriptorsAsync(): Promise<Descriptor[]>;
180
+ subscribeAsync(): Promise<void>;
181
+ unsubscribeAsync(): Promise<void>;
182
+
183
+ read(callback?: (error: Error | undefined, data: Buffer) => void): void;
184
+ write(data: Buffer, withoutResponse: boolean, callback?: (error: Error | undefined) => void): void;
185
+ broadcast(broadcast: boolean, callback?: (error: Error | undefined) => void): void;
186
+ notify(notify: boolean, callback?: (error: Error | undefined) => void): void;
187
+ discoverDescriptors(callback?: (error: Error | undefined, descriptors: Descriptor[]) => void): void;
188
+ subscribe(callback?: (error: Error | undefined) => void): void;
189
+ unsubscribe(callback?: (error: Error | undefined) => void): void;
190
+
191
+ toString(): string;
192
+
193
+ on(event: "read", listener: (data: Buffer, isNotification: boolean) => void): this;
194
+ on(event: "write", withoutResponse: boolean, listener: (error: Error | undefined) => void): this;
195
+ on(event: "broadcast", listener: (state: string) => void): this;
196
+ on(event: "notify", listener: (state: string) => void): this;
197
+ on(event: "data", listener: (data: Buffer, isNotification: boolean) => void): this;
198
+ on(event: "descriptorsDiscover", listener: (descriptors: Descriptor[]) => void): this;
199
+ on(event: string, listener: Function): this;
200
+
201
+ once(event: "read", listener: (data: Buffer, isNotification: boolean) => void): this;
202
+ once(event: "write", withoutResponse: boolean, listener: (error: Error | undefined) => void): this;
203
+ once(event: "broadcast", listener: (state: string) => void): this;
204
+ once(event: "notify", listener: (state: string) => void): this;
205
+ once(event: "data", listener: (data: Buffer, isNotification: boolean) => void): this;
206
+ once(event: "descriptorsDiscover", listener: (descriptors: Descriptor[]) => void): this;
207
+ once(event: string, listener: Function): this;
208
+ }
209
+
210
+ export class Descriptor extends EventEmitter {
211
+ readonly uuid: string;
212
+ readonly name: string;
213
+ readonly type: string;
214
+
215
+ readValueAsync(): Promise<Buffer>;
216
+ writeValueAsync(data: Buffer): Promise<void>;
217
+
218
+ readValue(callback?: (error: Error | undefined, data: Buffer) => void): void;
219
+ writeValue(data: Buffer, callback?: (error: Error | undefined) => void): void;
220
+
221
+ toString(): string;
222
+
223
+ on(event: "valueRead", listener: (error: Error | undefined, data: Buffer) => void): this;
224
+ on(event: "valueWrite", listener: (error: Error | undefined) => void): this;
225
+ on(event: string, listener: Function): this;
226
+
227
+ once(event: "valueRead", listener: (error: Error | undefined, data: Buffer) => void): this;
228
+ once(event: "valueWrite", listener: (error: Error | undefined) => void): this;
229
+ once(event: string, listener: Function): this;
230
+ }
231
+
232
+ export type BindingType = 'default' | 'hci' | 'mac' | 'win';
233
+
234
+ export interface BaseBindingsOptions {}
235
+
236
+ export interface HciBindingsOptions extends BaseBindingsOptions {
237
+ /** Driver Type ('default' | 'uart' | 'usb' | 'native') */
238
+ hciDriver?: import('@stoprocent/bluetooth-hci-socket').DriverType;
239
+ /** Bind Params (for USB and UART Hci Drivers only) */
240
+ bindParams?: import('@stoprocent/bluetooth-hci-socket').BindParams;
241
+ /** HCI Device ID (Linux Only), Default is 0 */
242
+ deviceId?: number;
243
+ /** Use With BLE5 Extended Features, Default is based on the result of LE_READ_LOCAL_SUPPORTED_FEATURES command */
244
+ extended?: boolean;
245
+ /** Uses User channel instead of Raw HCI Channel */
246
+ userChannel?: boolean;
247
+ }
203
248
 
204
- export declare class Descriptor extends events.EventEmitter {
205
- uuid: string;
206
- name: string;
207
- type: string;
249
+ export interface MacBindingsOptions extends BaseBindingsOptions {}
250
+ export interface WinBindingsOptions extends BaseBindingsOptions {}
208
251
 
209
- readValue(callback?: (error: string, data: Buffer) => void): void;
210
- readValueAsync(): Promise<Buffer>;
211
- writeValue(data: Buffer, callback?: (error: string) => void): void;
212
- writeValueAsync(data: Buffer): Promise<void>;
213
- toString(): string;
252
+ export type WithBindingsOptions = HciBindingsOptions | MacBindingsOptions | WinBindingsOptions;
214
253
 
215
- on(event: "valueRead", listener: (error: string, data: Buffer) => void): this;
216
- on(event: "valueWrite", listener: (error: string) => void): this;
217
- on(event: string, listener: Function): this;
254
+ export function withBindings(
255
+ bindingType?: BindingType,
256
+ options?: WithBindingsOptions
257
+ ): Noble;
218
258
 
219
- once(event: "valueRead", listener: (error: string, data: Buffer) => void): this;
220
- once(event: "valueWrite", listener: (error: string) => void): this;
221
- once(event: string, listener: Function): this;
259
+ // Define a default export
260
+ const NobleDefault: Noble;
261
+ export default NobleDefault;
222
262
  }
package/index.js CHANGED
@@ -1 +1,4 @@
1
- module.exports = require('./with-custom-binding')();
1
+ const withBindings = require('./lib/resolve-bindings');
2
+
3
+ module.exports = withBindings();
4
+ module.exports.withBindings = withBindings;
package/jest.config.js ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ collectCoverage: true,
3
+ coverageReporters: ['text', 'cobertura'],
4
+ }