Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QBluetoothDeviceDiscoveryAgent Class Reference

\inmodule QtBluetooth More...

#include <qbluetoothdevicediscoveryagent.h>

+ Inheritance diagram for QBluetoothDeviceDiscoveryAgent:
+ Collaboration diagram for QBluetoothDeviceDiscoveryAgent:

Public Types

enum  Error {
  NoError , InputOutputError , PoweredOffError , InvalidBluetoothAdapterError ,
  UnsupportedPlatformError , UnsupportedDiscoveryMethod , LocationServiceTurnedOffError , MissingPermissionsError ,
  UnknownError = 100
}
 Indicates all possible error conditions found during Bluetooth device discovery. More...
 
enum  DiscoveryMethod { NoMethod = 0x0 , ClassicMethod = 0x01 , LowEnergyMethod = 0x02 }
 This enum descibes the type of discovery method employed by the QBluetoothDeviceDiscoveryAgent. More...
 

Public Slots

void start ()
 Starts Bluetooth device discovery, if it is not already started.
 
void start (DiscoveryMethods method)
 Starts Bluetooth device discovery, if it is not already started and the provided methods are supported.
 
void stop ()
 Stops Bluetooth device discovery.
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Signals

void deviceDiscovered (const QBluetoothDeviceInfo &info)
 This signal is emitted when the Bluetooth device described by info is discovered.
 
void deviceUpdated (const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
 This signal is emitted when the agent receives additional information about the Bluetooth device described by info.
 
void finished ()
 This signal is emitted when Bluetooth device discovery completes.
 
void errorOccurred (QBluetoothDeviceDiscoveryAgent::Error error)
 This signal is emitted when an error occurs during Bluetooth device discovery.
 
void canceled ()
 This signal is emitted when device discovery is aborted by a call to stop().
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QBluetoothDeviceDiscoveryAgent (QObject *parent=nullptr)
 Constructs a new Bluetooth device discovery agent with parent parent.
 
 QBluetoothDeviceDiscoveryAgent (const QBluetoothAddress &deviceAdapter, QObject *parent=nullptr)
 Constructs a new Bluetooth device discovery agent with parent.
 
 ~QBluetoothDeviceDiscoveryAgent ()
 Destructor for ~QBluetoothDeviceDiscoveryAgent()
 
bool isActive () const
 Returns true if the agent is currently discovering Bluetooth devices, otherwise returns false.
 
Error error () const
 Returns the last error.
 
QString errorString () const
 Returns a human-readable description of the last error.
 
QList< QBluetoothDeviceInfodiscoveredDevices () const
 Returns a list of all discovered Bluetooth devices.
 
void setLowEnergyDiscoveryTimeout (int msTimeout)
 Sets the maximum search time for Bluetooth Low Energy device search to timeout in milliseconds.
 
int lowEnergyDiscoveryTimeout () const
 Returns a timeout in milliseconds that is applied to the Bluetooth Low Energy device search.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Static Public Member Functions

static DiscoveryMethods supportedDiscoveryMethods ()
 This function returns the discovery methods supported by the current platform.
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 

Additional Inherited Members

- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Detailed Description

\inmodule QtBluetooth

The QBluetoothDeviceDiscoveryAgent class discovers the Bluetooth devices nearby.

Since
5.2

To discover the nearby Bluetooth devices: \list

To retrieve results asynchronously, connect to the deviceDiscovered() signal. To get a list of all discovered devices, call discoveredDevices() after the finished() signal.

This class can be used to discover Classic and Low Energy Bluetooth devices. The individual device type can be determined via the \l QBluetoothDeviceInfo::coreConfigurations() attribute. In most cases the list returned by \l discoveredDevices() contains both types of devices. However not every platform can detect both types of devices. On platforms with this limitation (for example iOS only suports Low Energy discovery), the discovery process will limit the search to the type which is supported.

Note
Since Android 6.0 the ability to detect devices requires ACCESS_COARSE_LOCATION.
The Win32 backend currently does not support the Received Signal Strength Indicator (RSSI), as well as the Manufacturer Specific Data, or other data updates advertised by Bluetooth LE devices after discovery.

Definition at line 18 of file qbluetoothdevicediscoveryagent.h.

Member Enumeration Documentation

◆ DiscoveryMethod

This enum descibes the type of discovery method employed by the QBluetoothDeviceDiscoveryAgent.

\value NoMethod The discovery is not possible. None of the available methods are supported. \value ClassicMethod The discovery process searches for Bluetooth Classic (BaseRate) devices. \value LowEnergyMethod The discovery process searches for Bluetooth Low Energy devices.

See also
supportedDiscoveryMethods()
Since
5.8
Enumerator
NoMethod 
ClassicMethod 
LowEnergyMethod 

Definition at line 38 of file qbluetoothdevicediscoveryagent.h.

◆ Error

Indicates all possible error conditions found during Bluetooth device discovery.

\value NoError No error has occurred. \value PoweredOffError The Bluetooth adaptor is powered off, power it on before doing discovery. \value InputOutputError Writing or reading from the device resulted in an error. \value InvalidBluetoothAdapterError The passed local adapter address does not match the physical adapter address of any local Bluetooth device. \value [since 5.5] UnsupportedPlatformError Device discovery is not possible or implemented on the current platform. The error is set in response to a call to \l start(). An example for such cases are iOS versions below 5.0 which do not support Bluetooth device search at all. \value [since 5.8] UnsupportedDiscoveryMethod One of the requested discovery methods is not supported by the current platform. \value [since 6.2] LocationServiceTurnedOffError The location service is turned off. Usage of Bluetooth APIs is not possible when location service is turned off. \value [since 6.4] MissingPermissionsError The operating system requests permissions which were not granted by the user. \value UnknownError An unknown error has occurred.

Enumerator
NoError 
InputOutputError 
PoweredOffError 
InvalidBluetoothAdapterError 
UnsupportedPlatformError 
UnsupportedDiscoveryMethod 
LocationServiceTurnedOffError 
MissingPermissionsError 
UnknownError 

Definition at line 25 of file qbluetoothdevicediscoveryagent.h.

Constructor & Destructor Documentation

◆ QBluetoothDeviceDiscoveryAgent() [1/2]

QBluetoothDeviceDiscoveryAgent::QBluetoothDeviceDiscoveryAgent ( QObject * parent = nullptr)
explicit

Constructs a new Bluetooth device discovery agent with parent parent.

Definition at line 164 of file qbluetoothdevicediscoveryagent.cpp.

◆ QBluetoothDeviceDiscoveryAgent() [2/2]

QBluetoothDeviceDiscoveryAgent::QBluetoothDeviceDiscoveryAgent ( const QBluetoothAddress & deviceAdapter,
QObject * parent = nullptr )
explicit

Constructs a new Bluetooth device discovery agent with parent.

It uses deviceAdapter for the device search. If deviceAdapter is default constructed the resulting QBluetoothDeviceDiscoveryAgent object will use the local default Bluetooth adapter.

If a deviceAdapter is specified that is not a local adapter \l error() will be set to \l InvalidBluetoothAdapterError. Therefore it is recommended to test the error flag immediately after using this constructor.

See also
error()

Definition at line 182 of file qbluetoothdevicediscoveryagent.cpp.

◆ ~QBluetoothDeviceDiscoveryAgent()

QBluetoothDeviceDiscoveryAgent::~QBluetoothDeviceDiscoveryAgent ( )

Destructor for ~QBluetoothDeviceDiscoveryAgent()

Definition at line 201 of file qbluetoothdevicediscoveryagent.cpp.

Member Function Documentation

◆ canceled

void QBluetoothDeviceDiscoveryAgent::canceled ( )
signal

This signal is emitted when device discovery is aborted by a call to stop().

◆ deviceDiscovered

void QBluetoothDeviceDiscoveryAgent::deviceDiscovered ( const QBluetoothDeviceInfo & info)
signal

This signal is emitted when the Bluetooth device described by info is discovered.

The signal is emitted as soon as the most important device information has been collected. However, as long as the \l finished() signal has not been emitted the information collection continues even for already discovered devices. This is particularly true for signal strength information (RSSI) and manufacturer data updates. If the use case requires continuous manufacturer data or RSSI updates it is advisable to retrieve the device information via \l discoveredDevices() once the discovery has finished or listen to the \l deviceUpdated() signal.

If \l lowEnergyDiscoveryTimeout() is larger than 0 the signal is only ever emitted when at least one attribute of info changes. This reflects the desire to receive updates as more precise information becomes available. The exception to this behavior is the case when \l lowEnergyDiscoveryTimeout is set to 0. A timeout of 0 expresses the desire to monitor the appearance and disappearance of Low Energy devices over time. Under this condition the \l deviceDiscovered() signal is emitted even if info has not changed since the last signal emission.

See also
QBluetoothDeviceInfo::rssi(), lowEnergyDiscoveryTimeout()

◆ deviceUpdated

void QBluetoothDeviceDiscoveryAgent::deviceUpdated ( const QBluetoothDeviceInfo & info,
QBluetoothDeviceInfo::Fields updatedFields )
signal

This signal is emitted when the agent receives additional information about the Bluetooth device described by info.

The updatedFields flags tell which information has been updated.

During discovery, some information can change dynamically, such as \l {QBluetoothDeviceInfo::rssi()}{signal strength} and \l {QBluetoothDeviceInfo::manufacturerData()}{manufacturerData}. This signal informs you that if your application is displaying this data, it can be updated, rather than waiting until the discovery has finished.

See also
QBluetoothDeviceInfo::rssi(), lowEnergyDiscoveryTimeout()

◆ discoveredDevices()

QList< QBluetoothDeviceInfo > QBluetoothDeviceDiscoveryAgent::discoveredDevices ( ) const

Returns a list of all discovered Bluetooth devices.

Definition at line 209 of file qbluetoothdevicediscoveryagent.cpp.

◆ error()

QBluetoothDeviceDiscoveryAgent::Error QBluetoothDeviceDiscoveryAgent::error ( ) const

Returns the last error.

Any possible previous errors are cleared upon restarting the discovery.

Definition at line 355 of file qbluetoothdevicediscoveryagent.cpp.

◆ errorOccurred

void QBluetoothDeviceDiscoveryAgent::errorOccurred ( QBluetoothDeviceDiscoveryAgent::Error error)
signal

This signal is emitted when an error occurs during Bluetooth device discovery.

The error parameter describes the error that occurred.

See also
error(), errorString()
Since
6.2

◆ errorString()

QString QBluetoothDeviceDiscoveryAgent::errorString ( ) const

Returns a human-readable description of the last error.

See also
error(), errorOccurred()

Definition at line 367 of file qbluetoothdevicediscoveryagent.cpp.

◆ finished

void QBluetoothDeviceDiscoveryAgent::finished ( )
signal

This signal is emitted when Bluetooth device discovery completes.

The signal is not going to be emitted if the device discovery finishes with an error.

◆ isActive()

bool QBluetoothDeviceDiscoveryAgent::isActive ( ) const

Returns true if the agent is currently discovering Bluetooth devices, otherwise returns false.

Definition at line 344 of file qbluetoothdevicediscoveryagent.cpp.

◆ lowEnergyDiscoveryTimeout()

int QBluetoothDeviceDiscoveryAgent::lowEnergyDiscoveryTimeout ( ) const

Returns a timeout in milliseconds that is applied to the Bluetooth Low Energy device search.

A value of -1 implies that the platform does not support this property and the timeout for the device search cannot be adjusted. A return value of 0 implies a never-ending search which must be manually stopped via \l stop().

See also
setLowEnergyDiscoveryTimeout()
Since
5.8

Definition at line 263 of file qbluetoothdevicediscoveryagent.cpp.

◆ setLowEnergyDiscoveryTimeout()

void QBluetoothDeviceDiscoveryAgent::setLowEnergyDiscoveryTimeout ( int timeout)

Sets the maximum search time for Bluetooth Low Energy device search to timeout in milliseconds.

If timeout is 0 the discovery runs until \l stop() is called.

This reflects the fact that the discovery process for Bluetooth Low Energy devices is mostly open ended. The platform continues to look for more devices until the search is manually stopped. The timeout ensures that the search is aborted after timeout milliseconds. Of course, it is still possible to manually abort the discovery by calling \l stop().

The new timeout value does not take effect until the device search is restarted. In addition the timeout does not affect the classic Bluetooth device search. Depending on the platform the classic search may add more time to the total discovery process beyond timeout.

For a reliable Bluetooth Low Energy discovery, use at least 40000 milliseconds.

See also
lowEnergyDiscoveryTimeout()
Since
5.8

Definition at line 235 of file qbluetoothdevicediscoveryagent.cpp.

◆ start [1/2]

void QBluetoothDeviceDiscoveryAgent::start ( )
slot

Starts Bluetooth device discovery, if it is not already started.

The deviceDiscovered() signal is emitted as each device is discovered. The finished() signal is emitted once device discovery is complete. The discovery utilizes the maximum set of supported discovery methods on the platform.

See also
supportedDiscoveryMethods()

Definition at line 287 of file qbluetoothdevicediscoveryagent.cpp.

◆ start [2/2]

void QBluetoothDeviceDiscoveryAgent::start ( DiscoveryMethods methods)
slot

Starts Bluetooth device discovery, if it is not already started and the provided methods are supported.

The discovery methods limit the scope of the device search. For example, if the target service or device is a Bluetooth Low Energy device, this function could be used to limit the search to Bluetooth Low Energy devices and thereby reduces the discovery time significantly.

Note
methods only determines the type of discovery and does not imply the filtering of the results. For example, the search may still contain classic bluetooth devices despite methods being set to \l {QBluetoothDeviceDiscoveryAgent::LowEnergyMethod} {LowEnergyMethod} only. This may happen due to previously cached search results which may be incorporated into the search results.
Since
5.8

Definition at line 310 of file qbluetoothdevicediscoveryagent.cpp.

◆ stop

void QBluetoothDeviceDiscoveryAgent::stop ( )
slot

Stops Bluetooth device discovery.

The cancel() signal is emitted once the device discovery is canceled. start() maybe called before the cancel signal is received. Once start() has been called the cancel signal from the prior discovery will be discarded.

Definition at line 337 of file qbluetoothdevicediscoveryagent.cpp.

◆ supportedDiscoveryMethods()

QBluetoothDeviceDiscoveryAgent::DiscoveryMethods QBluetoothDeviceDiscoveryAgent::supportedDiscoveryMethods ( )
static

This function returns the discovery methods supported by the current platform.

It can be used to limit the scope of the device discovery.

Since
5.8

Definition at line 66 of file qbluetoothdevicediscoveryagent_android.cpp.


The documentation for this class was generated from the following files: