Implement BLE device discovery

This commit is contained in:
2026-04-11 10:51:29 +08:00
parent 4fb515a63d
commit 27e16df141
3 changed files with 90 additions and 4 deletions

View File

@@ -1,8 +1,75 @@
#include "DRI/GATT/Dri_Gatt.h"
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothDeviceInfo>
#include <QtCore/QEventLoop>
#include <QtCore/QTimer>
QVector<Dri_Gatt_Struct_PortInfo> Dri_Gatt_Enum()
{
return {};
QVector<Dri_Gatt_Struct_PortInfo> PortList;
QBluetoothDeviceDiscoveryAgent Agent;
QEventLoop EventLoop;
QTimer TimeoutTimer;
TimeoutTimer.setSingleShot(true);
QObject::connect(
&Agent,
&QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
[&PortList](const QBluetoothDeviceInfo& DeviceInfo)
{
if (!DeviceInfo.coreConfigurations().testFlag(
QBluetoothDeviceInfo::LowEnergyCoreConfiguration))
{
return;
}
Dri_Gatt_Struct_PortInfo PortInfo;
PortInfo.DeviceName = DeviceInfo.name().trimmed();
PortInfo.DeviceAddress = DeviceInfo.address().toString();
if (PortInfo.DeviceAddress.trimmed().isEmpty())
{
return;
}
for (const Dri_Gatt_Struct_PortInfo& ExistingInfo : PortList)
{
if (ExistingInfo.DeviceAddress == PortInfo.DeviceAddress)
{
return;
}
}
PortList.append(PortInfo);
});
QObject::connect(
&Agent,
&QBluetoothDeviceDiscoveryAgent::finished,
&EventLoop,
&QEventLoop::quit);
QObject::connect(
&Agent,
&QBluetoothDeviceDiscoveryAgent::canceled,
&EventLoop,
&QEventLoop::quit);
QObject::connect(
&TimeoutTimer,
&QTimer::timeout,
[&Agent, &EventLoop]()
{
Agent.stop();
EventLoop.quit();
});
Agent.start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
TimeoutTimer.start(4000);
EventLoop.exec();
return PortList;
}
void Dri_Gatt_Close(Dri_Gatt_Struct_Port* p_Port)

View File

@@ -37,12 +37,12 @@
</ImportGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
<QtInstall>D:\App\Qt\5.13.1\msvc2015_64</QtInstall>
<QtModules>core;serialport</QtModules>
<QtModules>core;serialport;bluetooth</QtModules>
<QtBuildConfig>debug</QtBuildConfig>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
<QtInstall>D:\App\Qt\5.13.1\msvc2015_64</QtInstall>
<QtModules>core;serialport</QtModules>
<QtModules>core;serialport;bluetooth</QtModules>
<QtBuildConfig>release</QtBuildConfig>
</PropertyGroup>
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">

View File

@@ -43,7 +43,26 @@ Implemented behavior:
- stay transport-only
- keep real BLE implementation for a later node
### Node 2: CDC transport cleanup
### Node 2: BLE device discovery
Files updated in this step:
- `KeyBorad/KeyBorad/DRI/GATT/Dri_Gatt.cpp`
- `KeyBorad/KeyBorad/KeyBorad.vcxproj`
Design notes:
- use Qt Bluetooth directly
- keep discovery synchronous for now so the first teaching step stays small
- only collect low-energy devices
Implemented behavior:
- start BLE discovery with `QBluetoothDeviceDiscoveryAgent`
- collect device name and address
- filter to `LowEnergyCoreConfiguration`
- stop after a short timeout
### Node 3: CDC transport cleanup
Files updated in this step: