mirror of
https://github.com/OpenBluetoothToolbox/SimpleBLE
synced 2025-05-08 21:47:10 +08:00
Added callback frontend
This commit is contained in:
parent
3e1379f381
commit
664af9a850
@ -16,8 +16,9 @@ The format is based on `Keep a Changelog`_, and this project adheres to `Semanti
|
||||
|
||||
**Added**
|
||||
|
||||
- (Windows) Added support for powering on and off the adapter.
|
||||
- (Windows) Added support for powering adapters on and off.
|
||||
- (Python) Exposed the `Adapter::power_on()`, `Adapter::power_off()` and `Adapter::is_powered()` methods.
|
||||
- Callbacks to monitor adapter power on and off events.
|
||||
|
||||
**Changed**
|
||||
|
||||
|
@ -41,6 +41,7 @@ set(SIMPLEBLE_SRC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/frontends/base/Descriptor.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/frontends/base/Backend.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/common/AdapterBase.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/common/ServiceBase.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/common/CharacteristicBase.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/common/DescriptorBase.cpp
|
||||
|
@ -47,11 +47,15 @@ class SIMPLEBLE_EXPORT Adapter {
|
||||
/**
|
||||
* Control the power state of the adapter.
|
||||
*
|
||||
* This is only supported on Windows and Linux (implementation pending).
|
||||
* NOTE: The power on/off functionality is only supported on Windows and Linux (implementation pending).
|
||||
* On other platforms, this method will do nothing.
|
||||
* NOTE: Callbacks are currently a placeholder for future implementation.
|
||||
*/
|
||||
void power_on();
|
||||
void power_off();
|
||||
bool is_powered();
|
||||
void set_callback_on_power_on(std::function<void()> on_power_on);
|
||||
void set_callback_on_power_off(std::function<void()> on_power_off);
|
||||
|
||||
void scan_start();
|
||||
void scan_stop();
|
||||
|
21
simpleble/src/backends/common/AdapterBase.cpp
Normal file
21
simpleble/src/backends/common/AdapterBase.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "AdapterBase.h"
|
||||
|
||||
namespace SimpleBLE {
|
||||
|
||||
void AdapterBase::set_callback_on_power_on(std::function<void()> on_power_on) {
|
||||
if (on_power_on) {
|
||||
_callback_on_power_on.load(on_power_on);
|
||||
} else {
|
||||
_callback_on_power_on.unload();
|
||||
}
|
||||
}
|
||||
|
||||
void AdapterBase::set_callback_on_power_off(std::function<void()> on_power_off) {
|
||||
if (on_power_off) {
|
||||
_callback_on_power_off.load(on_power_off);
|
||||
} else {
|
||||
_callback_on_power_off.unload();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -5,11 +5,11 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <simpleble/export.h>
|
||||
|
||||
#include <simpleble/Exceptions.h>
|
||||
#include <simpleble/Types.h>
|
||||
|
||||
#include <kvn_safe_callback.hpp>
|
||||
|
||||
namespace SimpleBLE {
|
||||
|
||||
class Peripheral;
|
||||
@ -22,7 +22,6 @@ class PeripheralBase;
|
||||
*
|
||||
* Notes for implementers:
|
||||
*
|
||||
* - The `initialize()` method is not present because internally we enforce RAII.
|
||||
* - The methods here return shared pointers to AdapterBase, PeripheralBase,
|
||||
* etc. The code in Adapter.cpp will automatically wrap these in Adapter,
|
||||
* Peripheral, etc. objects.
|
||||
@ -39,10 +38,11 @@ class AdapterBase {
|
||||
virtual std::string identifier() = 0;
|
||||
virtual BluetoothAddress address() = 0;
|
||||
|
||||
// NOTE: A default implementation is provided for these methods until all backends have implemented them.
|
||||
virtual void power_on() = 0;
|
||||
virtual void power_off() = 0;
|
||||
virtual bool is_powered() = 0;
|
||||
virtual void set_callback_on_power_on(std::function<void()> on_power_on);
|
||||
virtual void set_callback_on_power_off(std::function<void()> on_power_off);
|
||||
|
||||
virtual void scan_start() = 0;
|
||||
virtual void scan_stop() = 0;
|
||||
@ -67,6 +67,9 @@ class AdapterBase {
|
||||
|
||||
protected:
|
||||
AdapterBase() = default;
|
||||
|
||||
kvn::safe_callback<void()> _callback_on_power_on;
|
||||
kvn::safe_callback<void()> _callback_on_power_off;
|
||||
};
|
||||
|
||||
} // namespace SimpleBLE
|
||||
|
@ -56,6 +56,14 @@ void Adapter::power_off() { (*this)->power_off(); }
|
||||
|
||||
bool Adapter::is_powered() { return (*this)->is_powered(); }
|
||||
|
||||
void Adapter::set_callback_on_power_on(std::function<void()> on_power_on) {
|
||||
(*this)->set_callback_on_power_on(std::move(on_power_on));
|
||||
}
|
||||
|
||||
void Adapter::set_callback_on_power_off(std::function<void()> on_power_off) {
|
||||
(*this)->set_callback_on_power_off(std::move(on_power_off));
|
||||
}
|
||||
|
||||
void Adapter::scan_start() {
|
||||
if (!bluetooth_enabled()) {
|
||||
SIMPLEBLE_LOG_WARN(fmt::format("Bluetooth is not enabled."));
|
||||
|
Loading…
x
Reference in New Issue
Block a user