simplernble 1.2.1-dev100 → 1.2.1-dev104

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/native/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1-dev100
1
+ 1.2.1-dev104
@@ -21,14 +21,8 @@ AdapterAndroid::AdapterAndroid() {
21
21
  _btScanCallback.set_callback_onScanResult([this](Android::ScanResult scan_result) {
22
22
  std::string address = scan_result.getDevice().getAddress();
23
23
 
24
- if (this->peripherals_.count(address) == 0) {
25
- // If the incoming peripheral has never been seen before, create and save a reference to it.
26
- auto base_peripheral = std::make_shared<PeripheralAndroid>(scan_result.getDevice());
27
- this->peripherals_.insert(std::make_pair(address, base_peripheral));
28
- }
29
-
30
24
  // Update the received advertising data.
31
- auto base_peripheral = this->peripherals_.at(address);
25
+ auto base_peripheral = this->cached_peripheral(scan_result.getDevice());
32
26
  base_peripheral->update_advertising_data(scan_result);
33
27
 
34
28
  // Convert the base object into an external-facing Peripheral object
@@ -101,12 +95,24 @@ SharedPtrVector<PeripheralBase> AdapterAndroid::get_paired_peripherals() {
101
95
 
102
96
  auto paired_list = _btAdapter.getBondedDevices();
103
97
  for (auto& device : paired_list) {
104
- peripherals.push_back(std::make_shared<PeripheralAndroid>(device));
98
+ // Reuse the wrapper a scan made, so that callbacks set on it keep working.
99
+ peripherals.push_back(cached_peripheral(device));
105
100
  }
106
101
 
107
102
  return peripherals;
108
103
  }
109
104
 
105
+ std::shared_ptr<PeripheralAndroid> AdapterAndroid::cached_peripheral(const Android::BluetoothDevice& device) {
106
+ std::string address = device.getAddress();
107
+
108
+ std::scoped_lock lock(peripherals_mutex_);
109
+ if (peripherals_.count(address) == 0) {
110
+ // If the peripheral has never been seen before, create and save a reference to it.
111
+ peripherals_.insert(std::make_pair(address, std::make_shared<PeripheralAndroid>(device)));
112
+ }
113
+ return peripherals_.at(address);
114
+ }
115
+
110
116
  std::shared_ptr<Local::PeripheralBase> AdapterAndroid::create_local_peripheral() {
111
117
  return std::make_shared<Local::PeripheralAndroid>(_btAdapter);
112
118
  }
@@ -10,6 +10,7 @@
10
10
  #include <atomic>
11
11
  #include <functional>
12
12
  #include <memory>
13
+ #include <mutex>
13
14
  #include <string>
14
15
  #include <vector>
15
16
 
@@ -60,6 +61,10 @@ class AdapterAndroid : public AdapterBase {
60
61
  Android::BluetoothScanner _btScanner = _btAdapter.getBluetoothLeScanner();
61
62
  Android::Bridge::ScanCallback _btScanCallback;
62
63
 
64
+ // Returns the wrapper for a device, creating it the first time, so that scans and the paired list share it.
65
+ std::shared_ptr<PeripheralAndroid> cached_peripheral(const Android::BluetoothDevice& device);
66
+
67
+ std::mutex peripherals_mutex_;
63
68
  std::map<BluetoothAddress, std::shared_ptr<PeripheralAndroid>> peripherals_;
64
69
  std::map<BluetoothAddress, std::shared_ptr<PeripheralAndroid>> seen_peripherals_;
65
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplernble",
3
- "version": "1.2.1-dev100",
3
+ "version": "1.2.1-dev104",
4
4
  "description": "SimpleBLE for React Native: a cross-platform Bluetooth Low Energy library for iOS and Android",
5
5
  "keywords": [
6
6
  "react-native",