@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
package/lib/peripheral.js CHANGED
@@ -1,212 +1,209 @@
1
- const events = require('events');
2
- const util = require('util');
3
-
4
- function Peripheral (noble, id, address, addressType, connectable, advertisement, rssi, scannable) {
5
- this._noble = noble;
6
-
7
- this.id = id;
8
- this.uuid = id; // for legacy
9
- this.address = address;
10
- this.addressType = addressType;
11
- this.connectable = connectable;
12
- this.scannable = scannable;
13
- this.advertisement = advertisement;
14
- this.rssi = rssi;
15
- this.services = null;
16
- this.mtu = null;
17
- this.state = 'disconnected';
18
- }
1
+ const { EventEmitter } = require('events');
2
+
3
+ class Peripheral extends EventEmitter {
4
+ constructor (noble, id, address, addressType, connectable, advertisement, rssi, scannable) {
5
+ super();
6
+ this._noble = noble;
7
+ this.id = id;
8
+ this.address = address;
9
+ this.addressType = addressType;
10
+ this.connectable = connectable;
11
+ this.scannable = scannable;
12
+ this.advertisement = advertisement;
13
+ this.rssi = rssi;
14
+ this.services = null;
15
+ this.mtu = null;
16
+ this.state = 'disconnected';
17
+ }
19
18
 
20
- util.inherits(Peripheral, events.EventEmitter);
21
-
22
- Peripheral.prototype.toString = function () {
23
- return JSON.stringify({
24
- id: this.id,
25
- address: this.address,
26
- addressType: this.addressType,
27
- connectable: this.connectable,
28
- advertisement: this.advertisement,
29
- rssi: this.rssi,
30
- mtu: this.mtu,
31
- state: this.state
32
- });
33
- };
34
-
35
- const connect = function (options, callback) {
36
- if (typeof options === 'function') {
37
- callback = options;
38
- options = undefined;
39
- }
40
-
41
- if (callback) {
42
- this.once('connect', error => {
43
- callback(error);
44
- });
19
+ get uuid () {
20
+ return this.id;
45
21
  }
46
22
 
47
- if (this.state === 'connected') {
48
- this.emit('connect', new Error('Peripheral already connected'));
49
- } else {
50
- this.state = 'connecting';
51
- this._noble.connect(this.id, options);
23
+ toString () {
24
+ return JSON.stringify({
25
+ id: this.id,
26
+ address: this.address,
27
+ addressType: this.addressType,
28
+ connectable: this.connectable,
29
+ advertisement: this.advertisement,
30
+ rssi: this.rssi,
31
+ mtu: this.mtu,
32
+ state: this.state
33
+ });
52
34
  }
53
- };
54
35
 
55
- Peripheral.prototype.connect = connect;
56
- Peripheral.prototype.connectAsync = function (options) {
57
- return util.promisify(callback => this.connect(options, callback))();
58
- };
36
+ connect (options, callback) {
37
+ if (typeof options === 'function') {
38
+ callback = options;
39
+ options = undefined;
40
+ }
59
41
 
60
- Peripheral.prototype.cancelConnect = function (options) {
61
- if (this.state === 'connecting') {
62
- this.emit('connect', new Error('connection canceled!'));
63
- this._noble.cancelConnect(this.id, options);
64
- }
65
- };
42
+ if (callback) {
43
+ this.once('connect', error => callback(error));
44
+ }
66
45
 
67
- const disconnect = function (callback) {
68
- if (callback) {
69
- this.once('disconnect', () => {
70
- callback(null);
71
- });
46
+ if (this.state === 'connected') {
47
+ this.emit('connect', new Error('Peripheral already connected'));
48
+ } else {
49
+ this.state = 'connecting';
50
+ this._noble.connect(this.id, options);
51
+ }
72
52
  }
73
- this.state = 'disconnecting';
74
- this._noble.disconnect(this.id);
75
- };
76
53
 
77
- Peripheral.prototype.disconnect = disconnect;
78
- Peripheral.prototype.disconnectAsync = util.promisify(disconnect);
79
-
80
- const updateRssi = function (callback) {
81
- if (callback) {
82
- this.once('rssiUpdate', rssi => {
83
- callback(null, rssi);
54
+ async connectAsync (options) {
55
+ return new Promise((resolve, reject) => {
56
+ this.connect(options, error => error ? reject(error) : resolve());
84
57
  });
85
58
  }
86
59
 
87
- this._noble.updateRssi(this.id);
88
- };
60
+ cancelConnect (options) {
61
+ if (this.state === 'connecting') {
62
+ this.emit('connect', new Error('connection canceled!'));
63
+ this._noble.cancelConnect(this.id, options);
64
+ }
65
+ }
89
66
 
90
- Peripheral.prototype.updateRssi = updateRssi;
91
- Peripheral.prototype.updateRssiAsync = util.promisify(updateRssi);
67
+ disconnect (callback) {
68
+ if (callback) {
69
+ this.once('disconnect', () => callback(null));
70
+ }
71
+ this.state = 'disconnecting';
72
+ this._noble.disconnect(this.id);
73
+ }
92
74
 
93
- const discoverServices = function (uuids, callback) {
94
- if (callback) {
95
- this.once('servicesDiscover', services => {
96
- callback(null, services);
75
+ async disconnectAsync () {
76
+ return new Promise((resolve, reject) => {
77
+ this.disconnect(error => error ? reject(error) : resolve());
97
78
  });
98
79
  }
99
80
 
100
- this._noble.discoverServices(this.id, uuids);
101
- };
102
-
103
- Peripheral.prototype.discoverServices = discoverServices;
104
- Peripheral.prototype.discoverServicesAsync = function (uuids) {
105
- return util.promisify((callback) => this.discoverServices(uuids, callback))();
106
- };
107
-
108
- const discoverSomeServicesAndCharacteristics = function (serviceUuids, characteristicsUuids, callback) {
109
- this.discoverServices(serviceUuids, (err, services) => {
110
- if (!err && services.length < serviceUuids.length) {
111
- err = 'Could not find all requested services';
112
- }
113
-
114
- if (err) {
115
- callback(err, null, null);
116
- return;
81
+ updateRssi (callback) {
82
+ if (callback) {
83
+ this.once('rssiUpdate', (rssi, error) => callback(error, rssi));
117
84
  }
118
- let numDiscovered = 0;
119
- const allCharacteristics = [];
85
+ this._noble.updateRssi(this.id);
86
+ }
120
87
 
121
- for (const i in services) {
122
- const service = services[i];
88
+ async updateRssiAsync () {
89
+ return this._noble._withDisconnectHandler(this.id, () => {
90
+ return new Promise((resolve, reject) => {
91
+ this.updateRssi((error, rssi) => error ? reject(error) : resolve(rssi));
92
+ });
93
+ });
94
+ }
123
95
 
124
- service.discoverCharacteristics(characteristicsUuids, (error, characteristics) => {
125
- numDiscovered++;
96
+ discoverServices (uuids, callback) {
97
+ if (callback) {
98
+ this.once('servicesDiscover', (services, error) => callback(error, services));
99
+ }
100
+ this._noble.discoverServices(this.id, uuids);
101
+ }
126
102
 
127
- // TODO: handle `error`?
128
- if (error === null) {
129
- for (const j in characteristics) {
130
- const characteristic = characteristics[j];
103
+ async discoverServicesAsync (uuids) {
104
+ return this._noble._withDisconnectHandler(this.id, () => {
105
+ return new Promise((resolve, reject) => {
106
+ this.discoverServices(uuids, (error, services) => error ? reject(error) : resolve(services));
107
+ });
108
+ });
109
+ }
131
110
 
132
- allCharacteristics.push(characteristic);
111
+ discoverSomeServicesAndCharacteristics (serviceUuids, characteristicsUuids, callback) {
112
+ this.discoverServices(serviceUuids, (err, services) => {
113
+ if (!err && services.length < serviceUuids.length) {
114
+ err = 'Could not find all requested services';
115
+ }
116
+
117
+ if (err) {
118
+ callback(err, null, null);
119
+ return;
120
+ }
121
+ let numDiscovered = 0;
122
+ const allCharacteristics = [];
123
+
124
+ for (const i in services) {
125
+ const service = services[i];
126
+
127
+ service.discoverCharacteristics(characteristicsUuids, (error, characteristics) => {
128
+ numDiscovered++;
129
+ if (error == null) {
130
+ for (const j in characteristics) {
131
+ const characteristic = characteristics[j];
132
+ allCharacteristics.push(characteristic);
133
+ }
133
134
  }
134
- }
135
-
136
- if (numDiscovered === services.length) {
137
- if (callback) {
138
- callback(null, services, allCharacteristics);
135
+ if (numDiscovered === services.length) {
136
+ if (callback) {
137
+ callback(null, services, allCharacteristics);
138
+ }
139
139
  }
140
- }
140
+ });
141
+ }
142
+ });
143
+ }
144
+
145
+ async discoverSomeServicesAndCharacteristicsAsync (serviceUuids, characteristicsUuids) {
146
+ return this._noble._withDisconnectHandler(this.id, () => {
147
+ return new Promise((resolve, reject) => {
148
+ this.discoverSomeServicesAndCharacteristics(
149
+ serviceUuids,
150
+ characteristicsUuids,
151
+ (error, services, characteristics) =>
152
+ error ? reject(error) : resolve({ services, characteristics })
153
+ );
141
154
  });
142
- }
143
- });
144
- };
145
-
146
- Peripheral.prototype.discoverSomeServicesAndCharacteristics = discoverSomeServicesAndCharacteristics;
147
- Peripheral.prototype.discoverSomeServicesAndCharacteristicsAsync = function (serviceUuids, characteristicsUuids) {
148
- return new Promise((resolve, reject) =>
149
- this.discoverSomeServicesAndCharacteristics(
150
- serviceUuids,
151
- characteristicsUuids,
152
- (error, services, characteristics) =>
153
- error
154
- ? reject(error)
155
- : resolve({
156
- services,
157
- characteristics
158
- })
159
- )
160
- );
161
- };
162
-
163
- const discoverAllServicesAndCharacteristics = function (callback) {
164
- this.discoverSomeServicesAndCharacteristics([], [], callback);
165
- };
166
-
167
- Peripheral.prototype.discoverAllServicesAndCharacteristics = discoverAllServicesAndCharacteristics;
168
- Peripheral.prototype.discoverAllServicesAndCharacteristicsAsync = function () {
169
- return new Promise((resolve, reject) =>
170
- this.discoverAllServicesAndCharacteristics(
171
- (error, services, characteristics) =>
172
- error
173
- ? reject(error)
174
- : resolve({
175
- services,
176
- characteristics
177
- })
178
- )
179
- );
180
- };
181
-
182
- const readHandle = function (handle, callback) {
183
- if (callback) {
184
- this.once(`handleRead${handle}`, data => {
185
- callback(null, data);
186
155
  });
187
156
  }
188
157
 
189
- this._noble.readHandle(this.id, handle);
190
- };
158
+ discoverAllServicesAndCharacteristics (callback) {
159
+ this.discoverSomeServicesAndCharacteristics([], [], callback);
160
+ }
191
161
 
192
- Peripheral.prototype.readHandle = readHandle;
193
- Peripheral.prototype.readHandleAsync = util.promisify(readHandle);
162
+ async discoverAllServicesAndCharacteristicsAsync () {
163
+ return this._noble._withDisconnectHandler(this.id, () => {
164
+ return new Promise((resolve, reject) => {
165
+ this.discoverAllServicesAndCharacteristics(
166
+ (error, services, characteristics) =>
167
+ error ? reject(error) : resolve({ services, characteristics })
168
+ );
169
+ });
170
+ });
171
+ }
194
172
 
195
- const writeHandle = function (handle, data, withoutResponse, callback) {
196
- if (!(data instanceof Buffer)) {
197
- throw new Error('data must be a Buffer');
173
+ readHandle (handle, callback) {
174
+ if (callback) {
175
+ this.once(`handleRead${handle}`, (data, error) => callback(error, data));
176
+ }
177
+ this._noble.readHandle(this.id, handle);
198
178
  }
199
179
 
200
- if (callback) {
201
- this.once(`handleWrite${handle}`, () => {
202
- callback(null);
180
+ async readHandleAsync (handle) {
181
+ return this._noble._withDisconnectHandler(this.id, () => {
182
+ return new Promise((resolve, reject) => {
183
+ this.readHandle(handle, (error, data) => error ? reject(error) : resolve(data));
184
+ });
203
185
  });
204
186
  }
205
187
 
206
- this._noble.writeHandle(this.id, handle, data, withoutResponse);
207
- };
188
+ writeHandle (handle, data, withoutResponse, callback) {
189
+ if (!(data instanceof Buffer)) {
190
+ throw new Error('data must be a Buffer');
191
+ }
192
+
193
+ if (callback) {
194
+ this.once(`handleWrite${handle}`, (error) => callback(error));
195
+ }
208
196
 
209
- Peripheral.prototype.writeHandle = writeHandle;
210
- Peripheral.prototype.writeHandleAsync = util.promisify(writeHandle);
197
+ this._noble.writeHandle(this.id, handle, data, withoutResponse);
198
+ }
199
+
200
+ async writeHandleAsync (handle, data, withoutResponse) {
201
+ return this._noble._withDisconnectHandler(this.id, () => {
202
+ return new Promise((resolve, reject) => {
203
+ this.writeHandle(handle, data, withoutResponse, error => error ? reject(error) : resolve());
204
+ });
205
+ });
206
+ }
207
+ }
211
208
 
212
209
  module.exports = Peripheral;
@@ -1,44 +1,51 @@
1
1
  const os = require('os');
2
2
 
3
- function getWindowsBindings () {
4
- const ver = os
5
- .release()
6
- .split('.')
7
- .map((str) => parseInt(str, 10));
8
- if (
9
- !(
10
- ver[0] > 10 ||
11
- (ver[0] === 10 && ver[1] > 0) ||
12
- (ver[0] === 10 && ver[1] === 0 && ver[2] >= 15063)
13
- )
14
- ) {
15
- return require('./hci-socket/bindings');
16
- } else {
17
- return require('./win/bindings');
3
+ const Noble = require('./noble');
4
+
5
+ function loadBindings (bindingType = null, options = {}) {
6
+ switch (bindingType) {
7
+ case 'hci':
8
+ return new (require('./hci-socket/bindings'))(options);
9
+ case 'mac':
10
+ return new (require('./mac/bindings'))(options);
11
+ case 'win':
12
+ return new (getWindowsBindings())(options);
13
+ default:
14
+ throw new Error('Unsupported binding type: ' + bindingType);
18
15
  }
19
16
  }
20
17
 
21
- module.exports = function (options = {}) {
22
- const platform = os.platform();
18
+ function getWindowsBindings () {
19
+ const ver = os.release().split('.').map((str) => parseInt(str, 10));
20
+ const isWin10WithBLE =
21
+ ver[0] > 10 ||
22
+ (ver[0] === 10 && ver[1] > 0) ||
23
+ (ver[0] === 10 && ver[1] === 0 && ver[2] >= 15063);
24
+ return isWin10WithBLE ? require('./win/bindings') : require('./hci-socket/bindings');
25
+ }
23
26
 
24
- if (process.env.NOBLE_WEBSOCKET) {
25
- return new (require('./websocket/bindings'))(options);
26
- } else if (process.env.NOBLE_DISTRIBUTED) {
27
- return new (require('./distributed/bindings'))(options);
28
- } else if (
27
+ function getDefaultBindings (options = {}) {
28
+ const platform = os.platform();
29
+ if (
29
30
  platform === 'linux' ||
30
31
  platform === 'freebsd' ||
31
- (process.env.BLUETOOTH_HCI_SOCKET_UART_PORT ||
32
- process.env.BLUETOOTH_HCI_SOCKET_FORCE_UART) ||
33
- (process.env.BLUETOOTH_HCI_SOCKET_USB_VID &&
34
- process.env.BLUETOOTH_HCI_SOCKET_USB_PID)
32
+ process.env.BLUETOOTH_HCI_SOCKET_UART_PORT ||
33
+ process.env.BLUETOOTH_HCI_SOCKET_FORCE_UART ||
34
+ (process.env.BLUETOOTH_HCI_SOCKET_USB_VID && process.env.BLUETOOTH_HCI_SOCKET_USB_PID)
35
35
  ) {
36
- return new (require('./hci-socket/bindings'))(options);
36
+ return loadBindings('hci', options);
37
37
  } else if (platform === 'darwin') {
38
- return new (require('./mac/bindings'))(options);
38
+ return loadBindings('mac', options);
39
39
  } else if (platform === 'win32') {
40
- return new (getWindowsBindings())(options);
40
+ return loadBindings('win', options);
41
41
  } else {
42
- throw new Error('Unsupported platform');
42
+ throw new Error('Unsupported platform: ' + platform);
43
43
  }
44
+ }
45
+
46
+ module.exports = function (bindingType = 'default', options = {}) {
47
+ const bindings = bindingType === 'default'
48
+ ? getDefaultBindings(options)
49
+ : loadBindings(bindingType, options);
50
+ return new Noble(bindings);
44
51
  };
package/lib/service.js CHANGED
@@ -1,72 +1,75 @@
1
- const events = require('events');
2
- const util = require('util');
3
-
1
+ const { EventEmitter } = require('events');
4
2
  const services = require('./services.json');
5
3
 
6
- function Service (noble, peripheralId, uuid) {
7
- this._noble = noble;
8
- this._peripheralId = peripheralId;
9
-
10
- this.uuid = uuid;
11
- this.name = null;
12
- this.type = null;
13
- this.includedServiceUuids = null;
14
- this.characteristics = null;
4
+ class Service extends EventEmitter {
15
5
 
16
- const service = services[uuid];
17
- if (service) {
18
- this.name = service.name;
19
- this.type = service.type;
20
- }
21
- }
6
+ constructor (noble, peripheralId, uuid) {
7
+ super();
8
+
9
+ this._noble = noble;
10
+ this._peripheralId = peripheralId;
22
11
 
23
- util.inherits(Service, events.EventEmitter);
12
+ this.uuid = uuid;
13
+ this.name = null;
14
+ this.type = null;
15
+ this.includedServiceUuids = null;
16
+ this.characteristics = null;
24
17
 
25
- Service.prototype.toString = function () {
26
- return JSON.stringify({
27
- uuid: this.uuid,
28
- name: this.name,
29
- type: this.type,
30
- includedServiceUuids: this.includedServiceUuids
31
- });
32
- };
18
+ const service = services[uuid];
19
+ if (service) {
20
+ this.name = service.name;
21
+ this.type = service.type;
22
+ }
23
+ }
33
24
 
34
- const discoverIncludedServices = function (serviceUuids, callback) {
35
- if (callback) {
36
- this.once('includedServicesDiscover', includedServiceUuids => {
37
- callback(null, includedServiceUuids);
25
+ toString () {
26
+ return JSON.stringify({
27
+ uuid: this.uuid,
28
+ name: this.name,
29
+ type: this.type,
30
+ includedServiceUuids: this.includedServiceUuids
38
31
  });
39
32
  }
40
33
 
41
- this._noble.discoverIncludedServices(
42
- this._peripheralId,
43
- this.uuid,
44
- serviceUuids
45
- );
46
- };
34
+ discoverIncludedServices (serviceUuids, callback) {
35
+ if (callback) {
36
+ this.once('includedServicesDiscover', (includedServiceUuids, error) => callback(error, includedServiceUuids));
37
+ }
47
38
 
48
- Service.prototype.discoverIncludedServices = discoverIncludedServices;
49
- Service.prototype.discoverIncludedServicesAsync = function (serviceUuids) {
50
- return util.promisify((callback) => this.discoverIncludedServices(serviceUuids, callback))();
51
- };
39
+ this._noble.discoverIncludedServices(
40
+ this._peripheralId,
41
+ this.uuid,
42
+ serviceUuids
43
+ );
44
+ }
52
45
 
53
- const discoverCharacteristics = function (characteristicUuids, callback) {
54
- if (callback) {
55
- this.once('characteristicsDiscover', characteristics => {
56
- callback(null, characteristics);
46
+ async discoverIncludedServicesAsync (serviceUuids) {
47
+ return this._noble._withDisconnectHandler(this._peripheralId, () => {
48
+ return new Promise((resolve, reject) => {
49
+ this.discoverIncludedServices(serviceUuids, (error, includedServiceUuids) => error ? reject(error) : resolve(includedServiceUuids));
50
+ });
57
51
  });
58
52
  }
59
53
 
60
- this._noble.discoverCharacteristics(
61
- this._peripheralId,
62
- this.uuid,
63
- characteristicUuids
64
- );
65
- };
54
+ discoverCharacteristics (characteristicUuids, callback) {
55
+ if (callback) {
56
+ this.once('characteristicsDiscover', (characteristics, error) => callback(error, characteristics));
57
+ }
66
58
 
67
- Service.prototype.discoverCharacteristics = discoverCharacteristics;
68
- Service.prototype.discoverCharacteristicsAsync = function (characteristicUuids) {
69
- return util.promisify((callback) => this.discoverCharacteristics(characteristicUuids, callback))();
70
- };
59
+ this._noble.discoverCharacteristics(
60
+ this._peripheralId,
61
+ this.uuid,
62
+ characteristicUuids
63
+ );
64
+ }
65
+
66
+ async discoverCharacteristicsAsync (characteristicUuids) {
67
+ return this._noble._withDisconnectHandler(this._peripheralId, () => {
68
+ return new Promise((resolve, reject) => {
69
+ this.discoverCharacteristics(characteristicUuids, (error, characteristics) => error ? reject(error) : resolve(characteristics));
70
+ });
71
+ });
72
+ }
73
+ }
71
74
 
72
75
  module.exports = Service;
@@ -6,19 +6,12 @@
6
6
  {
7
7
  'target_name': 'binding',
8
8
  'sources': [
9
- 'src/noble_winrt.cc',
10
- 'src/napi_winrt.cc',
11
- 'src/peripheral_winrt.cc',
12
- 'src/radio_watcher.cc',
13
- 'src/notify_map.cc',
14
- 'src/ble_manager.cc',
15
- 'src/winrt_cpp.cc',
16
- 'src/winrt_guid.cc',
17
- 'src/callbacks.cc'
9
+ "<!@(node -p \"require('fs').readdirSync('src').filter(f=>new RegExp('.*\\\\.(c|cc|cpp)$').test(f)).map(f=>'src/'+f).join(' ')\")",
10
+ "<!@(node -p \"require('fs').readdirSync('../common/src').filter(f=>new RegExp('.*\\\\.(c|cc|cpp)$').test(f)).map(f=>'../common/src/'+f).join(' ')\")"
18
11
  ],
19
12
  'include_dirs': [
20
13
  "<!(node -p \"require('node-addon-api').include_dir\")",
21
- "<!@(node -p \"require('napi-thread-safe-callback').include\")"
14
+ "../common/include"
22
15
  ],
23
16
  'cflags!': [ '-fno-exceptions' ],
24
17
  'cflags_cc!': [ '-fno-exceptions' ],
@@ -27,7 +20,7 @@
27
20
  'ExceptionHandling': 1,
28
21
  'AdditionalOptions': [
29
22
  '/await',
30
- '/std:c++latest'
23
+ '/std:c++20'
31
24
  ],
32
25
  },
33
26
  },
@@ -1,11 +1,9 @@
1
1
  const { EventEmitter } = require('events');
2
- const { inherits } = require('util');
3
2
  const { resolve } = require('path');
4
3
  const dir = resolve(__dirname, '..', '..');
5
4
  const binding = require('node-gyp-build')(dir);
6
-
7
5
  const { NobleWinrt } = binding;
8
6
 
9
- inherits(NobleWinrt, EventEmitter);
7
+ Object.setPrototypeOf(NobleWinrt.prototype, EventEmitter.prototype);
10
8
 
11
9
  module.exports = NobleWinrt;