This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devicefinder.cpp
190 lines (154 loc) · 4.7 KB
/
devicefinder.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "devicefinder.h"
// system includes
#include <algorithm>
// local includes
#include "devicehandler.h"
DeviceFinder::DeviceFinder(QObject *parent):
QAbstractItemModel{parent},
m_deviceDiscoveryAgent{this}
{
m_deviceDiscoveryAgent.setLowEnergyDiscoveryTimeout(5000);
connect(&m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &DeviceFinder::addDevice);
connect(&m_deviceDiscoveryAgent, qOverload<QBluetoothDeviceDiscoveryAgent::Error>(&QBluetoothDeviceDiscoveryAgent::error),
this, &DeviceFinder::scanError);
connect(&m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &DeviceFinder::scanFinished);
connect(&m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &DeviceFinder::scanFinished);
}
QModelIndex DeviceFinder::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent)
return createIndex(row, column);
}
QModelIndex DeviceFinder::parent(const QModelIndex &child) const
{
Q_UNUSED(child)
return {};
}
int DeviceFinder::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_devices.size();
}
int DeviceFinder::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 1;
}
QVariant DeviceFinder::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
qWarning() << "invalid index";
return {};
}
if (index.row() < 0 ||
index.row() >= m_devices.size() ||
index.column() != 0)
{
qWarning() << "index out of bounds" << index;
return {};
}
const auto &device = m_devices.at(index.row());
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
case Qt::UserRole + 1:
return device.name();
case Qt::UserRole + 2:
return device.address().toString();
}
return {};
}
QMap<int, QVariant> DeviceFinder::itemData(const QModelIndex &index) const
{
if (!index.isValid())
{
qWarning() << "invalid index";
return {};
}
if (index.row() < 0 ||
index.row() >= m_devices.size() ||
index.column() != 0)
{
qWarning() << "index out of bounds" << index;
return {};
}
const auto &device = m_devices.at(index.row());
return QMap<int, QVariant> {
{ Qt::UserRole + 1, device.name() },
{ Qt::UserRole + 2, device.address().toString() }
};
}
QHash<int, QByteArray> DeviceFinder::roleNames() const
{
return QHash<int, QByteArray> {
{ Qt::UserRole + 1, QByteArrayLiteral("deviceName") },
{ Qt::UserRole + 2, QByteArrayLiteral("deviceAddress") }
};
}
void DeviceFinder::startSearch()
{
clearMessages();
beginResetModel();
m_devices.clear();
endResetModel();
m_deviceDiscoveryAgent.start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
emit scanningChanged();
setInfo(tr("Scanning for devices..."));
}
void DeviceFinder::addDevice(const QBluetoothDeviceInfo &device)
{
// If device is LowEnergy-device, add it to the list
if (!(device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration))
return;
//if (!device.name().contains("bobby"))
// return;
beginInsertRows({}, m_devices.size(), m_devices.size());
m_devices.push_back(device);
endInsertRows();
setInfo(tr("Low Energy device found. Scanning more..."));
}
void DeviceFinder::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
switch (error)
{
case QBluetoothDeviceDiscoveryAgent::PoweredOffError:
setError(tr("The Bluetooth adaptor is powered off."));
break;
case QBluetoothDeviceDiscoveryAgent::InputOutputError:
setError(tr("Writing or reading from the device resulted in an error."));
break;
default:
setError(tr("An unknown error has occurred."));
}
}
void DeviceFinder::scanFinished()
{
if (m_devices.empty())
setError(tr("No Low Energy devices found."));
else
setInfo(tr("Scanning done."));
emit scanningChanged();
}
void DeviceFinder::connectToService(const QString &address)
{
m_deviceDiscoveryAgent.stop();
auto iter = std::find_if(std::cbegin(m_devices), std::cend(m_devices), [&address](const QBluetoothDeviceInfo &device){
return device.address().toString() == address;
});
if (iter == std::cend(m_devices))
{
qWarning() << "could not find address" << address;
setError(tr("could not find address %0").arg(address));
return;
}
if (!m_handler)
{
qWarning() << "no valid handler!";
setError(tr("no valid handler!"));
return;
}
m_handler->setDevice(*iter);
clearMessages();
}