@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.
- package/README.md +393 -650
- package/examples/advertisement-discovery.js +57 -48
- package/examples/connect-address.js +59 -34
- package/examples/echo.js +59 -69
- package/examples/enter-exit.js +55 -49
- package/examples/multiple-bindings.js +53 -0
- package/examples/peripheral-explorer-async.js +39 -21
- package/examples/peripheral-explorer.ts +52 -0
- package/index.d.ts +249 -209
- package/index.js +4 -1
- package/jest.config.js +4 -0
- package/lib/characteristic.js +153 -127
- package/lib/{win/src/callbacks.h → common/include/Emit.h} +17 -14
- package/lib/common/include/Peripheral.h +31 -0
- package/lib/common/include/ThreadSafeCallback.h +95 -0
- package/lib/{win/src/callbacks.cc → common/src/Emit.cc} +111 -68
- package/lib/descriptor.js +57 -54
- package/lib/hci-socket/acl-stream.js +2 -4
- package/lib/hci-socket/bindings.js +96 -73
- package/lib/hci-socket/gap.js +2 -3
- package/lib/hci-socket/gatt.js +2 -5
- package/lib/hci-socket/hci.js +19 -7
- package/lib/hci-socket/signaling.js +2 -3
- package/lib/hci-socket/smp.js +2 -3
- package/lib/hci-socket/vs.js +1 -0
- package/lib/mac/binding.gyp +5 -7
- package/lib/mac/bindings.js +1 -3
- package/lib/mac/src/ble_manager.h +1 -8
- package/lib/mac/src/ble_manager.mm +87 -44
- package/lib/mac/src/napi_objc.h +1 -0
- package/lib/mac/src/napi_objc.mm +0 -6
- package/lib/mac/src/noble_mac.h +5 -3
- package/lib/mac/src/noble_mac.mm +99 -57
- package/lib/mac/src/objc_cpp.h +3 -2
- package/lib/mac/src/objc_cpp.mm +0 -6
- package/lib/noble.js +579 -488
- package/lib/peripheral.js +171 -174
- package/lib/resolve-bindings.js +37 -30
- package/lib/service.js +58 -55
- package/lib/win/binding.gyp +4 -11
- package/lib/win/bindings.js +1 -3
- package/lib/win/src/ble_manager.cc +291 -166
- package/lib/win/src/ble_manager.h +11 -13
- package/lib/win/src/napi_winrt.cc +1 -7
- package/lib/win/src/napi_winrt.h +1 -1
- package/lib/win/src/noble_winrt.cc +88 -61
- package/lib/win/src/noble_winrt.h +5 -3
- package/lib/win/src/notify_map.cc +0 -7
- package/lib/win/src/notify_map.h +1 -8
- package/lib/win/src/peripheral_winrt.cc +29 -11
- package/lib/win/src/peripheral_winrt.h +1 -1
- package/lib/win/src/radio_watcher.cc +79 -69
- package/lib/win/src/radio_watcher.h +30 -11
- package/lib/win/src/winrt_cpp.cc +1 -1
- package/lib/win/src/winrt_cpp.h +3 -0
- package/package.json +14 -17
- package/prebuilds/darwin-x64+arm64/@stoprocent+noble.node +0 -0
- package/prebuilds/win32-ia32/@stoprocent+noble.node +0 -0
- package/prebuilds/win32-x64/@stoprocent+noble.node +0 -0
- package/test/lib/characteristic.test.js +202 -322
- package/test/lib/descriptor.test.js +62 -95
- package/test/lib/hci-socket/acl-stream.test.js +112 -108
- package/test/lib/hci-socket/bindings.test.js +576 -365
- package/test/lib/hci-socket/hci.test.js +442 -473
- package/test/lib/hci-socket/signaling.test.js +45 -48
- package/test/lib/hci-socket/smp.test.js +144 -142
- package/test/lib/hci-socket/vs.test.js +193 -18
- package/test/lib/peripheral.test.js +492 -322
- package/test/lib/resolve-bindings.test.js +207 -82
- package/test/lib/service.test.js +79 -88
- package/test/noble.test.js +381 -1085
- package/.editorconfig +0 -11
- package/.nycrc.json +0 -4
- package/codecov.yml +0 -5
- package/examples/cache-gatt-discovery.js +0 -198
- package/examples/cache-gatt-reconnect.js +0 -164
- package/examples/ext-advertisement-discovery.js +0 -65
- package/examples/peripheral-explorer.js +0 -225
- package/examples/pizza/central.js +0 -194
- package/examples/pizza/pizza.js +0 -60
- package/examples/test/test.custom.js +0 -131
- package/examples/uart-bind-params.js +0 -28
- package/lib/distributed/bindings.js +0 -326
- package/lib/mac/src/callbacks.cc +0 -222
- package/lib/mac/src/callbacks.h +0 -84
- package/lib/mac/src/peripheral.h +0 -23
- package/lib/resolve-bindings-web.js +0 -9
- package/lib/webbluetooth/bindings.js +0 -368
- package/lib/websocket/bindings.js +0 -321
- package/lib/win/src/peripheral.h +0 -23
- package/test/lib/distributed/bindings.test.js +0 -918
- package/test/lib/webbluetooth/bindings.test.js +0 -190
- package/test/lib/websocket/bindings.test.js +0 -456
- package/test/mocha.setup.js +0 -0
- package/with-bindings.js +0 -5
- package/with-custom-binding.js +0 -6
- 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
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
advertisment.Set(_s("
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
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() :
|
|
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 = {
|
|
200
|
-
|
|
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
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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 = {
|
|
245
|
-
|
|
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 = {
|
|
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
|
|
2
|
-
const util = require('util');
|
|
3
|
-
|
|
1
|
+
const { EventEmitter } = require('events');
|
|
4
2
|
const descriptors = require('./descriptors.json');
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
this.uuid = uuid;
|
|
14
|
+
this.name = null;
|
|
15
|
+
this.type = null;
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
24
|
+
toString () {
|
|
25
|
+
return JSON.stringify({
|
|
26
|
+
uuid: this.uuid,
|
|
27
|
+
name: this.name,
|
|
28
|
+
type: this.type
|
|
29
|
+
});
|
|
30
|
+
}
|
|
24
31
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
48
|
-
|
|
50
|
+
writeValue (data, callback) {
|
|
51
|
+
if (!(data instanceof Buffer)) {
|
|
52
|
+
throw new Error('data must be a Buffer');
|
|
53
|
+
}
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
19
|
+
Object.setPrototypeOf(AclStream.prototype, EventEmitter.prototype);
|
|
22
20
|
|
|
23
21
|
AclStream.prototype.encrypt = function () {
|
|
24
22
|
this._smp.sendPairingRequest();
|