chore: update qt version for ble module

This commit is contained in:
Qsaker 2024-01-25 19:15:39 +08:00
parent 18ea42b8b0
commit 080484f30a
5 changed files with 53 additions and 78 deletions

View File

@ -23,13 +23,16 @@ set(SAK_QT_COMPONENTS
SerialBus)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS ${SAK_QT_COMPONENTS})
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS ${SAK_QT_COMPONENTS})
find_package(Qt${QT_VERSION_MAJOR} QUIET COMPONENTS Bluetooth)
if(Qt${QT_VERSION_MAJOR}Bluetooth_FOUND)
message(STATUS "Qt${QT_VERSION_MAJOR}::Bluetooth found.")
option(SAK_IMPORT_MODULE_BLUETOOTH "Import Bluetooth module." ON)
add_compile_definitions(SAK_IMPORT_MODULE_BLUETOOTH)
else()
message(STATUS "Qt${QT_VERSION_MAJOR}::Bluetooth not found.")
if(NOT Qt6_VERSION VERSION_LESS "6.5.0")
find_package(Qt${QT_VERSION_MAJOR} QUIET COMPONENTS Bluetooth)
if(Qt${QT_VERSION_MAJOR}Bluetooth_FOUND)
message(STATUS "Qt${QT_VERSION_MAJOR}::Bluetooth found.")
option(SAK_IMPORT_MODULE_BLUETOOTH "Import Bluetooth module." ON)
add_compile_definitions(SAK_IMPORT_MODULE_BLUETOOTH)
else()
message(STATUS "Qt${QT_VERSION_MAJOR}::Bluetooth not found.")
endif()
endif()
set(BUILD_TESTING OFF)
@ -224,5 +227,7 @@ endif()
# The private module is not open source.
option(SAK_ENABLE_APP_PRIVATE "Do not change the option unless you are the author" OFF)
if(SAK_ENABLE_APP_PRIVATE)
include(${CMAKE_SOURCE_DIR}/src/private/private.cmake)
if(NOT Qt6_VERSION VERSION_LESS "6.5.0")
include(${CMAKE_SOURCE_DIR}/src/private/private.cmake)
endif()
endif()

View File

@ -1,5 +1,5 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
@ -38,7 +38,6 @@ QVariantList SAKBleCentralTool::characteristics(QVariant service)
if (service.canConvert<QLowEnergyService *>()) {
return list;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
auto cookedService = service.value<QLowEnergyService *>();
if (cookedService) {
auto characteristics = cookedService->characteristics();
@ -46,20 +45,15 @@ QVariantList SAKBleCentralTool::characteristics(QVariant service)
list.append(QVariant::fromValue(characteristic));
}
}
#endif
return list;
}
QString SAKBleCentralTool::characteristicName(QVariant characteristic)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
if (characteristic.canConvert<QLowEnergyCharacteristic>()) {
auto c = characteristic.value<QLowEnergyCharacteristic>();
return c.name();
}
#else
Q_UNUSED(characteristic)
#endif
return "Invalid";
}
@ -149,7 +143,6 @@ bool SAKBleCentralTool::initialize(QString &errStr)
return false;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
mServices.clear();
mBleCentral = QLowEnergyController::createCentral(mBluetoothDeviceInfo);
connect(mBleCentral,
@ -160,11 +153,7 @@ bool SAKBleCentralTool::initialize(QString &errStr)
onServiceDiscoveryFinished();
});
connect(mBleCentral,
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
&QLowEnergyController::errorOccurred,
#else
QOverload<BLE_ERR_SIG>::of(&QLowEnergyController::error),
#endif
mBleCentral,
[=](QLowEnergyController::Error err) { onBleCentralErrorOccuured(err); });
connect(mBleCentral, &QLowEnergyController::connected, mBleCentral, [=]() {
@ -177,9 +166,6 @@ bool SAKBleCentralTool::initialize(QString &errStr)
mBleCentral->connectToDevice();
emit serviceDiscoveryStarted();
return true;
#else
return false;
#endif
}
void SAKBleCentralTool::readBytes()
@ -317,11 +303,7 @@ void SAKBleCentralTool::onServiceObjectStateChanged(QLowEnergyService *service,
QLowEnergyService::ServiceState newState)
{
Q_UNUSED(service);
#if QT_VERSION >= QT_VERSION_CHECK(6, 3, 0)
auto state = QLowEnergyService::RemoteServiceDiscovered;
#else
auto state = QLowEnergyService::ServiceDiscovered;
#endif
if (newState == state) {
qInfo() << "Remote service discovered:" << newState;
}

View File

@ -1,5 +1,5 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*

View File

@ -16,7 +16,7 @@
SAKBleScanner::SAKBleScanner(QObject* parent)
: QThread(parent)
, mDiscover(Q_NULLPTR)
, m_discover(Q_NULLPTR)
{}
SAKBleScanner::~SAKBleScanner() {}
@ -38,12 +38,12 @@ bool SAKBleScanner::isActive()
QVariant SAKBleScanner::deviceInfo(int index)
{
mDeviceInfoListMutex.lock();
if (index >= 0 && index < mDeviceInfoList.length()) {
QBluetoothDeviceInfo info = mDeviceInfoList.at(index);
m_deviceInfoListMutex.lock();
if (index >= 0 && index < m_deviceInfoList.length()) {
QBluetoothDeviceInfo info = m_deviceInfoList.at(index);
return QVariant::fromValue<QBluetoothDeviceInfo>(info);
}
mDeviceInfoListMutex.unlock();
m_deviceInfoListMutex.unlock();
return QVariant();
}
@ -56,38 +56,29 @@ QString SAKBleScanner::deviceName(const QVariant& deviceInfo)
void SAKBleScanner::run()
{
mDiscover = new QBluetoothDeviceDiscoveryAgent();
connect(mDiscover,
m_discover = new QBluetoothDeviceDiscoveryAgent();
connect(m_discover,
&QBluetoothDeviceDiscoveryAgent::finished,
this,
&SAKBleScanner::onDiscoveryFinished);
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
connect(mDiscover,
connect(m_discover,
&QBluetoothDeviceDiscoveryAgent::errorOccurred,
this,
&SAKBleScanner::onDiscoveryErrorOccurred);
#else
connect(mDiscover,
static_cast<BLE_ERR_SIG>(&QBluetoothDeviceDiscoveryAgent::error),
this,
&SAKBleScanner::onDiscoveryErrorOccurred);
#endif
connect(mDiscover,
connect(m_discover,
&QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
this,
&SAKBleScanner::onDiscoveryDeviceDiscovered);
// 10s-1minute
int interval = mTimeoutInterval < 10 ? 10 : mTimeoutInterval;
int interval = m_timeoutInterval < 10 ? 10 : m_timeoutInterval;
interval = interval > 120 ? 120 : interval;
mDiscover->setLowEnergyDiscoveryTimeout(interval * 1000);
m_discover->setLowEnergyDiscoveryTimeout(interval * 1000);
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
mDeviceInfoListMutex.lock();
mDeviceInfoList.clear();
mDeviceInfoListMutex.unlock();
mDiscover->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
#endif
m_deviceInfoListMutex.lock();
m_deviceInfoList.clear();
m_deviceInfoListMutex.unlock();
m_discover->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
exec();
}
@ -100,10 +91,9 @@ void SAKBleScanner::onDiscoveryFinished()
void SAKBleScanner::onDiscoveryErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error)
{
Q_UNUSED(error);
qWarning() << "QBluetoothDeviceDiscoveryAgent error:"
<< mDiscover->errorString();
qWarning() << "QBluetoothDeviceDiscoveryAgent error:" << m_discover->errorString();
exit();
emit errorOccurred(mDiscover->errorString());
emit errorOccurred(m_discover->errorString());
}
void SAKBleScanner::onDiscoveryDeviceDiscovered(const QBluetoothDeviceInfo& info)
@ -111,49 +101,49 @@ void SAKBleScanner::onDiscoveryDeviceDiscovered(const QBluetoothDeviceInfo& info
const QString name = info.name();
qInfo() << "new ble device:" << name;
if (!mNameFiltter.isEmpty()) {
if (!name.contains(mNameFiltter)) {
if (!m_nameFiltter.isEmpty()) {
if (!name.contains(m_nameFiltter)) {
qInfo() << "device is ignored:" << name;
return;
}
}
mDeviceInfoListMutex.lock();
mDeviceInfoList.append(info);
mDeviceInfoListMutex.unlock();
m_deviceInfoListMutex.lock();
m_deviceInfoList.append(info);
m_deviceInfoListMutex.unlock();
emit deviceDiscovered(info);
}
QVariantList SAKBleScanner::devicesInfoList()
{
QVariantList list;
mDeviceInfoListMutex.lock();
for (auto& info : mDeviceInfoList) {
m_deviceInfoListMutex.lock();
for (auto& info : m_deviceInfoList) {
list.append(QVariant::fromValue(info));
}
mDeviceInfoListMutex.unlock();
m_deviceInfoListMutex.unlock();
return list;
}
int SAKBleScanner::timeoutInterval()
{
return mTimeoutInterval;
return m_timeoutInterval;
}
void SAKBleScanner::setTimeoutInterval(int interval)
{
mTimeoutInterval = interval;
m_timeoutInterval = interval;
emit timeoutIntervalChanged();
}
QString SAKBleScanner::namefiltter()
{
return mNameFiltter;
return m_nameFiltter;
}
void SAKBleScanner::setNameFiltter(const QString& flag)
{
mNameFiltter = flag;
m_nameFiltter = flag;
emit filtterNameChanged();
}

View File

@ -1,5 +1,5 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
@ -19,10 +19,11 @@
class SAKBleScanner : public QThread
{
Q_OBJECT
// clang-format off
Q_PROPERTY(QVariantList deviceInfoList READ devicesInfoList NOTIFY devicesInfoListChanged)
Q_PROPERTY(int timeoutInterval READ timeoutInterval WRITE setTimeoutInterval NOTIFY
timeoutIntervalChanged)
Q_PROPERTY(int timeoutInterval READ timeoutInterval WRITE setTimeoutInterval NOTIFY timeoutIntervalChanged)
Q_PROPERTY(QString namefiltter READ namefiltter WRITE setNameFiltter NOTIFY filtterNameChanged)
// clang-format on
public:
explicit SAKBleScanner(QObject *parent = nullptr);
~SAKBleScanner();
@ -43,17 +44,14 @@ protected:
virtual void run() override;
private:
QBluetoothDeviceDiscoveryAgent *mDiscover;
QLoggingCategory mLoggingCategory{"sak.blescanner"};
QMutex mDeviceInfoListMutex;
QBluetoothDeviceDiscoveryAgent *m_discover;
QMutex m_deviceInfoListMutex;
private:
void onDiscoveryFinished();
void onDiscoveryErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error);
void onDiscoveryDeviceDiscovered(const QBluetoothDeviceInfo &info);
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//Properties
public:
QVariantList devicesInfoList();
int timeoutInterval();
@ -68,9 +66,9 @@ signals:
void filtterNameChanged();
private:
QVector<QBluetoothDeviceInfo> mDeviceInfoList;
int mTimeoutInterval{120};
QString mNameFiltter{""};
QVector<QBluetoothDeviceInfo> m_deviceInfoList;
int m_timeoutInterval{120};
QString m_nameFiltter{""};
};
#endif // SAKBLESCANNER_H