-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblefinder.cpp
95 lines (81 loc) · 2.82 KB
/
blefinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "app.h"
#include "blefinder.h"
#include <QMetaEnum>
#if defined Q_OS_DARWIN
#include <QBluetoothUuid>
#endif
BleFinder::BleFinder(QObject *parent) :
QObject(parent),
m_deviceDiscoveryAgent(nullptr),
m_scanTimer(nullptr)
{
m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000); // FIXME? 0 => always keep going
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BleFinder::addDevice);
connect(m_deviceDiscoveryAgent, static_cast<void (QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error),
this, &BleFinder::scanError);
m_scanTimer.setSingleShot(true);
m_scanTimer.setInterval(5000);
connect(&m_scanTimer, &QTimer::timeout, this, &BleFinder::handleDiscoveryTimeout);
// m_scanTimer.start();
// TODO: Remove old devices
}
BleFinder::~BleFinder()
{
m_devices.clear();
}
QList<QBluetoothDeviceInfo> BleFinder::getDevices()
{
if (!m_deviceDiscoveryAgent->isActive()) {
g_app->log("BLE: Starting scan");
m_devices.clear();
m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
// if the timer is running, it'll be restarted
m_scanTimer.start();
return m_devices;
}
void BleFinder::forceStop()
{
g_app->log("BLE: Force stop scan");
m_scanTimer.stop();
m_deviceDiscoveryAgent->stop();
}
const QBluetoothDeviceInfo *BleFinder::findDeviceByAddress(QString address)
{
const QBluetoothDeviceInfo *device = 0;
for (int i = 0; i < m_devices.size(); i++) {
if (getDeviceAddress(m_devices.at(i)) == address ) {
device = &m_devices.at(i);
break;
}
}
return device;
}
QString BleFinder::getDeviceAddress(const QBluetoothDeviceInfo &device) {
#if defined Q_OS_DARWIN
// workaround for Core Bluetooth:
return device.deviceUuid().toString();
#else
return device.address().toString();
#endif
}
// ==================================================================================================================
void BleFinder::addDevice(const QBluetoothDeviceInfo &device)
{
// If device is LowEnergy-device, add it to the list
if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
m_devices.append(device);
g_app->log("BLE: device found:" + device.name());
}
}
void BleFinder::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothDeviceDiscoveryAgent::Error>();
g_app->error(QString("SCAN ERROR ") + metaEnum.valueToKey(error));
}
void BleFinder::handleDiscoveryTimeout()
{
g_app->log("BLE: Stopping scan after inactivity");
m_deviceDiscoveryAgent->stop();
}