Added Windows support to retrieve connected peripherals.

This commit is contained in:
Kevin Dewald 2025-04-28 23:05:15 -07:00
parent 8aa7a6c07d
commit f6f6094c03
5 changed files with 50 additions and 5 deletions

View File

@ -14,13 +14,16 @@ The format is based on `Keep a Changelog`_, and this project adheres to `Semanti
- iOS, MacOS and Android do not support powering on and off the adapter. Calling these methods will not have any effect on the adapter.
- Linux does have support for powering on and off the adapter, but further architecture changes are needed to properly expose this.
- Callbacks for power on and off events are currently only supported on Windows.
- Retrieving connected peripherals is currently only supported on Windows. (More backends coming soon.)
**Added**
- (Windows) Added support for powering adapters on and off.
- (Python) Exposed the `Adapter::power_on()`, `Adapter::power_off()` and `Adapter::is_powered()` methods.
- Functions for powering adapters on, off and querying their power state.
- Callbacks to monitor adapter power on and off events.
- (Windows) Added support for powering adapters on and off.
- (Windows) Added support for retrieving paired peripherals.
- (Windows) Added support for retrieving connected peripherals.
- (Python) Exposed the `Adapter::power_on()`, `Adapter::power_off()` and `Adapter::is_powered()` methods.
**Changed**

View File

@ -68,8 +68,20 @@ class SIMPLEBLE_EXPORT Adapter {
void set_callback_on_scan_updated(std::function<void(Peripheral)> on_scan_updated);
void set_callback_on_scan_found(std::function<void(Peripheral)> on_scan_found);
/**
* Retrieve a list of all paired peripherals.
*
* NOTE:This method is currently only supported by the Linux, Windows and Android backends.
*/
std::vector<Peripheral> get_paired_peripherals();
/**
* Retrieve a list of all connected peripherals.
*
* NOTE: This method is currently only supported by the Windows backend. (More backends coming soon.)
*/
std::vector<Peripheral> get_connected_peripherals();
static bool bluetooth_enabled();
/**

View File

@ -56,6 +56,7 @@ class AdapterBase {
virtual void set_callback_on_scan_found(std::function<void(Peripheral)> on_scan_found);
virtual std::vector<std::shared_ptr<PeripheralBase>> get_paired_peripherals() = 0;
virtual std::vector<std::shared_ptr<PeripheralBase>> get_connected_peripherals() { return {}; };
/**
* Checks if Bluetooth is enabled.

View File

@ -149,9 +149,37 @@ SharedPtrVector<PeripheralBase> AdapterWindows::get_paired_peripherals() {
} catch (const winrt::hresult_error& e) {
SIMPLEBLE_LOG_ERROR(fmt::format("WinRT error processing paired device {} : {}", winrt::to_string(dev_info.Id()), winrt::to_string(e.message())));
throw Exception::WinRTException(e.code().value, winrt::to_string(e.message()));
} catch (...) {
SIMPLEBLE_LOG_ERROR(fmt::format("Unknown error processing paired device {}", winrt::to_string(dev_info.Id())));
throw Exception::BaseException(fmt::format("Unknown error processing paired device {}", winrt::to_string(dev_info.Id())));
}
}
return peripherals;
});
}
SharedPtrVector<PeripheralBase> AdapterWindows::get_connected_peripherals() {
return MtaManager::get().execute_sync<SharedPtrVector<PeripheralBase>>([this]() {
SharedPtrVector<PeripheralBase> peripherals;
winrt::hstring aqs_filter = BluetoothLEDevice::GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus::Connected);
auto dev_info_collection = async_get(Devices::Enumeration::DeviceInformation::FindAllAsync(aqs_filter));
for (const auto& dev_info : dev_info_collection) {
try {
BluetoothLEDevice device = async_get(BluetoothLEDevice::FromIdAsync(dev_info.Id()));
if (device == nullptr) {
SIMPLEBLE_LOG_WARN(fmt::format("Could not get BluetoothLEDevice for connected device ID: {}", winrt::to_string(dev_info.Id())));
continue;
}
BluetoothAddress address = _mac_address_to_str(device.BluetoothAddress());
if (this->peripherals_.count(address) == 0) {
// If the peripheral has never been seen before, create and save a reference to it.
auto base_peripheral = std::make_shared<PeripheralWindows>(device);
this->peripherals_.insert(std::make_pair(address, base_peripheral));
}
peripherals.push_back(this->peripherals_.at(address));
} catch (const winrt::hresult_error& e) {
SIMPLEBLE_LOG_ERROR(fmt::format("WinRT error processing connected device {} : {}", winrt::to_string(dev_info.Id()), winrt::to_string(e.message())));
throw Exception::WinRTException(e.code().value, winrt::to_string(e.message()));
}
}
return peripherals;

View File

@ -50,6 +50,7 @@ class AdapterWindows : public AdapterBase {
virtual std::vector<std::shared_ptr<PeripheralBase>> scan_get_results() override;
virtual std::vector<std::shared_ptr<PeripheralBase>> get_paired_peripherals() override;
virtual std::vector<std::shared_ptr<PeripheralBase>> get_connected_peripherals() override;
virtual bool bluetooth_enabled() override;