Implement BLE device discovery
This commit is contained in:
@@ -1,8 +1,75 @@
|
|||||||
#include "DRI/GATT/Dri_Gatt.h"
|
#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()
|
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)
|
void Dri_Gatt_Close(Dri_Gatt_Struct_Port* p_Port)
|
||||||
|
|||||||
@@ -37,12 +37,12 @@
|
|||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
|
||||||
<QtInstall>D:\App\Qt\5.13.1\msvc2015_64</QtInstall>
|
<QtInstall>D:\App\Qt\5.13.1\msvc2015_64</QtInstall>
|
||||||
<QtModules>core;serialport</QtModules>
|
<QtModules>core;serialport;bluetooth</QtModules>
|
||||||
<QtBuildConfig>debug</QtBuildConfig>
|
<QtBuildConfig>debug</QtBuildConfig>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
|
||||||
<QtInstall>D:\App\Qt\5.13.1\msvc2015_64</QtInstall>
|
<QtInstall>D:\App\Qt\5.13.1\msvc2015_64</QtInstall>
|
||||||
<QtModules>core;serialport</QtModules>
|
<QtModules>core;serialport;bluetooth</QtModules>
|
||||||
<QtBuildConfig>release</QtBuildConfig>
|
<QtBuildConfig>release</QtBuildConfig>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
|
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
|
||||||
|
|||||||
@@ -43,7 +43,26 @@ Implemented behavior:
|
|||||||
- stay transport-only
|
- stay transport-only
|
||||||
- keep real BLE implementation for a later node
|
- 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:
|
Files updated in this step:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user