diff --git a/KeyBorad/KeyBorad/DRI/GATT/Dri_Gatt.cpp b/KeyBorad/KeyBorad/DRI/GATT/Dri_Gatt.cpp index 64ef797..2b0c1ec 100644 --- a/KeyBorad/KeyBorad/DRI/GATT/Dri_Gatt.cpp +++ b/KeyBorad/KeyBorad/DRI/GATT/Dri_Gatt.cpp @@ -1,8 +1,75 @@ #include "DRI/GATT/Dri_Gatt.h" +#include +#include +#include +#include + QVector Dri_Gatt_Enum() { - return {}; + QVector 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) diff --git a/KeyBorad/KeyBorad/KeyBorad.vcxproj b/KeyBorad/KeyBorad/KeyBorad.vcxproj index 5725fde..d7836fe 100644 --- a/KeyBorad/KeyBorad/KeyBorad.vcxproj +++ b/KeyBorad/KeyBorad/KeyBorad.vcxproj @@ -37,12 +37,12 @@ D:\App\Qt\5.13.1\msvc2015_64 - core;serialport + core;serialport;bluetooth debug D:\App\Qt\5.13.1\msvc2015_64 - core;serialport + core;serialport;bluetooth release diff --git a/docs/host_dri_rebuild.md b/docs/host_dri_rebuild.md index 40e1c6b..b6dfc06 100644 --- a/docs/host_dri_rebuild.md +++ b/docs/host_dri_rebuild.md @@ -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: