update libs
This commit is contained in:
@@ -1,13 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file BleInterfaces.h
|
||||
*
|
||||
* Created: 2022
|
||||
* License: GNU GENERAL PUBLIC LICENSE (see LICENSE)
|
||||
*
|
||||
* This library provides a BLE scanner to be used by other libraries to
|
||||
* receive advertisements from BLE devices
|
||||
*
|
||||
*/
|
||||
|
||||
#include <NimBLEDevice.h>
|
||||
|
||||
class BLEScannerSubscriber {
|
||||
namespace BleScanner {
|
||||
|
||||
|
||||
class Subscriber {
|
||||
public:
|
||||
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
|
||||
};
|
||||
|
||||
class BLEScannerPublisher {
|
||||
class Publisher {
|
||||
public:
|
||||
virtual void subscribe(BLEScannerSubscriber* subscriber) = 0;
|
||||
virtual void unsubscribe(BLEScannerSubscriber* subscriber) = 0;
|
||||
virtual void subscribe(Subscriber* subscriber) = 0;
|
||||
virtual void unsubscribe(Subscriber* subscriber) = 0;
|
||||
};
|
||||
|
||||
} // namespace BleScanner
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
/**
|
||||
* @file BleScanner.cpp
|
||||
*
|
||||
* Created: 2022
|
||||
* License: GNU GENERAL PUBLIC LICENSE (see LICENSE)
|
||||
*
|
||||
* This library provides a BLE scanner to be used by other libraries to
|
||||
* receive advertisements from BLE devices
|
||||
*
|
||||
*/
|
||||
|
||||
#include "BleScanner.h"
|
||||
#include <NimBLEUtils.h>
|
||||
#include <NimBLEScan.h>
|
||||
#include <NimBLEAdvertisedDevice.h>
|
||||
|
||||
BleScanner::BleScanner(int reservedSubscribers) {
|
||||
namespace BleScanner {
|
||||
|
||||
Scanner::Scanner(int reservedSubscribers) {
|
||||
subscribers.reserve(reservedSubscribers);
|
||||
}
|
||||
|
||||
void BleScanner::initialize(const std::string& deviceName, const bool wantDuplicates, const uint16_t interval, const uint16_t window) {
|
||||
void Scanner::initialize(const std::string& deviceName, const bool wantDuplicates, const uint16_t interval, const uint16_t window) {
|
||||
if (!BLEDevice::getInitialized()) {
|
||||
BLEDevice::init(deviceName);
|
||||
}
|
||||
@@ -18,7 +31,7 @@ void BleScanner::initialize(const std::string& deviceName, const bool wantDuplic
|
||||
bleScan->setWindow(window);
|
||||
}
|
||||
|
||||
void BleScanner::update() {
|
||||
void Scanner::update() {
|
||||
if (bleScan->isScanning()) {
|
||||
return;
|
||||
}
|
||||
@@ -28,27 +41,28 @@ void BleScanner::update() {
|
||||
}
|
||||
}
|
||||
|
||||
void BleScanner::setScanDuration(const uint32_t value) {
|
||||
void Scanner::setScanDuration(const uint32_t value) {
|
||||
scanDuration = value;
|
||||
}
|
||||
|
||||
void BleScanner::subscribe(BLEScannerSubscriber* subscriber) {
|
||||
void Scanner::subscribe(Subscriber* subscriber) {
|
||||
if (std::find(subscribers.begin(), subscribers.end(), subscriber) != subscribers.end()) {
|
||||
return;
|
||||
}
|
||||
subscribers.push_back(subscriber);
|
||||
}
|
||||
|
||||
void BleScanner::unsubscribe(BLEScannerSubscriber* subscriber) {
|
||||
void Scanner::unsubscribe(Subscriber* subscriber) {
|
||||
auto it = std::find(subscribers.begin(), subscribers.end(), subscriber);
|
||||
if (it != subscribers.end()) {
|
||||
subscribers.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void BleScanner::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
|
||||
void Scanner::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
|
||||
for (const auto& subscriber : subscribers) {
|
||||
subscriber->onResult(advertisedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BleScanner
|
||||
|
||||
@@ -1,25 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file BleScanner.h
|
||||
*
|
||||
* Created: 2022
|
||||
* License: GNU GENERAL PUBLIC LICENSE (see LICENSE)
|
||||
*
|
||||
* This library provides a BLE scanner to be used by other libraries to
|
||||
* receive advertisements from BLE devices
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <string>
|
||||
#include <NimBLEDevice.h>
|
||||
#include "BleInterfaces.h"
|
||||
|
||||
class BleScanner : public BLEScannerPublisher, BLEAdvertisedDeviceCallbacks {
|
||||
public:
|
||||
BleScanner(int reservedSubscribers = 10);
|
||||
~BleScanner() = default;
|
||||
namespace BleScanner {
|
||||
|
||||
void initialize(const std::string& deviceName = "blescanner", const bool wantDuplicates = false, const uint16_t interval = 40, const uint16_t window = 40);
|
||||
class Scanner : public Publisher, BLEAdvertisedDeviceCallbacks {
|
||||
public:
|
||||
Scanner(int reservedSubscribers = 10);
|
||||
~Scanner() = default;
|
||||
|
||||
/**
|
||||
* @brief Initializes the BLE scanner
|
||||
*
|
||||
* @param deviceName
|
||||
* @param wantDuplicates true if you want to receive advertisements from devices for which an advertisement was allready received within the same scan (scanduration)
|
||||
* @param interval Time in ms from the start of a window until the start of the next window (so the interval time = window + time to wait until next window)
|
||||
* @param window time in ms to scan
|
||||
*
|
||||
* The default (same) interval and window parameters means continuesly scanning without delay between windows
|
||||
*/
|
||||
void initialize(const std::string& deviceName = "blescanner", const bool wantDuplicates = true, const uint16_t interval = 23, const uint16_t window = 23);
|
||||
|
||||
/**
|
||||
* @brief starts the scan if not allready running, this should be called in loop() or a task;
|
||||
*
|
||||
*/
|
||||
void update();
|
||||
|
||||
/**
|
||||
* @brief Set the Scan Duration
|
||||
*
|
||||
* @param value scan duration in seconds
|
||||
*/
|
||||
void setScanDuration(const uint32_t value);
|
||||
|
||||
void subscribe(BLEScannerSubscriber* subscriber) override;
|
||||
void unsubscribe(BLEScannerSubscriber* subscriber) override;
|
||||
/**
|
||||
* @brief Subscribe to the scanner and receive results
|
||||
*
|
||||
* @param subscriber
|
||||
*/
|
||||
void subscribe(Subscriber* subscriber) override;
|
||||
|
||||
/**
|
||||
* @brief Un-Subscribe the scanner
|
||||
*
|
||||
* @param subscriber
|
||||
*/
|
||||
void unsubscribe(Subscriber* subscriber) override;
|
||||
|
||||
/**
|
||||
* @brief Forwards the scan result to the subcriber which has the onResult implemented
|
||||
*
|
||||
* @param advertisedDevice
|
||||
*/
|
||||
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
|
||||
|
||||
private:
|
||||
uint32_t scanDuration = 3;
|
||||
BLEScan* bleScan = nullptr;
|
||||
std::vector<BLEScannerSubscriber*> subscribers;
|
||||
std::vector<Subscriber*> subscribers;
|
||||
};
|
||||
|
||||
} // namespace BleScanner
|
||||
|
||||
|
||||
Reference in New Issue
Block a user