@stoprocent/noble 2.1.7 → 2.2.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.
@@ -13,6 +13,7 @@ try {
13
13
  }
14
14
 
15
15
  const starTime = Date.now();
16
+ let cancelConnectTimeout;
16
17
 
17
18
  let peripheral;
18
19
  async function main () {
@@ -20,12 +21,13 @@ async function main () {
20
21
  await noble.waitForPoweredOnAsync();
21
22
 
22
23
  // Cancel the connection after 5 seconds if it is still connecting
23
- setTimeout(() => {
24
+ cancelConnectTimeout = setTimeout(() => {
24
25
  noble.cancelConnect(peripheralIdOrAddress);
25
26
  }, 5000);
26
27
 
27
28
  if (directConnect === '1') {
28
29
  peripheral = await noble.connectAsync(peripheralIdOrAddress, { addressType });
30
+ clearTimeout(cancelConnectTimeout);
29
31
  await explore(peripheral);
30
32
  } else {
31
33
  await noble.startScanningAsync([], false);
@@ -92,13 +94,18 @@ const explore = async (peripheral) => {
92
94
  console.log('noble stopped');
93
95
  });
94
96
 
97
+ peripheral.on('mtu', (mtu) => {
98
+ console.log('MTU Updated: ', mtu);
99
+ });
100
+
95
101
  if (peripheral.state !== 'connected') {
96
102
  await peripheral.connectAsync();
103
+ clearTimeout(cancelConnectTimeout);
97
104
  }
98
105
 
99
106
  const rssi = await peripheral.updateRssiAsync();
100
107
  console.log('RSSI', rssi);
101
-
108
+
102
109
  const services = await peripheral.discoverServicesAsync([]);
103
110
 
104
111
  for (const service of services) {
@@ -18,6 +18,7 @@ public:
18
18
  void Scan(const std::string& uuid, int rssi, const Peripheral& peripheral);
19
19
  void Connected(const std::string& uuid, const std::string& error = "");
20
20
  void Disconnected(const std::string& uuid);
21
+ void MTU(const std::string& uuid, int mtu);
21
22
  void RSSI(const std::string& uuid, int rssi, const std::string& error = "");
22
23
  void ServicesDiscovered(const std::string& uuid, const std::vector<std::string>& serviceUuids, const std::string& error = "");
23
24
  void IncludedServicesDiscovered(const std::string& uuid, const std::string& serviceUuid, const std::vector<std::string>& serviceUuids, const std::string& error = "");
@@ -162,6 +162,14 @@ void Emit::Disconnected(const std::string& uuid)
162
162
  });
163
163
  }
164
164
 
165
+ void Emit::MTU(const std::string& uuid, int mtu)
166
+ {
167
+ mCallback->call([uuid, mtu](Napi::Env env, std::vector<napi_value>& args) {
168
+ // emit('onMtu', deviceUuid, mtu);
169
+ args = { _s("onMtu"), _u(uuid), _n(mtu) };
170
+ });
171
+ }
172
+
165
173
  void Emit::RSSI(const std::string& uuid, int rssi, const std::string& error)
166
174
  {
167
175
  mCallback->call([uuid, rssi, error](Napi::Env env, std::vector<napi_value>& args) {
@@ -14,8 +14,10 @@
14
14
  @property (assign) CBManagerState lastState;
15
15
  @property dispatch_queue_t dispatchQueue;
16
16
  @property NSMutableDictionary *peripherals;
17
+ @property NSMutableDictionary *mtus;
17
18
  @property NSMutableSet *discovered;
18
19
 
20
+
19
21
  - (instancetype)init: (const Napi::Value&) receiver with: (const Napi::Function&) callback;
20
22
  - (void)scan: (NSArray<NSString*> *)serviceUUIDs allowDuplicates: (BOOL)allowDuplicates;
21
23
  - (void)stopScan;
@@ -4,6 +4,10 @@
4
4
 
5
5
  #import <Foundation/Foundation.h>
6
6
 
7
+ @interface BLEManager ()
8
+ - (void)updateMtuForPeripheral:(CBPeripheral*) peripheral;
9
+ @end
10
+
7
11
  @implementation BLEManager
8
12
 
9
13
  - (instancetype)init: (const Napi::Value&) receiver with: (const Napi::Function&) callback
@@ -16,10 +20,20 @@
16
20
  self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.dispatchQueue];
17
21
  self.discovered = [NSMutableSet set];
18
22
  self.peripherals = [NSMutableDictionary dictionaryWithCapacity:10];
23
+ self.mtus = [NSMutableDictionary dictionaryWithCapacity:10];
19
24
  }
20
25
  return self;
21
26
  }
22
27
 
28
+ - (void)updateMtuForPeripheral:(CBPeripheral*) peripheral {
29
+ NSUInteger mtu = [peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithoutResponse];
30
+ NSNumber *mtuNumber = [self.mtus objectForKey: peripheral.identifier];
31
+ if (!mtuNumber || [mtuNumber unsignedIntegerValue] != mtu) {
32
+ emit.MTU(getUuid(peripheral), mtu);
33
+ [self.mtus setObject:[NSNumber numberWithInt:mtu] forKey: peripheral.identifier];
34
+ }
35
+ }
36
+
23
37
  - (void)centralManagerDidUpdateState:(CBCentralManager *)central
24
38
  {
25
39
  if (central.state != self.lastState && self.lastState == CBManagerStatePoweredOff && central.state == CBManagerStatePoweredOn) {
@@ -154,6 +168,8 @@
154
168
 
155
169
  std::string uuid = getUuid(peripheral);
156
170
  emit.Connected(uuid, "");
171
+ [self updateMtuForPeripheral:peripheral];
172
+
157
173
  }
158
174
 
159
175
  - (void) centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
@@ -213,6 +229,7 @@
213
229
  - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
214
230
  std::string uuid = getUuid(peripheral);
215
231
  std::vector<std::string> services = getServices(peripheral.services);
232
+ [self updateMtuForPeripheral:peripheral];
216
233
  emit.ServicesDiscovered(uuid, services, error ? error.localizedDescription.UTF8String : "");
217
234
  }
218
235
 
@@ -263,6 +280,7 @@
263
280
  std::string uuid = getUuid(peripheral);
264
281
  std::string serviceUuid = std::string([service.UUID.UUIDString UTF8String]);
265
282
  auto characteristics = getCharacteristics(service.characteristics);
283
+ [self updateMtuForPeripheral:peripheral];
266
284
  emit.CharacteristicsDiscovered(uuid, serviceUuid, characteristics, error ? error.localizedDescription.UTF8String : "");
267
285
  }
268
286
 
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "license": "MIT",
7
7
  "name": "@stoprocent/noble",
8
8
  "description": "A Node.js BLE (Bluetooth Low Energy) central library.",
9
- "version": "2.1.7",
9
+ "version": "2.2.0",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"