Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbluetoothservicediscoveryagent.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
8
10
12
129
150 : QObject(parent),
151 d_ptr(new QBluetoothServiceDiscoveryAgentPrivate(this, deviceAdapter))
152{
153 if (!deviceAdapter.isNull()) {
154 const QList<QBluetoothHostInfo> localDevices = QBluetoothLocalDevice::allDevices();
155 for (const QBluetoothHostInfo &hostInfo : localDevices) {
156 if (hostInfo.address() == deviceAdapter)
157 return;
158 }
160 d_ptr->errorString = tr("Invalid Bluetooth adapter address");
161 }
162}
163
171{
172 if (isActive()) {
173 disconnect(); //don't emit any signals due to stop()
174 stop();
175 }
176
177 delete d_ptr;
178}
179
192QList<QBluetoothServiceInfo> QBluetoothServiceDiscoveryAgent::discoveredServices() const
193{
195
196 return d->discoveredServices;
197}
208void QBluetoothServiceDiscoveryAgent::setUuidFilter(const QList<QBluetoothUuid> &uuids)
209{
211
212 d->uuidFilter = uuids;
213}
214
226{
228
229 d->uuidFilter.clear();
230 d->uuidFilter.append(uuid);
231}
232
239{
241
242 return d->uuidFilter;
243}
244
258{
259 if (isActive())
260 return false;
261 if (!address.isNull())
262 d_ptr->singleDevice = true;
263 d_ptr->deviceAddress = address;
264 return true;
265}
266
274{
275 if (d_ptr->singleDevice == true)
276 return d_ptr->deviceAddress;
277 else
278 return QBluetoothAddress();
279}
280
281namespace DarwinBluetooth {
282
284
285}
286
287
296{
298#ifdef QT_OSX_BLUETOOTH
299 // Make sure we are on the right thread/have a run loop:
301#endif
302
304 && d->error != InvalidBluetoothAdapterError) {
305#if QT_CONFIG(bluez)
306 // done to avoid repeated parsing for adapter address
307 // on Bluez5
308 d->foundHostAdapterPath.clear();
309#endif
310 d->setDiscoveryMode(mode);
311 // Clear any possible previous errors
313 d->errorString.clear();
314 if (d->deviceAddress.isNull()) {
315 d->startDeviceDiscovery();
316 } else {
317 d->discoveredDevices << QBluetoothDeviceInfo(d->deviceAddress, QString(), 0);
318 d->startServiceDiscovery();
319 }
320 }
321}
322
328{
330
331 if (d->error == InvalidBluetoothAdapterError || !isActive())
332 return;
333
334 switch (d->discoveryState()) {
336 d->stopDeviceDiscovery();
337 break;
339 d->stopServiceDiscovery();
340 default:
341 ;
342 }
343
344 d->discoveredDevices.clear();
345}
346
354{
356
357 //don't clear the list while the search is ongoing
358 if (isActive())
359 return;
360
361 d->discoveredDevices.clear();
362 d->discoveredServices.clear();
363 d->uuidFilter.clear();
364}
365
376
395
403{
405 return d->errorString;
406}
407
408
420{
422
423 if (!deviceDiscoveryAgent) {
424#if QT_CONFIG(bluez)
425 deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(m_deviceAdapterAddress, q);
426#else
427 deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(q);
428#endif
430 q, [this](){
432 });
434 q, [this](const QBluetoothDeviceInfo &info){
435 this->_q_deviceDiscovered(info);
436 });
439 this->_q_deviceDiscoveryError(newError);
440 });
441 }
442
444
446}
447
452{
453 // disconnect to avoid recursion during stop() - QTBUG-60131
454 // we don't care about a potential signals from device discovery agent anymore
455 deviceDiscoveryAgent->disconnect();
456
457 deviceDiscoveryAgent->stop();
458 delete deviceDiscoveryAgent;
459 deviceDiscoveryAgent = nullptr;
460
462
464 emit q->canceled();
465}
466
471{
472 if (deviceDiscoveryAgent->error() != QBluetoothDeviceDiscoveryAgent::NoError) {
473 //Forward the device discovery error
474 error = static_cast<QBluetoothServiceDiscoveryAgent::Error>(deviceDiscoveryAgent->error());
475 errorString = deviceDiscoveryAgent->errorString();
478 emit q->errorOccurred(error);
479 emit q->finished();
480 return;
481 }
482
483 delete deviceDiscoveryAgent;
484 deviceDiscoveryAgent = nullptr;
485
487}
488
490{
491 // look for duplicates, and cached entries
492 const auto addressEquals = [](const auto &a) {
493 return [a](const auto &info) { return info.address() == a; };
494 };
495 erase_if(discoveredDevices, addressEquals(info.address()));
497}
498
500{
501 error = static_cast<QBluetoothServiceDiscoveryAgent::Error>(newError);
502 errorString = deviceDiscoveryAgent->errorString();
503
504 // disconnect to avoid recursion during stop() - QTBUG-60131
505 // we don't care about a potential signals from device discovery agent anymore
506 deviceDiscoveryAgent->disconnect();
507
508 deviceDiscoveryAgent->stop();
509 delete deviceDiscoveryAgent;
510 deviceDiscoveryAgent = nullptr;
511
514 emit q->errorOccurred(error);
515 emit q->finished();
516}
517
534
544
553
554bool QBluetoothServiceDiscoveryAgentPrivate::isDuplicatedService(
555 const QBluetoothServiceInfo &serviceInfo) const
556{
557 //check the service is not already part of our known list
559 if (info.device() == serviceInfo.device()
560 && info.serviceClassUuids() == serviceInfo.serviceClassUuids()
561 && info.serviceUuid() == serviceInfo.serviceUuid()
562 && info.serverChannel() == serviceInfo.serverChannel()) {
563 return true;
564 }
565 }
566 return false;
567}
568
570
571#include "moc_qbluetoothservicediscoveryagent.cpp"
\inmodule QtBluetooth
void errorOccurred(QBluetoothDeviceDiscoveryAgent::Error error)
This signal is emitted when an error occurs during Bluetooth device discovery.
QString errorString() const
Returns a human-readable description of the last error.
void start()
Starts Bluetooth device discovery, if it is not already started.
void finished()
This signal is emitted when Bluetooth device discovery completes.
Error error() const
Returns the last error.
void deviceDiscovered(const QBluetoothDeviceInfo &info)
This signal is emitted when the Bluetooth device described by info is discovered.
void stop()
Stops Bluetooth device discovery.
Error
Indicates all possible error conditions found during Bluetooth device discovery.
\inmodule QtBluetooth
QBluetoothAddress address() const
Returns the address of the device.
\inmodule QtBluetooth
static QList< QBluetoothHostInfo > allDevices()
Returns a list of all available local Bluetooth devices.
void _q_deviceDiscovered(const QBluetoothDeviceInfo &info)
void _q_deviceDiscoveryFinished()
Called when device discovery finishes.
void startServiceDiscovery()
Starts service discovery for the next device.
void _q_deviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error)
void stop()
Stops the service discovery process.
QBluetoothServiceDiscoveryAgent(QObject *parent=nullptr)
Constructs a new QBluetoothServiceDiscoveryAgent with parent.
DiscoveryMode
This enum describes the service discovery mode.
void start(DiscoveryMode mode=MinimalDiscovery)
Starts service discovery.
QList< QBluetoothServiceInfo > discoveredServices() const
Returns the list of all discovered services.
Error error() const
Returns the type of error that last occurred.
bool setRemoteAddress(const QBluetoothAddress &address)
Sets the remote device address to address.
~QBluetoothServiceDiscoveryAgent()
Destructor for QBluetoothServiceDiscoveryAgent.
QList< QBluetoothUuid > uuidFilter() const
Returns the UUID filter.
QString errorString() const
Returns a human-readable description of the last error that occurred during the service discovery.
bool isActive() const
Returns true if the service discovery is currently active; otherwise returns false.
void clear()
Clears the results of previous service discoveries and resets \l uuidFilter().
QBluetoothAddress remoteAddress() const
Returns the remote device address.
Error
This enum describes errors that can occur during service discovery.
void setUuidFilter(const QList< QBluetoothUuid > &uuids)
Sets the UUID filter to uuids.
\inmodule QtBluetooth
\inmodule QtBluetooth
void removeFirst() noexcept
Definition qlist.h:807
bool isEmpty() const noexcept
Definition qlist.h:401
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void prepend(rvalue_ref t)
Definition qlist.h:473
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
#define this
Definition dialogs.cpp:9
void qt_test_iobluetooth_runloop()
Definition btutility.mm:125
Combined button and popup list for selecting options.
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:788
GLenum mode
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint start
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define tr(X)
#define emit
myObject disconnect()
[26]
QHostInfo info
[0]