-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Crude example where advertising works
- Loading branch information
Showing
17 changed files
with
533 additions
and
5 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,64 @@ | ||
#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; | ||
|
||
adapter->register_le_advertisement("/simplebluez"); | ||
|
||
// Sleep for a bit to allow the adapter to stop discovering. | ||
millisecond_delay(30000); | ||
|
||
adapter->unregister_le_advertisement("/simplebluez"); | ||
|
||
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
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
#pragma once | ||
#include <simpledbus/advanced/LocalInterface.h> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
#include <cstdint> | ||
|
||
namespace SimpleBluez { | ||
|
||
class LEAdvertisement1 : public SimpleDBus::LocalInterface { | ||
public: | ||
// ----- 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 SetSolicitUUIDs(const std::vector<std::string>& uuids); | ||
void SetData(const std::map<uint8_t, std::vector<uint8_t>>& data); | ||
void SetDiscoverable(bool discoverable); | ||
void SetDiscoverableTimeout(uint16_t timeout); | ||
void SetIncludes(const std::vector<std::string>& includes); | ||
void SetLocalName(const std::string& name); | ||
void SetAppearance(uint16_t appearance); | ||
void SetDuration(uint16_t duration); | ||
void SetTimeout(uint16_t timeout); | ||
void SetSecondaryChannel(const std::string& channel); | ||
void SetMinInterval(uint32_t interval); | ||
void SetMaxInterval(uint32_t interval); | ||
void SetTxPower(int16_t power); | ||
void SetIncludeTxPower(bool include); | ||
|
||
// ----- METHODS ----- | ||
|
||
void Release(); | ||
|
||
protected: | ||
void message_handle(SimpleDBus::Message& msg) override; | ||
|
||
private: | ||
// You might want to add private member variables to store the property values | ||
// std::string _type; | ||
// std::vector<std::string> _serviceUUIDs; | ||
// ... (other properties) | ||
}; | ||
|
||
} // namespace SimpleBluez |
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
#pragma once | ||
#include <simpledbus/advanced/RemoteInterface.h> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace SimpleBluez { | ||
|
||
class LEAdvertisingManager1 : public SimpleDBus::RemoteInterface { | ||
|
||
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
Oops, something went wrong.