@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
@@ -1,14 +1,7 @@
1
- //
2
- // callbacks.cc
3
- // noble-mac-native
4
- //
5
- // Created by Georg Vienna on 30.08.18.
6
- //
7
- #include "callbacks.h"
8
-
9
- #include <napi-thread-safe-callback.hpp>
1
+ #include "Emit.h"
10
2
 
11
3
  #define _s(val) Napi::String::New(env, val)
4
+ #define _e(val) Napi::Error::New(env, _s(val)).Value()
12
5
  #define _b(val) Napi::Boolean::New(env, val)
13
6
  #define _n(val) Napi::Number::New(env, val)
14
7
  #define _u(str) toUuid(env, str)
@@ -36,11 +29,12 @@ Napi::String toAddressType(Napi::Env& env, const AddressType& type)
36
29
 
37
30
  Napi::Buffer<uint8_t> toBuffer(Napi::Env& env, const Data& data)
38
31
  {
39
- if (data.empty())
40
- {
32
+ if (data.empty()) {
41
33
  return Napi::Buffer<uint8_t>::New(env, 0);
42
34
  }
43
- return Napi::Buffer<uint8_t>::Copy(env, &data[0], data.size());
35
+ else {
36
+ return Napi::Buffer<uint8_t>::Copy(env, &data[0], data.size());
37
+ }
44
38
  }
45
39
 
46
40
  Napi::Array toUuidArray(Napi::Env& env, const std::vector<std::string>& data)
@@ -84,6 +78,14 @@ void Emit::RadioState(const std::string& state)
84
78
  });
85
79
  }
86
80
 
81
+ void Emit::Address(const std::string& address)
82
+ {
83
+ mCallback->call([address](Napi::Env env, std::vector<napi_value>& args) {
84
+ // emit('addressChange', address);
85
+ args = { _s("addressChange"), _s(address) };
86
+ });
87
+ }
88
+
87
89
  void Emit::ScanState(bool start)
88
90
  {
89
91
  mCallback->call([start](Napi::Env env, std::vector<napi_value>& args) {
@@ -94,6 +96,7 @@ void Emit::ScanState(bool start)
94
96
 
95
97
  void Emit::Scan(const std::string& uuid, int rssi, const Peripheral& peripheral)
96
98
  {
99
+ // Copy values to capture in lambda
97
100
  auto address = peripheral.address;
98
101
  auto addressType = peripheral.addressType;
99
102
  auto connectable = peripheral.connectable;
@@ -102,24 +105,41 @@ void Emit::Scan(const std::string& uuid, int rssi, const Peripheral& peripheral)
102
105
  auto manufacturerData = peripheral.manufacturerData;
103
106
  auto serviceData = peripheral.serviceData;
104
107
  auto serviceUuids = peripheral.serviceUuids;
108
+
105
109
  mCallback->call([uuid, rssi, address, addressType, connectable, name, txPowerLevel,
106
110
  manufacturerData, serviceData,
107
111
  serviceUuids](Napi::Env env, std::vector<napi_value>& args) {
108
112
  Napi::Object advertisment = Napi::Object::New(env);
109
- advertisment.Set(_s("localName"), _s(name));
110
- advertisment.Set(_s("txPowerLevel"), txPowerLevel);
111
- advertisment.Set(_s("manufacturerData"), toBuffer(env, manufacturerData));
112
- auto array =
113
- serviceData.empty() ? Napi::Array::New(env) : Napi::Array::New(env, serviceData.size());
114
- for (size_t i = 0; i < serviceData.size(); i++)
115
- {
116
- Napi::Object data = Napi::Object::New(env);
117
- data.Set(_s("uuid"), _u(serviceData[i].first));
118
- data.Set(_s("data"), toBuffer(env, serviceData[i].second));
119
- array.Set(i, data);
113
+
114
+ // Handle optional name
115
+ advertisment.Set(_s("localName"),
116
+ name.has_value() ? _s(name.value()) : env.Null());
117
+
118
+ // Handle optional txPowerLevel
119
+ advertisment.Set(_s("txPowerLevel"),
120
+ txPowerLevel.has_value() ? _n(txPowerLevel.value()) : env.Null());
121
+
122
+ // Handle optional manufacturerData
123
+ advertisment.Set(_s("manufacturerData"),
124
+ manufacturerData.has_value() ? toBuffer(env, manufacturerData.value()) : Napi::Buffer<uint8_t>::New(env, 0));
125
+
126
+ // Handle optional serviceData
127
+ auto array = Napi::Array::New(env);
128
+ if (serviceData.has_value()) {
129
+ const auto& data = serviceData.value();
130
+ array = Napi::Array::New(env, data.size());
131
+ for (size_t i = 0; i < data.size(); i++) {
132
+ Napi::Object dataObj = Napi::Object::New(env);
133
+ dataObj.Set(_s("uuid"), _u(data[i].first));
134
+ dataObj.Set(_s("data"), toBuffer(env, data[i].second));
135
+ array.Set(i, dataObj);
136
+ }
120
137
  }
121
138
  advertisment.Set(_s("serviceData"), array);
122
- advertisment.Set(_s("serviceUuids"), toUuidArray(env, serviceUuids));
139
+
140
+ // Handle optional serviceUuids
141
+ advertisment.Set(_s("serviceUuids"), serviceUuids.has_value() ? toUuidArray(env, serviceUuids.value()) : Napi::Array::New(env));
142
+
123
143
  // emit('discover', deviceUuid, address, addressType, connectable, advertisement, rssi);
124
144
  args = { _s("discover"), _u(uuid), _s(address), toAddressType(env, addressType),
125
145
  _b(connectable), advertisment, _n(rssi) };
@@ -130,7 +150,7 @@ void Emit::Connected(const std::string& uuid, const std::string& error)
130
150
  {
131
151
  mCallback->call([uuid, error](Napi::Env env, std::vector<napi_value>& args) {
132
152
  // emit('connect', deviceUuid) error added here
133
- args = { _s("connect"), _u(uuid), error.empty() ? env.Null() : _s(error) };
153
+ args = { _s("connect"), _u(uuid), error.empty() ? env.Null() : _e(error) };
134
154
  });
135
155
  }
136
156
 
@@ -142,39 +162,39 @@ void Emit::Disconnected(const std::string& uuid)
142
162
  });
143
163
  }
144
164
 
145
- void Emit::RSSI(const std::string& uuid, int rssi)
165
+ void Emit::RSSI(const std::string& uuid, int rssi, const std::string& error)
146
166
  {
147
- mCallback->call([uuid, rssi](Napi::Env env, std::vector<napi_value>& args) {
167
+ mCallback->call([uuid, rssi, error](Napi::Env env, std::vector<napi_value>& args) {
148
168
  // emit('rssiUpdate', deviceUuid, rssi);
149
- args = { _s("rssiUpdate"), _u(uuid), _n(rssi) };
169
+ args = { _s("rssiUpdate"), _u(uuid), _n(rssi), error.empty() ? env.Null() : _e(error) };
150
170
  });
151
171
  }
152
172
 
153
- void Emit::ServicesDiscovered(const std::string& uuid, const std::vector<std::string>& serviceUuids)
173
+ void Emit::ServicesDiscovered(const std::string& uuid, const std::vector<std::string>& serviceUuids, const std::string& error)
154
174
  {
155
- mCallback->call([uuid, serviceUuids](Napi::Env env, std::vector<napi_value>& args) {
175
+ mCallback->call([uuid, serviceUuids, error](Napi::Env env, std::vector<napi_value>& args) {
156
176
  // emit('servicesDiscover', deviceUuid, serviceUuids)
157
- args = { _s("servicesDiscover"), _u(uuid), toUuidArray(env, serviceUuids) };
177
+ args = { _s("servicesDiscover"), _u(uuid), toUuidArray(env, serviceUuids), error.empty() ? env.Null() : _e(error) };
158
178
  });
159
179
  }
160
180
 
161
181
  void Emit::IncludedServicesDiscovered(const std::string& uuid, const std::string& serviceUuid,
162
- const std::vector<std::string>& serviceUuids)
182
+ const std::vector<std::string>& serviceUuids, const std::string& error)
163
183
  {
164
184
  mCallback->call(
165
- [uuid, serviceUuid, serviceUuids](Napi::Env env, std::vector<napi_value>& args) {
185
+ [uuid, serviceUuid, serviceUuids, error](Napi::Env env, std::vector<napi_value>& args) {
166
186
  // emit('includedServicesDiscover', deviceUuid, serviceUuid, includedServiceUuids)
167
187
  args = { _s("includedServicesDiscover"), _u(uuid), _u(serviceUuid),
168
- toUuidArray(env, serviceUuids) };
188
+ toUuidArray(env, serviceUuids), error.empty() ? env.Null() : _e(error) };
169
189
  });
170
190
  }
171
191
 
172
192
  void Emit::CharacteristicsDiscovered(
173
193
  const std::string& uuid, const std::string& serviceUuid,
174
- const std::vector<std::pair<std::string, std::vector<std::string>>>& characteristics)
194
+ const std::vector<std::pair<std::string, std::vector<std::string>>>& characteristics, const std::string& error)
175
195
  {
176
196
  mCallback->call(
177
- [uuid, serviceUuid, characteristics](Napi::Env env, std::vector<napi_value>& args) {
197
+ [uuid, serviceUuid, characteristics, error](Napi::Env env, std::vector<napi_value>& args) {
178
198
  auto arr = characteristics.empty() ? Napi::Array::New(env)
179
199
  : Napi::Array::New(env, characteristics.size());
180
200
  for (size_t i = 0; i < characteristics.size(); i++)
@@ -186,89 +206,112 @@ void Emit::CharacteristicsDiscovered(
186
206
  }
187
207
  // emit('characteristicsDiscover', deviceUuid, serviceUuid, { uuid, properties:
188
208
  // ['broadcast', 'read', ...]})
189
- args = { _s("characteristicsDiscover"), _u(uuid), _u(serviceUuid), arr };
209
+ args = { _s("characteristicsDiscover"), _u(uuid), _u(serviceUuid), arr, error.empty() ? env.Null() : _e(error) };
190
210
  });
191
211
  }
192
212
 
193
213
  void Emit::Read(const std::string& uuid, const std::string& serviceUuid,
194
- const std::string& characteristicUuid, const Data& data, bool isNotification)
214
+ const std::string& characteristicUuid, const Data& data, bool isNotification, const std::string& error)
195
215
  {
196
216
  mCallback->call([uuid, serviceUuid, characteristicUuid, data,
197
- isNotification](Napi::Env env, std::vector<napi_value>& args) {
217
+ isNotification, error](Napi::Env env, std::vector<napi_value>& args) {
198
218
  // emit('read', deviceUuid, serviceUuid, characteristicsUuid, data, isNotification);
199
- args = { _s("read"), _u(uuid), _u(serviceUuid), _u(characteristicUuid),
200
- toBuffer(env, data), _b(isNotification) };
219
+ args = {
220
+ _s("read"),
221
+ _u(uuid),
222
+ _u(serviceUuid),
223
+ _u(characteristicUuid),
224
+ error.empty() ? toBuffer(env, data) : env.Null(),
225
+ _b(isNotification),
226
+ error.empty() ? env.Null() : _e(error)
227
+ };
201
228
  });
202
229
  }
203
230
 
204
231
  void Emit::Write(const std::string& uuid, const std::string& serviceUuid,
205
- const std::string& characteristicUuid)
232
+ const std::string& characteristicUuid, const std::string& error)
206
233
  {
207
234
  mCallback->call(
208
- [uuid, serviceUuid, characteristicUuid](Napi::Env env, std::vector<napi_value>& args) {
235
+ [uuid, serviceUuid, characteristicUuid, error](Napi::Env env, std::vector<napi_value>& args) {
209
236
  // emit('write', deviceUuid, servicesUuid, characteristicsUuid)
210
- args = { _s("write"), _u(uuid), _u(serviceUuid), _u(characteristicUuid) };
237
+ args = { _s("write"), _u(uuid), _u(serviceUuid), _u(characteristicUuid), error.empty() ? env.Null() : _e(error) };
211
238
  });
212
239
  }
213
240
 
214
241
  void Emit::Notify(const std::string& uuid, const std::string& serviceUuid,
215
- const std::string& characteristicUuid, bool state)
242
+ const std::string& characteristicUuid, bool state, const std::string& error)
216
243
  {
217
- mCallback->call([uuid, serviceUuid, characteristicUuid, state](Napi::Env env,
244
+ mCallback->call([uuid, serviceUuid, characteristicUuid, state, error](Napi::Env env,
218
245
  std::vector<napi_value>& args) {
219
246
  // emit('notify', deviceUuid, servicesUuid, characteristicsUuid, state)
220
- args = { _s("notify"), _u(uuid), _u(serviceUuid), _u(characteristicUuid), _b(state) };
247
+ args = { _s("notify"), _u(uuid), _u(serviceUuid), _u(characteristicUuid), _b(state), error.empty() ? env.Null() : _e(error) };
221
248
  });
222
249
  }
223
250
 
224
251
  void Emit::DescriptorsDiscovered(const std::string& uuid, const std::string& serviceUuid,
225
252
  const std::string& characteristicUuid,
226
- const std::vector<std::string>& descriptorUuids)
253
+ const std::vector<std::string>& descriptorUuids, const std::string& error)
227
254
  {
228
- mCallback->call([uuid, serviceUuid, characteristicUuid,
229
- descriptorUuids](Napi::Env env, std::vector<napi_value>& args) {
230
- // emit('descriptorsDiscover', deviceUuid, servicesUuid, characteristicsUuid, descriptors:
231
- // [uuids])
232
- args = { _s("descriptorsDiscover"), _u(uuid), _u(serviceUuid), _u(characteristicUuid),
233
- toUuidArray(env, descriptorUuids) };
255
+ mCallback->call([uuid, serviceUuid, characteristicUuid, descriptorUuids, error](Napi::Env env, std::vector<napi_value>& args) {
256
+ // emit('descriptorsDiscover', deviceUuid, servicesUuid, characteristicsUuid, descriptors:[uuids])
257
+ args = {
258
+ _s("descriptorsDiscover"),
259
+ _u(uuid),
260
+ _u(serviceUuid),
261
+ _u(characteristicUuid),
262
+ toUuidArray(env, descriptorUuids),
263
+ error.empty() ? env.Null() : _e(error)
264
+ };
234
265
  });
235
266
  }
236
267
 
237
268
  void Emit::ReadValue(const std::string& uuid, const std::string& serviceUuid,
238
269
  const std::string& characteristicUuid, const std::string& descriptorUuid,
239
- const Data& data)
270
+ const Data& data, const std::string& error)
240
271
  {
241
272
  mCallback->call([uuid, serviceUuid, characteristicUuid, descriptorUuid,
242
- data](Napi::Env env, std::vector<napi_value>& args) {
273
+ data, error](Napi::Env env, std::vector<napi_value>& args) {
243
274
  // emit('valueRead', deviceUuid, serviceUuid, characteristicUuid, descriptorUuid, data)
244
- args = { _s("valueRead"), _u(uuid), _u(serviceUuid),
245
- _u(characteristicUuid), _u(descriptorUuid), toBuffer(env, data) };
275
+ args = {
276
+ _s("valueRead"),
277
+ _u(uuid),
278
+ _u(serviceUuid),
279
+ _u(characteristicUuid),
280
+ _u(descriptorUuid),
281
+ error.empty() ? toBuffer(env, data) : env.Null(),
282
+ error.empty() ? env.Null() : _e(error)
283
+ };
246
284
  });
247
285
  }
248
286
 
249
287
  void Emit::WriteValue(const std::string& uuid, const std::string& serviceUuid,
250
- const std::string& characteristicUuid, const std::string& descriptorUuid)
288
+ const std::string& characteristicUuid, const std::string& descriptorUuid, const std::string& error)
251
289
  {
252
- mCallback->call([uuid, serviceUuid, characteristicUuid,
253
- descriptorUuid](Napi::Env env, std::vector<napi_value>& args) {
290
+ mCallback->call([uuid, serviceUuid, characteristicUuid, descriptorUuid, error](Napi::Env env, std::vector<napi_value>& args) {
254
291
  // emit('valueWrite', deviceUuid, serviceUuid, characteristicUuid, descriptorUuid);
255
292
  args = { _s("valueWrite"), _u(uuid), _u(serviceUuid), _u(characteristicUuid),
256
- _u(descriptorUuid) };
293
+ _u(descriptorUuid), error.empty() ? env.Null() : _e(error) };
257
294
  });
258
295
  }
259
296
 
260
- void Emit::ReadHandle(const std::string& uuid, int descriptorHandle, const Data& data)
297
+ void Emit::ReadHandle(const std::string& uuid, int descriptorHandle, const Data& data, const std::string& error)
261
298
  {
262
- mCallback->call([uuid, descriptorHandle, data](Napi::Env env, std::vector<napi_value>& args) {
299
+ mCallback->call([uuid, descriptorHandle, data, error](Napi::Env env, std::vector<napi_value>& args) {
263
300
  // emit('handleRead', deviceUuid, descriptorHandle, data);
264
- args = { _s("handleRead"), _u(uuid), _n(descriptorHandle), toBuffer(env, data) };
301
+ args = {
302
+ _s("handleRead"),
303
+ _u(uuid),
304
+ _n(descriptorHandle),
305
+ error.empty() ? toBuffer(env, data) : env.Null(),
306
+ error.empty() ? env.Null() : _e(error)
307
+ };
265
308
  });
266
309
  }
267
310
 
268
- void Emit::WriteHandle(const std::string& uuid, int descriptorHandle)
311
+ void Emit::WriteHandle(const std::string& uuid, int descriptorHandle, const std::string& error)
269
312
  {
270
- mCallback->call([uuid, descriptorHandle](Napi::Env env, std::vector<napi_value>& args) {
313
+ mCallback->call([uuid, descriptorHandle, error](Napi::Env env, std::vector<napi_value>& args) {
271
314
  // emit('handleWrite', deviceUuid, descriptorHandle);
272
- args = { _s("handleWrite"), _u(uuid), _n(descriptorHandle) };
315
+ args = { _s("handleWrite"), _u(uuid), _n(descriptorHandle), error.empty() ? env.Null() : _e(error) };
273
316
  });
274
317
  }
package/lib/descriptor.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 descriptors = require('./descriptors.json');
5
3
 
6
- function Descriptor (noble, peripheralId, serviceUuid, characteristicUuid, uuid) {
7
- this._noble = noble;
8
- this._peripheralId = peripheralId;
9
- this._serviceUuid = serviceUuid;
10
- this._characteristicUuid = characteristicUuid;
4
+ class Descriptor extends EventEmitter {
5
+ constructor (noble, peripheralId, serviceUuid, characteristicUuid, uuid) {
6
+ super();
7
+
8
+ this._noble = noble;
9
+ this._peripheralId = peripheralId;
10
+ this._serviceUuid = serviceUuid;
11
+ this._characteristicUuid = characteristicUuid;
11
12
 
12
- this.uuid = uuid;
13
- this.name = null;
14
- this.type = null;
13
+ this.uuid = uuid;
14
+ this.name = null;
15
+ this.type = null;
15
16
 
16
- const descriptor = descriptors[uuid];
17
- if (descriptor) {
18
- this.name = descriptor.name;
19
- this.type = descriptor.type;
17
+ const descriptor = descriptors[uuid];
18
+ if (descriptor) {
19
+ this.name = descriptor.name;
20
+ this.type = descriptor.type;
21
+ }
20
22
  }
21
- }
22
23
 
23
- util.inherits(Descriptor, events.EventEmitter);
24
+ toString () {
25
+ return JSON.stringify({
26
+ uuid: this.uuid,
27
+ name: this.name,
28
+ type: this.type
29
+ });
30
+ }
24
31
 
25
- Descriptor.prototype.toString = function () {
26
- return JSON.stringify({
27
- uuid: this.uuid,
28
- name: this.name,
29
- type: this.type
30
- });
31
- };
32
+ readValue (callback) {
33
+ if (callback) {
34
+ this.once('valueRead', (data, error) => callback(error, data));
35
+ }
36
+ this._noble.readValue(
37
+ this._peripheralId,
38
+ this._serviceUuid,
39
+ this._characteristicUuid,
40
+ this.uuid
41
+ );
42
+ }
32
43
 
33
- const readValue = function (callback) {
34
- if (callback) {
35
- this.once('valueRead', data => {
36
- callback(null, data);
44
+ async readValueAsync () {
45
+ return new Promise((resolve, reject) => {
46
+ this.readValue((error, data) => error ? reject(error) : resolve(data));
37
47
  });
38
48
  }
39
- this._noble.readValue(
40
- this._peripheralId,
41
- this._serviceUuid,
42
- this._characteristicUuid,
43
- this.uuid
44
- );
45
- };
46
49
 
47
- Descriptor.prototype.readValue = readValue;
48
- Descriptor.prototype.readValueAsync = util.promisify(readValue);
50
+ writeValue (data, callback) {
51
+ if (!(data instanceof Buffer)) {
52
+ throw new Error('data must be a Buffer');
53
+ }
49
54
 
50
- const writeValue = function (data, callback) {
51
- if (!(data instanceof Buffer)) {
52
- throw new Error('data must be a Buffer');
55
+ if (callback) {
56
+ this.once('valueWrite', error => callback(error));
57
+ }
58
+ this._noble.writeValue(
59
+ this._peripheralId,
60
+ this._serviceUuid,
61
+ this._characteristicUuid,
62
+ this.uuid,
63
+ data
64
+ );
53
65
  }
54
66
 
55
- if (callback) {
56
- this.once('valueWrite', () => {
57
- callback(null);
67
+ // Using modern async/await pattern instead of util.promisify
68
+ async writeValueAsync (data) {
69
+ return new Promise((resolve, reject) => {
70
+ this.writeValue(data, error => error ? reject(error) : resolve());
58
71
  });
59
72
  }
60
- this._noble.writeValue(
61
- this._peripheralId,
62
- this._serviceUuid,
63
- this._characteristicUuid,
64
- this.uuid,
65
- data
66
- );
67
- };
68
-
69
- Descriptor.prototype.writeValue = writeValue;
70
- Descriptor.prototype.writeValueAsync = util.promisify(writeValue);
73
+ }
71
74
 
72
75
  module.exports = Descriptor;
@@ -1,6 +1,4 @@
1
- const events = require('events');
2
- const util = require('util');
3
-
1
+ const { EventEmitter } = require('events');
4
2
  const Smp = require('./smp');
5
3
 
6
4
  const AclStream = function (hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress) {
@@ -18,7 +16,7 @@ const AclStream = function (hci, handle, localAddressType, localAddress, remoteA
18
16
  this._smp.on('end', this.onSmpEndBinded);
19
17
  };
20
18
 
21
- util.inherits(AclStream, events.EventEmitter);
19
+ Object.setPrototypeOf(AclStream.prototype, EventEmitter.prototype);
22
20
 
23
21
  AclStream.prototype.encrypt = function () {
24
22
  this._smp.sendPairingRequest();