Skip to content

Commit

Permalink
Changed pBLEScan (file static) to _pBLEScan (member)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed Jan 21, 2025
1 parent 366487e commit ee8da04
Showing 1 changed file with 14 additions and 41 deletions.
55 changes: 14 additions & 41 deletions src/BleSensors/BleSensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@

#include "BleSensors.h"

static NimBLEScan *pBLEScan;

class ScanCallbacks : public NimBLEScanCallbacks
{
public:
std::vector<std::string> m_knownBLEAddresses; /// MAC addresses of known sensors
std::vector<ble_sensors_t> *m_sensorData; /// Sensor data
NimBLEScan *m_pBLEScan;

private:
int m_devices_found = 0; /// Number of known devices found
Expand Down Expand Up @@ -138,7 +137,7 @@ class ScanCallbacks : public NimBLEScanCallbacks
if (m_devices_found == m_knownBLEAddresses.size())
{
log_i("All devices found.");
pBLEScan->stop();
m_pBLEScan->stop();
}
}

Expand All @@ -150,7 +149,7 @@ class ScanCallbacks : public NimBLEScanCallbacks

void BleSensors::clearScanResults(void)
{
pBLEScan->clearResults();
_pBLEScan->clearResults();
}

// Set all array members invalid
Expand All @@ -167,47 +166,21 @@ void BleSensors::resetData(void)
*/
unsigned BleSensors::getData(uint32_t scanTime, bool activeScan)
{
// From https://github.com/theengs/decoder/blob/development/examples/ESP32/ScanAndDecode/ScanAndDecode.ino:
// MyAdvertisedDeviceCallbacks are still triggered multiple times; this makes keeping track of received
// sensors difficult. Setting ScanFilterMode to CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE seems to
// restrict callback invocation to once per device as desired.
// NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE);
// NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE);
// NimBLEDevice::setScanDuplicateCacheSize(200);
// NimBLEDevice::init("");

//_pBLEScan = NimBLEDevice::getScan(); //create new scan

// Set the callback for when devices are discovered, no duplicates.
//_pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false);
// MyAdvertisedDeviceCallbacks *myCb = new MyAdvertisedDeviceCallbacks();

// Copy some data required by the Callback
// myCb->m_pBLEScan = _pBLEScan;
// myCb->m_knownBLEAddresses = _known_sensors;
// myCb->m_sensorData = &data;

//_pBLEScan->setAdvertisedDeviceCallbacks(myCb);
//_pBLEScan->setActiveScan(activeScan); // Set active scanning, this will get more data from the advertiser.
//_pBLEScan->setInterval(97); // How often the scan occurs / switches channels; in milliseconds,
//_pBLEScan->setWindow(37); // How long to scan during the interval; in milliseconds.
//_pBLEScan->setMaxResults(0); // do not store the scan results, use callback only.
//_pBLEScan->start(duration, false /* is_continue */);

// New
scanCallbacks.m_knownBLEAddresses = _known_sensors;
scanCallbacks.m_sensorData = &data;
NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE);
NimBLEDevice::setScanDuplicateCacheSize(200);

NimBLEDevice::init("ble-scan");
pBLEScan = NimBLEDevice::getScan();
pBLEScan->setScanCallbacks(&scanCallbacks);
pBLEScan->setActiveScan(activeScan);
pBLEScan->setInterval(97);
pBLEScan->setWindow(37);
// pBLEScan->start(scanTime * 1000, false);
pBLEScan->getResults(scanTime * 1000, false);
_pBLEScan = NimBLEDevice::getScan();
_pBLEScan->setScanCallbacks(&scanCallbacks);
_pBLEScan->setActiveScan(activeScan);
_pBLEScan->setInterval(97);
_pBLEScan->setWindow(37);
scanCallbacks.m_knownBLEAddresses = _known_sensors;
scanCallbacks.m_sensorData = &data;
scanCallbacks.m_pBLEScan = _pBLEScan;
// Start scanning
// Blocks until all known devices are found or scanTime is expired
_pBLEScan->getResults(scanTime * 1000, false);

return 0;
}
Expand Down

0 comments on commit ee8da04

Please sign in to comment.