-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qt5 doen't implement some bluetooth functions in the WinRT backend. I guess it's caused by the C++ minimum standard requirement. I have to port some functions from Qt6 to make the Bluetooth function on MSVC builds. Limitations: 1. BLE Central doesn't work well on MinGW builds. 2. Bluetooth Client discovery doesn't work well on MSVC builds. Users can still specify the Bluetooth address and connect to a server manually. 3. Bluetooth Client reconnect might causes crashes on MSVC builds.
- Loading branch information
Showing
4 changed files
with
140 additions
and
8 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
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,90 @@ | ||
#include "winrtbluetooth.h" | ||
|
||
#include <winrt/Windows.Foundation.Collections.h> | ||
#include <winrt/Windows.Devices.Enumeration.h> | ||
#include <winrt/Windows.Devices.Bluetooth.h> | ||
#include <winrt/Windows.Devices.Radios.h> | ||
|
||
#include <QCoreApplication> | ||
#include <QElapsedTimer> | ||
|
||
using namespace winrt::Windows::Foundation; | ||
using namespace winrt::Windows::Foundation::Collections; | ||
using namespace winrt::Windows::Devices::Enumeration; | ||
using namespace winrt::Windows::Devices::Bluetooth; | ||
using namespace winrt::Windows::Devices::Radios; | ||
|
||
template <typename T> | ||
static bool await(IAsyncOperation<T> &&asyncInfo, T &result, uint timeout = 0) | ||
{ | ||
using WinRtAsyncStatus = winrt::Windows::Foundation::AsyncStatus; | ||
WinRtAsyncStatus status; | ||
QElapsedTimer timer; | ||
if(timeout) | ||
timer.start(); | ||
do | ||
{ | ||
QCoreApplication::processEvents(); | ||
status = asyncInfo.Status(); | ||
} | ||
while(status == WinRtAsyncStatus::Started && (!timeout || !timer.hasExpired(timeout))); | ||
if(status == WinRtAsyncStatus::Completed) | ||
{ | ||
result = asyncInfo.GetResults(); | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
static DeviceInformationCollection getAvailableAdapters() | ||
{ | ||
const auto btSelector = BluetoothAdapter::GetDeviceSelector(); | ||
DeviceInformationCollection deviceInfoCollection(nullptr); | ||
await(DeviceInformation::FindAllAsync(btSelector), deviceInfoCollection); | ||
return deviceInfoCollection; | ||
} | ||
|
||
static Radio getRadioFromAdapterId(winrt::hstring id) | ||
{ | ||
BluetoothAdapter a(nullptr); | ||
bool res = await(BluetoothAdapter::FromIdAsync(id), a); | ||
if(res && a) | ||
{ | ||
Radio r(nullptr); | ||
res = await(a.GetRadioAsync(), r); | ||
if(res && r) | ||
return r; | ||
} | ||
return nullptr; | ||
} | ||
|
||
WinRTBluetooth::WinRTBluetooth(QObject *parent) | ||
: QObject{parent} | ||
{ | ||
|
||
} | ||
|
||
QList<QBluetoothHostInfo> WinRTBluetooth::allLocalDevices(bool PoweredOnOnly) | ||
{ | ||
QList<QBluetoothHostInfo> devices; | ||
const auto deviceInfoCollection = getAvailableAdapters(); | ||
if(deviceInfoCollection) | ||
{ | ||
for(const auto &devInfo : deviceInfoCollection) | ||
{ | ||
BluetoothAdapter adapter(nullptr); | ||
const bool res = await(BluetoothAdapter::FromIdAsync(devInfo.Id()), adapter); | ||
if(res && adapter && (!PoweredOnOnly || getRadioFromAdapterId(devInfo.Id()).State() == RadioState::On)) | ||
{ | ||
QBluetoothHostInfo info; | ||
info.setName(QString::fromStdString(winrt::to_string(devInfo.Name()))); | ||
info.setAddress(QBluetoothAddress(adapter.BluetoothAddress())); | ||
devices.push_back(std::move(info)); | ||
} | ||
} | ||
} | ||
return devices; | ||
} |
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,17 @@ | ||
#ifndef WINRTBLUETOOTH_H | ||
#define WINRTBLUETOOTH_H | ||
|
||
#include <QObject> | ||
#include <QBluetoothHostInfo> | ||
|
||
class WinRTBluetooth : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit WinRTBluetooth(QObject *parent = nullptr); | ||
static QList<QBluetoothHostInfo> allLocalDevices(bool PoweredOnOnly = false); | ||
signals: | ||
|
||
}; | ||
|
||
#endif // WINRTBLUETOOTH_H |