Skip to content

Commit

Permalink
Added power on/off. Added discoverable flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdewald committed Oct 20, 2024
1 parent d10edd6 commit 55599a4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
9 changes: 9 additions & 0 deletions examples/simplebluez/incoming/incoming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ int main(int argc, char* argv[]) {
std::thread* async_thread = new std::thread(async_thread_function);
auto adapter = bluez->get_adapters()[0];

if (!adapter->powered()) {
std::cout << "Powering on adapter..." << std::endl;
adapter->powered(true);
}

std::cout << "Initializing SimpleBluez Peripheral Mode Demo" << std::endl;
auto service_manager = bluez->get_custom_service_manager();
auto advertisement_manager = bluez->get_custom_advertisement_manager();
Expand Down Expand Up @@ -105,6 +110,7 @@ int main(int argc, char* argv[]) {
data[0x1024] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
advertisement->manufacturer_data(data);
advertisement->timeout(10);
advertisement->discoverable(true);
advertisement->local_name("SimpleBluez");

// --- MAIN EVENT LOOP ---
Expand Down Expand Up @@ -136,6 +142,9 @@ int main(int argc, char* argv[]) {
adapter->unregister_advertisement(advertisement);
adapter->unregister_application(service_manager->path());

std::cout << "Powering off adapter..." << std::endl;
adapter->powered(false);

async_thread_active = false;
async_thread->join();
delete async_thread;
Expand Down
1 change: 1 addition & 0 deletions simplebluez/include/simplebluez/Adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Adapter : public SimpleDBus::Proxy {
std::string address();
bool discovering();
bool powered();
void powered(bool powered);

void discovery_filter(const DiscoveryFilter& filter);
void discovery_start();
Expand Down
3 changes: 2 additions & 1 deletion simplebluez/include/simplebluez/interfaces/Adapter1.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Adapter1 : public SimpleDBus::Interface {

// ----- PROPERTIES -----
bool Discovering(bool refresh = true);
bool Powered(bool refresh = true);
bool Powered();
void Powered(bool powered);
std::string Address();

protected:
Expand Down
2 changes: 2 additions & 0 deletions simplebluez/src/Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ bool Adapter::discovering() { return adapter1()->Discovering(); }

bool Adapter::powered() { return adapter1()->Powered(); }

void Adapter::powered(bool powered) { adapter1()->Powered(powered); }

void Adapter::discovery_filter(const DiscoveryFilter& filter) { adapter1()->SetDiscoveryFilter(filter); }

void Adapter::discovery_start() { adapter1()->StartDiscovery(); }
Expand Down
21 changes: 16 additions & 5 deletions simplebluez/src/interfaces/Adapter1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "simplebluez/interfaces/Adapter1.h"

#include "simpledbus/interfaces/Properties.h"
#include "simpledbus/advanced/Proxy.h"

using namespace SimpleBluez;

Adapter1::Adapter1(std::shared_ptr<SimpleDBus::Connection> conn, SimpleDBus::Proxy* proxy)
Expand Down Expand Up @@ -97,15 +100,23 @@ bool Adapter1::Discovering(bool refresh) {
return _properties["Discovering"].get_boolean();
}

bool Adapter1::Powered(bool refresh) {
if (refresh) {
property_refresh("Powered");
}

bool Adapter1::Powered() {
std::scoped_lock lock(_property_update_mutex);
return _properties["Powered"].get_boolean();
}

void Adapter1::Powered(bool powered) {
SimpleDBus::Holder powered_h = SimpleDBus::Holder::create_boolean(powered);

{
std::scoped_lock lock(_property_update_mutex);
_properties["Powered"] = powered_h;
}

std::shared_ptr<SimpleDBus::Properties> properties = std::dynamic_pointer_cast<SimpleDBus::Properties>(_proxy->interface_get("org.freedesktop.DBus.Properties"));
properties->Set("org.bluez.Adapter1", "Powered", powered_h);
}

std::string Adapter1::Address() {
std::scoped_lock lock(_property_update_mutex);
return _properties["Address"].get_string();
Expand Down

0 comments on commit 55599a4

Please sign in to comment.