-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
731 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
|
||
project(EXAMPLE_ADVERTISE) | ||
|
||
message("-- [INFO] Building Example") | ||
add_executable(example_advertise advertise.cpp) | ||
target_link_libraries(example_advertise simplebluez::simplebluez pthread) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <simplebluez/Bluez.h> | ||
|
||
#include <atomic> | ||
#include <chrono> | ||
#include <cstdlib> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <thread> | ||
|
||
SimpleBluez::Bluez bluez; | ||
|
||
std::atomic_bool async_thread_active = true; | ||
void async_thread_function() { | ||
while (async_thread_active) { | ||
bluez.run_async(); | ||
std::this_thread::sleep_for(std::chrono::microseconds(100)); | ||
} | ||
} | ||
|
||
void millisecond_delay(int ms) { | ||
for (int i = 0; i < ms; i++) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
int selection = -1; | ||
|
||
bluez.init(); | ||
std::thread* async_thread = new std::thread(async_thread_function); | ||
|
||
auto adapters = bluez.get_adapters(); | ||
std::cout << "The following adapters were found:" << std::endl; | ||
for (int i = 0; i < adapters.size(); i++) { | ||
std::cout << "[" << i << "] " << adapters[i]->identifier() << " [" << adapters[i]->address() << "]" | ||
<< std::endl; | ||
} | ||
|
||
// std::cout << "Please select an adapter to advertise: "; | ||
// std::cin >> selection; | ||
// if (selection < 0 || selection >= adapters.size()) { | ||
// std::cout << "Invalid selection" << std::endl; | ||
// return 1; | ||
// } | ||
|
||
auto adapter = adapters[0]; | ||
std::cout << "Advertising on " << adapter->identifier() << " [" << adapter->address() << "]" << std::endl; | ||
|
||
auto advertisement = bluez.make_le_advertisement("/potato"); | ||
adapter->register_le_advertisement(advertisement); | ||
|
||
// Sleep for a bit to allow the adapter to stop discovering. | ||
millisecond_delay(3000); | ||
|
||
async_thread_active = false; | ||
while (!async_thread->joinable()) { | ||
millisecond_delay(10); | ||
} | ||
async_thread->join(); | ||
delete async_thread; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <simplebluez/interfaces/LEAdvertisement1.h> | ||
#include <simpledbus/advanced/Proxy.h> | ||
|
||
namespace SimpleBluez { | ||
|
||
class LEAdvertisement : public SimpleDBus::Proxy { | ||
public: | ||
typedef enum { | ||
DisplayOnly, | ||
DisplayYesNo, | ||
KeyboardOnly, | ||
NoInputNoOutput, | ||
KeyboardDisplay, | ||
} Capabilities; | ||
|
||
LEAdvertisement(std::shared_ptr<SimpleDBus::Connection> conn, const std::string& bus_name, const std::string& path); | ||
virtual ~LEAdvertisement() = default; | ||
|
||
private: | ||
std::shared_ptr<SimpleDBus::Interface> interfaces_create(const std::string& interface_name) override; | ||
|
||
std::shared_ptr<LEAdvertisement1> le_advertisement1(); | ||
}; | ||
|
||
} // namespace SimpleBluez |
35 changes: 35 additions & 0 deletions
35
simplebluez/include/simplebluez/interfaces/LEAdvertisement1.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <simpledbus/advanced/Interface.h> | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace SimpleBluez { | ||
|
||
class LEAdvertisement1 : public SimpleDBus::Interface { | ||
public: | ||
// ----- TYPES ----- | ||
|
||
|
||
// ----- CONSTRUCTORS ----- | ||
LEAdvertisement1(std::shared_ptr<SimpleDBus::Connection> conn, std::string path); | ||
virtual ~LEAdvertisement1() = default; | ||
|
||
// ----- PROPERTIES ----- | ||
void SetType(const std::string& type); | ||
void SetServiceUUIDs(const std::vector<std::string>& uuids); | ||
void SetManufacturerData(const std::map<uint16_t, std::vector<uint8_t>>& data); | ||
void SetServiceData(const std::map<std::string, std::vector<uint8_t>>& data); | ||
void SetIncludeTxPower(bool include); | ||
|
||
// ----- METHODS ----- | ||
void Release(); | ||
|
||
protected: | ||
void property_changed(std::string option_name) override; | ||
void message_handle(SimpleDBus::Message& msg) override; | ||
}; | ||
|
||
} // namespace SimpleBluez |
34 changes: 34 additions & 0 deletions
34
simplebluez/include/simplebluez/interfaces/LEAdvertisingManager1.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <simpledbus/advanced/Interface.h> | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace SimpleBluez { | ||
|
||
class LEAdvertisingManager1 : public SimpleDBus::Interface { | ||
public: | ||
// ----- TYPES ----- | ||
|
||
|
||
// ----- CONSTRUCTORS ----- | ||
LEAdvertisingManager1(std::shared_ptr<SimpleDBus::Connection> conn, std::string path); | ||
virtual ~LEAdvertisingManager1() = default; | ||
|
||
// ----- METHODS ----- | ||
void RegisterAdvertisement(std::string advertisement_path); | ||
void UnregisterAdvertisement(std::string advertisement_path); | ||
|
||
|
||
// ----- PROPERTIES ----- | ||
uint8_t ActiveInstances(bool refresh = true); | ||
uint8_t SupportedInstances(bool refresh = true); | ||
std::vector<std::string> SupportedIncludes(bool refresh = true); | ||
|
||
protected: | ||
void property_changed(std::string option_name) override; | ||
}; | ||
|
||
} // namespace SimpleBluez |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <simplebluez/LEAdvertisement.h> | ||
|
||
using namespace SimpleBluez; | ||
|
||
LEAdvertisement::LEAdvertisement(std::shared_ptr<SimpleDBus::Connection> conn, const std::string& bus_name, | ||
const std::string& path) | ||
: Proxy(conn, bus_name, path) { | ||
_interfaces.emplace(std::make_pair("org.bluez.LEAdvertisement1", interfaces_create("org.bluez.LEAdvertisement1"))); | ||
} | ||
|
||
std::shared_ptr<SimpleDBus::Interface> LEAdvertisement::interfaces_create(const std::string& interface_name) { | ||
if (interface_name == "org.bluez.LEAdvertisement1") { | ||
return std::static_pointer_cast<SimpleDBus::Interface>(std::make_shared<LEAdvertisement1>(_conn, _path)); | ||
} | ||
|
||
auto interface = std::make_shared<SimpleDBus::Interface>(_conn, _bus_name, _path, interface_name); | ||
return std::static_pointer_cast<SimpleDBus::Interface>(interface); | ||
} |
Oops, something went wrong.