Skip to content

Commit 201d480

Browse files
committed
Comms: Add BluetoothLE Link
1 parent cfb841b commit 201d480

File tree

9 files changed

+784
-187
lines changed

9 files changed

+784
-187
lines changed

src/Comms/BluetoothLink.cc

Lines changed: 77 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BluetoothConfiguration::BluetoothConfiguration(const QString &name, QObject *par
2424
: LinkConfiguration(name, parent)
2525
, _deviceDiscoveryAgent(new QBluetoothDeviceDiscoveryAgent(this))
2626
{
27-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
27+
qCDebug(BluetoothLinkLog) << this;
2828

2929
_initDeviceDiscoveryAgent();
3030
}
@@ -34,7 +34,7 @@ BluetoothConfiguration::BluetoothConfiguration(const BluetoothConfiguration *cop
3434
, _device(copy->device())
3535
, _deviceDiscoveryAgent(new QBluetoothDeviceDiscoveryAgent(this))
3636
{
37-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
37+
qCDebug(BluetoothLinkLog) << this;
3838

3939
BluetoothConfiguration::copyFrom(copy);
4040

@@ -45,45 +45,38 @@ BluetoothConfiguration::~BluetoothConfiguration()
4545
{
4646
stopScan();
4747

48-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
48+
qCDebug(BluetoothLinkLog) << this;
4949
}
5050

5151
void BluetoothConfiguration::_initDeviceDiscoveryAgent()
5252
{
5353
(void) connect(_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothConfiguration::_deviceDiscovered);
54+
(void) connect(_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceUpdated, this, &BluetoothConfiguration::_deviceUpdated);
5455
(void) connect(_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &BluetoothConfiguration::scanningChanged);
5556
(void) connect(_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BluetoothConfiguration::scanningChanged);
5657
(void) connect(_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, this, &BluetoothConfiguration::_onSocketErrorOccurred);
57-
58-
if (BluetoothLinkLog().isDebugEnabled()) {
59-
(void) connect(_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceUpdated, this, [this](const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields) {
60-
qCDebug(BluetoothLinkLog) << "Device Updated";
61-
});
62-
}
6358
}
6459

6560
void BluetoothConfiguration::copyFrom(const LinkConfiguration *source)
6661
{
6762
Q_ASSERT(source);
6863
LinkConfiguration::copyFrom(source);
6964

70-
const BluetoothConfiguration *const bluetoothSource = qobject_cast<const BluetoothConfiguration*>(source);
71-
Q_ASSERT(bluetoothSource);
65+
const BluetoothConfiguration *config = qobject_cast<const BluetoothConfiguration*>(source);
66+
Q_ASSERT(config);
7267

73-
_device = bluetoothSource->device();
68+
_device = config->device();
7469
emit deviceChanged();
7570
}
7671

7772
void BluetoothConfiguration::loadSettings(QSettings &settings, const QString &root)
7873
{
7974
settings.beginGroup(root);
8075

81-
_device.name = settings.value("deviceName", _device.name).toString();
82-
#ifdef Q_OS_IOS
83-
_device.uuid = QUuid(settings.value("uuid", _device.uuid.toString()).toString());
84-
#else
85-
_device.address = QBluetoothAddress(settings.value("address", _device.address.toString()).toString());
86-
#endif
76+
const QString name = settings.value("deviceName", _device.name()).toString();
77+
setDevice(name);
78+
// const QBluetoothAddress address = QBluetoothAddress(settings.value("address", _device.address().toString()).toString());
79+
// _device = QBluetoothDeviceInfo(address, name);
8780

8881
settings.endGroup();
8982
}
@@ -92,23 +85,23 @@ void BluetoothConfiguration::saveSettings(QSettings &settings, const QString &ro
9285
{
9386
settings.beginGroup(root);
9487

95-
settings.setValue("deviceName", _device.name);
96-
#ifdef Q_OS_IOS
97-
settings.setValue("uuid", _device.uuid.toString());
98-
#else
99-
settings.setValue("address", _device.address.toString());
100-
#endif
88+
settings.setValue("deviceName", _device.name());
89+
settings.setValue("address", _device.address().toString());
10190

10291
settings.endGroup();
10392
}
10493

10594
QString BluetoothConfiguration::settingsTitle() const
10695
{
107-
if (QGCDeviceInfo::isBluetoothAvailable()) {
108-
return tr("Bluetooth Link Settings");
96+
if (!QGCDeviceInfo::isBluetoothAvailable()) {
97+
return tr("Bluetooth Not Available");
98+
}
99+
100+
if (QBluetoothDeviceDiscoveryAgent::supportedDiscoveryMethods() & (QBluetoothDeviceDiscoveryAgent::ClassicMethod == 0)) {
101+
return tr("Bluetooth Classic Not Supported");
109102
}
110103

111-
return tr("Bluetooth Not Available");
104+
return tr("Bluetooth Link Settings");
112105
}
113106

114107
void BluetoothConfiguration::startScan()
@@ -131,9 +124,9 @@ void BluetoothConfiguration::stopScan() const
131124

132125
void BluetoothConfiguration::setDevice(const QString &name)
133126
{
134-
for (const BluetoothData &data: _deviceList) {
135-
if (data.name == name) {
136-
_device = data;
127+
for (const QBluetoothDeviceInfo &info: _deviceList) {
128+
if (info.name() == name) {
129+
_device = info;
137130
emit deviceChanged();
138131
return;
139132
}
@@ -147,19 +140,44 @@ bool BluetoothConfiguration::scanning() const
147140

148141
void BluetoothConfiguration::_deviceDiscovered(const QBluetoothDeviceInfo &info)
149142
{
150-
if (!info.name().isEmpty() && info.isValid()) {
151-
BluetoothData data;
152-
data.name = info.name();
153-
#ifdef Q_OS_IOS
154-
data.uuid = info.deviceUuid();
155-
#else
156-
data.address = info.address();
157-
#endif
158-
159-
if (!_deviceList.contains(data)) {
160-
_deviceList.append(data);
161-
_nameList.append(data.name);
162-
emit nameListChanged();
143+
if (info.name().isEmpty() || !info.isValid()) {
144+
return;
145+
}
146+
147+
if (!_deviceList.contains(info) && (info.coreConfigurations() & QBluetoothDeviceInfo::BaseRateCoreConfiguration)) {
148+
_deviceList.append(info);
149+
_nameList.append(info.name());
150+
emit nameListChanged();
151+
}
152+
}
153+
154+
void BluetoothConfiguration::_deviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
155+
{
156+
if (!info.isValid() || (updatedFields == QBluetoothDeviceInfo::Field::None)) {
157+
return;
158+
}
159+
160+
for (QBluetoothDeviceInfo &dev: _deviceList) {
161+
if (dev.address() == info.address()) {
162+
if (updatedFields & QBluetoothDeviceInfo::Field::RSSI) {
163+
dev.setRssi(info.rssi());
164+
}
165+
if (updatedFields & QBluetoothDeviceInfo::Field::ManufacturerData) {
166+
const QMultiHash<quint16, QByteArray> data = info.manufacturerData();
167+
for (quint16 id : info.manufacturerIds()) {
168+
dev.setManufacturerData(id, data.value(id));
169+
}
170+
}
171+
if (updatedFields & QBluetoothDeviceInfo::Field::ServiceData) {
172+
const QMultiHash<QBluetoothUuid, QByteArray> data = info.serviceData();
173+
for (QBluetoothUuid uuid : info.serviceIds()) {
174+
dev.setServiceData(uuid, data.value(uuid));
175+
}
176+
}
177+
if (updatedFields & QBluetoothDeviceInfo::Field::All) {
178+
dev = info;
179+
}
180+
break;
163181
}
164182
}
165183
}
@@ -177,14 +195,14 @@ BluetoothWorker::BluetoothWorker(const BluetoothConfiguration *config, QObject *
177195
: QObject(parent)
178196
, _config(config)
179197
{
180-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
198+
qCDebug(BluetoothLinkLog) << this;
181199
}
182200

183201
BluetoothWorker::~BluetoothWorker()
184202
{
185203
disconnectLink();
186204

187-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
205+
qCDebug(BluetoothLinkLog) << this;
188206
}
189207

190208
bool BluetoothWorker::isConnected() const
@@ -197,23 +215,11 @@ void BluetoothWorker::setupSocket()
197215
Q_ASSERT(!_socket);
198216
_socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);
199217

200-
#ifdef Q_OS_IOS
201-
Q_ASSERT(!_serviceDiscoveryAgent);
202-
_serviceDiscoveryAgent = new QBluetoothServiceDiscoveryAgent(this);
203-
#endif
204-
205218
(void) connect(_socket, &QBluetoothSocket::connected, this, &BluetoothWorker::_onSocketConnected);
206219
(void) connect(_socket, &QBluetoothSocket::disconnected, this, &BluetoothWorker::_onSocketDisconnected);
207220
(void) connect(_socket, &QBluetoothSocket::readyRead, this, &BluetoothWorker::_onSocketReadyRead);
208221
(void) connect(_socket, &QBluetoothSocket::errorOccurred, this, &BluetoothWorker::_onSocketErrorOccurred);
209222

210-
#ifdef Q_OS_IOS
211-
(void) connect(_serviceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, this, &BluetoothWorker::_serviceDiscovered);
212-
(void) connect(_serviceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::finished, this, &BluetoothWorker::_discoveryFinished);
213-
(void) connect(_serviceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::canceled, this, &BluetoothWorker::_discoveryFinished);
214-
(void) connect(_serviceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::errorOccurred, this, &BluetoothWorker::_onServiceErrorOccurred);
215-
#endif
216-
217223
if (BluetoothLinkLog().isDebugEnabled()) {
218224
// (void) connect(_socket, &QBluetoothSocket::bytesWritten, this, &BluetoothWorker::_onSocketBytesWritten);
219225

@@ -226,36 +232,25 @@ void BluetoothWorker::setupSocket()
226232
void BluetoothWorker::connectLink()
227233
{
228234
if (isConnected()) {
229-
qCWarning(BluetoothLinkLog) << "Already connected to" << _config->device().name;
235+
qCWarning(BluetoothLinkLog) << "Already connected to" << _config->device().name();
230236
return;
231237
}
232238

233-
qCDebug(BluetoothLinkLog) << "Attempting to connect to" << _config->device().name;
239+
qCDebug(BluetoothLinkLog) << "Attempting to connect to" << _config->device().name();
234240

235-
#ifdef Q_OS_IOS
236-
if (_serviceDiscoveryAgent && _serviceDiscoveryAgent->isActive()) {
237-
_serviceDiscoveryAgent->start();
238-
}
239-
#else
240241
static constexpr QBluetoothUuid uuid = QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort);
241-
_socket->connectToService(_config->device().address, uuid);
242-
#endif
242+
_socket->connectToService(_config->device().address(), uuid);
243243
}
244244

245245
void BluetoothWorker::disconnectLink()
246246
{
247247
if (!isConnected()) {
248-
qCWarning(BluetoothLinkLog) << "Already disconnected from device:" << _config->device().name;
248+
qCWarning(BluetoothLinkLog) << "Already disconnected from device:" << _config->device().name();
249249
return;
250250
}
251251

252-
qCDebug(BluetoothLinkLog) << "Attempting to disconnect from device:" << _config->device().name;
252+
qCDebug(BluetoothLinkLog) << "Attempting to disconnect from device:" << _config->device().name();
253253

254-
#ifdef Q_OS_IOS
255-
if (_serviceDiscoveryAgent && _serviceDiscoveryAgent->isActive()) {
256-
_serviceDiscoveryAgent->stop();
257-
}
258-
#endif
259254
_socket->disconnectFromService();
260255
}
261256

@@ -295,28 +290,28 @@ void BluetoothWorker::writeData(const QByteArray &data)
295290

296291
void BluetoothWorker::_onSocketConnected()
297292
{
298-
qCDebug(BluetoothLinkLog) << "Socket connected to device:" << _config->device().name;
293+
qCDebug(BluetoothLinkLog) << "Socket connected to device:" << _config->device().name();
299294
emit connected();
300295
}
301296

302297
void BluetoothWorker::_onSocketDisconnected()
303298
{
304-
qCDebug(BluetoothLinkLog) << "Socket disconnected from device:" << _config->device().name;
299+
qCDebug(BluetoothLinkLog) << "Socket disconnected from device:" << _config->device().name();
305300
emit disconnected();
306301
}
307302

308303
void BluetoothWorker::_onSocketReadyRead()
309304
{
310305
const QByteArray data = _socket->readAll();
311306
if (!data.isEmpty()) {
312-
// qCDebug(BluetoothLinkLog) << "_onSocketReadyRead:" << data.size();
307+
// qCDebug(BluetoothLinkLog) << _config->device().name() << "Read" << data.size() << "bytes";
313308
emit dataReceived(data);
314309
}
315310
}
316311

317312
void BluetoothWorker::_onSocketBytesWritten(qint64 bytes)
318313
{
319-
qCDebug(BluetoothLinkLog) << _config->device().name << "Wrote" << bytes << "bytes";
314+
qCDebug(BluetoothLinkLog) << _config->device().name() << "Wrote" << bytes << "bytes";
320315
}
321316

322317
void BluetoothWorker::_onSocketErrorOccurred(QBluetoothSocket::SocketError socketError)
@@ -326,38 +321,6 @@ void BluetoothWorker::_onSocketErrorOccurred(QBluetoothSocket::SocketError socke
326321
emit errorOccurred(errorString);
327322
}
328323

329-
#ifdef Q_OS_IOS
330-
void BluetoothWorker::_onServiceErrorOccurred(QBluetoothServiceDiscoveryAgent::Error error)
331-
{
332-
const QString errorString = _serviceDiscoveryAgent->errorString();
333-
qCWarning(BluetoothLinkLog) << "Socket error:" << error << errorString;
334-
emit errorOccurred(errorString);
335-
}
336-
337-
void BluetoothWorker::_serviceDiscovered(const QBluetoothServiceInfo &info)
338-
{
339-
if (isConnected()) {
340-
qCWarning(BluetoothLinkLog) << "Already connected to" << _config->device().name;
341-
return;
342-
}
343-
344-
if (info.device().name().isEmpty() || !info.isValid()) {
345-
return;
346-
}
347-
348-
if ((_config->device().uuid == info.device().deviceUuid()) && (_config->device().name == info.device().name)) {
349-
_socket->connectToService(info);
350-
}
351-
}
352-
353-
void BluetoothWorker::_discoveryFinished()
354-
{
355-
if (!isConnected()) {
356-
emit errorOccurred(QStringLiteral("Discovery Error: Could Not Locate Device!"));
357-
}
358-
}
359-
#endif
360-
361324
/*===========================================================================*/
362325

363326
BluetoothLink::BluetoothLink(SharedLinkConfigurationPtr &config, QObject *parent)
@@ -366,13 +329,13 @@ BluetoothLink::BluetoothLink(SharedLinkConfigurationPtr &config, QObject *parent
366329
, _worker(new BluetoothWorker(_bluetoothConfig))
367330
, _workerThread(new QThread(this))
368331
{
369-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
332+
qCDebug(BluetoothLinkLog) << this;
370333

371334
_checkPermission();
372335

373336
_workerThread->setObjectName(QStringLiteral("Bluetooth_%1").arg(_bluetoothConfig->name()));
374337

375-
_worker->moveToThread(_workerThread);
338+
(void) _worker->moveToThread(_workerThread);
376339

377340
(void) connect(_workerThread, &QThread::started, _worker, &BluetoothWorker::setupSocket);
378341
(void) connect(_workerThread, &QThread::finished, _worker, &QObject::deleteLater);
@@ -397,7 +360,7 @@ BluetoothLink::~BluetoothLink()
397360
qCWarning(BluetoothLinkLog) << "Failed to wait for Bluetooth Thread to close";
398361
}
399362

400-
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;
363+
qCDebug(BluetoothLinkLog) << this;
401364
}
402365

403366
bool BluetoothLink::isConnected() const
@@ -428,7 +391,7 @@ void BluetoothLink::_onDisconnected()
428391
void BluetoothLink::_onErrorOccurred(const QString &errorString)
429392
{
430393
qCWarning(BluetoothLinkLog) << "Communication error:" << errorString;
431-
emit communicationError(tr("Bluetooth Link Error"), tr("Link %1: (Device: %2) %3").arg(_bluetoothConfig->name(), _bluetoothConfig->device().name, errorString));
394+
emit communicationError(tr("Bluetooth Link Error"), tr("Link %1: (Device: %2) %3").arg(_bluetoothConfig->name(), _bluetoothConfig->device().name(), errorString));
432395
}
433396

434397
void BluetoothLink::_onDataReceived(const QByteArray &data)
@@ -441,7 +404,7 @@ void BluetoothLink::_onDataSent(const QByteArray &data)
441404
emit bytesSent(this, data);
442405
}
443406

444-
void BluetoothLink::_writeBytes(const QByteArray& bytes)
407+
void BluetoothLink::_writeBytes(const QByteArray &bytes)
445408
{
446409
(void) QMetaObject::invokeMethod(_worker, "writeData", Qt::QueuedConnection, Q_ARG(QByteArray, bytes));
447410
}

0 commit comments

Comments
 (0)