@stoprocent/noble 2.2.0 → 2.3.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.
@@ -5,6 +5,7 @@
5
5
  #include <winrt/Windows.Storage.Streams.h>
6
6
  #include <winrt/Windows.Security.Cryptography.h>
7
7
  #include <winrt/Windows.Devices.Bluetooth.h>
8
+ #include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
8
9
 
9
10
 
10
11
  using winrt::Windows::Devices::Bluetooth::BluetoothCacheMode;
@@ -17,6 +18,7 @@ using winrt::Windows::Storage::Streams::IBuffer;
17
18
  using winrt::Windows::Storage::Streams::ByteOrder;
18
19
  using winrt::Windows::Security::Cryptography::CryptographicBuffer;
19
20
  using winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementBytePattern;
21
+ using winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattSession;
20
22
 
21
23
  template <typename T> auto inFilter(std::vector<T> filter, T object)
22
24
  {
@@ -324,6 +326,10 @@ void BLEManager::OnConnected(IAsyncOperation<BluetoothLEDevice> asyncOp, AsyncSt
324
326
  peripheral.device = device;
325
327
  peripheral.connectionToken = token;
326
328
  mEmit.Connected(uuid);
329
+
330
+ // Get GATT session to access the MTU
331
+ auto completed = bind2(this, &BLEManager::OnGattSessionCreated, uuid);
332
+ GattSession::FromDeviceIdAsync(device.BluetoothDeviceId()).Completed(completed);
327
333
  }
328
334
  else
329
335
  {
@@ -336,6 +342,46 @@ void BLEManager::OnConnected(IAsyncOperation<BluetoothLEDevice> asyncOp, AsyncSt
336
342
  }
337
343
  }
338
344
 
345
+ void BLEManager::OnGattSessionCreated(IAsyncOperation<GattSession> asyncOp, AsyncStatus status, const std::string uuid)
346
+ {
347
+ if (status == AsyncStatus::Completed)
348
+ {
349
+ auto session = asyncOp.GetResults();
350
+ if (session)
351
+ {
352
+ // MaxPduSize is equivalent to the MTU-3 (MTU minus ATT header)
353
+ // MTU = MaxPduSize + 3
354
+ int mtu = session.MaxPduSize();
355
+ mEmit.MTU(uuid, mtu);
356
+
357
+ // Subscribe to MTU changes
358
+ auto onPduSizeChanged = bind2(this, &BLEManager::OnMaxPduSizeChanged, uuid);
359
+
360
+ // Store both the session and the event token in the peripheral
361
+ PeripheralWinrt& peripheral = mDeviceMap[uuid];
362
+ peripheral.gattSession = session;
363
+ auto token = session.MaxPduSizeChanged(onPduSizeChanged);
364
+ peripheral.maxPduSizeChangedToken = token;
365
+ }
366
+ else
367
+ {
368
+ LOGE("Failed to get GattSession for device %s", uuid.c_str());
369
+ }
370
+ }
371
+ else
372
+ {
373
+ LOGE("Failed to create GattSession: %s", asyncStatusToString(status).c_str());
374
+ }
375
+ }
376
+
377
+ // Add this new method to handle the MaxPduSizeChanged event
378
+ void BLEManager::OnMaxPduSizeChanged(GattSession session, winrt::Windows::Foundation::IInspectable object, const std::string uuid)
379
+ {
380
+ // Update MTU value when it changes
381
+ int mtu = session.MaxPduSize();
382
+ mEmit.MTU(uuid, mtu);
383
+ }
384
+
339
385
  bool BLEManager::Disconnect(const std::string& uuid)
340
386
  {
341
387
  CHECK_DEVICE();
@@ -44,6 +44,8 @@ private:
44
44
  void OnScanStopped(BluetoothLEAdvertisementWatcher watcher, const BluetoothLEAdvertisementWatcherStoppedEventArgs& args);
45
45
  void OnConnected(IAsyncOperation<BluetoothLEDevice> asyncOp, AsyncStatus status, std::string uuid);
46
46
  void OnConnectionStatusChanged(BluetoothLEDevice device, winrt::Windows::Foundation::IInspectable inspectable);
47
+ void OnGattSessionCreated(IAsyncOperation<GattSession> asyncOp, AsyncStatus status, std::string uuid);
48
+ void OnMaxPduSizeChanged(GattSession session, winrt::Windows::Foundation::IInspectable object, std::string uuid);
47
49
  void OnServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp, AsyncStatus status, std::string uuid, std::vector<winrt::guid> serviceUUIDs);
48
50
  void OnIncludedServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::vector<winrt::guid> serviceUUIDs);
49
51
  void OnCharacteristicsDiscovered(IAsyncOperation<GattCharacteristicsResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::vector<winrt::guid> characteristicUUIDs);
@@ -163,6 +163,10 @@ void PeripheralWinrt::Update(const int rssiValue, const BluetoothLEAdvertisement
163
163
  void PeripheralWinrt::Disconnect()
164
164
  {
165
165
  cachedServices.clear();
166
+ if (gattSession.has_value() && maxPduSizeChangedToken)
167
+ {
168
+ gattSession->MaxPduSizeChanged(maxPduSizeChangedToken);
169
+ }
166
170
  if (device.has_value() && connectionToken)
167
171
  {
168
172
  device->Close();
@@ -4,12 +4,14 @@
4
4
  #define WIN32_LEAN_AND_MEAN
5
5
  #include <Windows.h>
6
6
  #include <winrt/Windows.Devices.Bluetooth.Advertisement.h>
7
+ #include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
7
8
 
8
9
  using namespace winrt::Windows::Devices::Bluetooth::Advertisement;
9
10
  using winrt::Windows::Devices::Bluetooth::BluetoothLEDevice;
10
11
  using winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattCharacteristic;
11
12
  using winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattDescriptor;
12
13
  using winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattDeviceService;
14
+ using winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattSession;
13
15
 
14
16
  #include "winrt/Windows.Devices.Bluetooth.h"
15
17
 
@@ -68,6 +70,8 @@ public:
68
70
  uint64_t bluetoothAddress;
69
71
  std::optional<BluetoothLEDevice> device;
70
72
  winrt::event_token connectionToken;
73
+ std::optional<GattSession> gattSession;
74
+ winrt::event_token maxPduSizeChangedToken;
71
75
 
72
76
  private:
73
77
  void GetServiceFromDevice(winrt::guid serviceUuid,
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.2.0",
9
+ "version": "2.3.0",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"