QBluetoothDeviceDiscoveryAgent Class

The QBluetoothDeviceDiscoveryAgent class discovers the Bluetooth devices nearby. More...

Header: #include <QBluetoothDeviceDiscoveryAgent>
qmake: QT += bluetooth
Since: Qt 5.2
Inherits: QObject

Public Types

enum DiscoveryMethod { NoMethod, ClassicMethod, LowEnergyMethod }
flags DiscoveryMethods
enum Error { NoError, PoweredOffError, InputOutputError, InvalidBluetoothAdapterError, UnsupportedPlatformError, …, UnknownError }

Public Functions

QBluetoothDeviceDiscoveryAgent(const QBluetoothAddress &deviceAdapter, QObject *parent = nullptr)
QBluetoothDeviceDiscoveryAgent(QObject *parent = nullptr)
virtual ~QBluetoothDeviceDiscoveryAgent()
QList<QBluetoothDeviceInfo> discoveredDevices() const
QBluetoothDeviceDiscoveryAgent::Error error() const
QString errorString() const
int lowEnergyDiscoveryTimeout() const
void setLowEnergyDiscoveryTimeout(int timeout)

Public Slots

void start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)
void start()
void stop()

Detailed Description

To discover the nearby Bluetooth devices:

  • create an instance of QBluetoothDeviceDiscoveryAgent,
  • connect to either the deviceDiscovered() or finished() signals,
  • and call start().
 void MyClass::startDeviceDiscovery()
 {

     // Create a discovery agent and connect to its signals
     QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
     connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
             this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));

     // Start a discovery
     discoveryAgent->start();

     //...
 }

 // In your local slot, read information about the found devices
 void MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
 {
     qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
 }

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 QBluetoothDeviceInfo::coreConfigurations() attribute. In most cases the list returned by 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.

Note: 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.

Member Type Documentation

[since 5.8] enum QBluetoothDeviceDiscoveryAgent::DiscoveryMethod
flags QBluetoothDeviceDiscoveryAgent::DiscoveryMethods

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

ConstantValueDescription
QBluetoothDeviceDiscoveryAgent::NoMethod0x0The discovery is not possible. None of the available methods are supported.
QBluetoothDeviceDiscoveryAgent::ClassicMethod0x01The discovery process searches for Bluetooth Classic (BaseRate) devices.
QBluetoothDeviceDiscoveryAgent::LowEnergyMethod0x02The discovery process searches for Bluetooth Low Energy devices.

This enum was introduced or modified in Qt 5.8.

The DiscoveryMethods type is a typedef for QFlags<DiscoveryMethod>. It stores an OR combination of DiscoveryMethod values.

See also supportedDiscoveryMethods().

enum QBluetoothDeviceDiscoveryAgent::Error

Indicates all possible error conditions found during Bluetooth device discovery.

ConstantValueDescription
QBluetoothDeviceDiscoveryAgent::NoError0No error has occurred.
QBluetoothDeviceDiscoveryAgent::PoweredOffError2The Bluetooth adaptor is powered off, power it on before doing discovery.
QBluetoothDeviceDiscoveryAgent::InputOutputError1Writing or reading from the device resulted in an error.
QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError3The passed local adapter address does not match the physical adapter address of any local Bluetooth device.
QBluetoothDeviceDiscoveryAgent::UnsupportedPlatformError4Device discovery is not possible or implemented on the current platform. The error is set in response to a call to start(). An example for such cases are iOS versions below 5.0 which do not support Bluetooth device search at all. This value was introduced by Qt 5.5.
QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod5One of the requested discovery methods is not supported by the current platform. This value was introduced by Qt 5.8.
QBluetoothDeviceDiscoveryAgent::LocationServiceTurnedOffError6The location service is turned off. Usage of Bluetooth APIs is not possible when location service is turned off. This value was introduced by Qt 6.2.
QBluetoothDeviceDiscoveryAgent::UnknownError100An unknown error has occurred.

Member Function Documentation

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

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 error() will be set to InvalidBluetoothAdapterError. Therefore it is recommended to test the error flag immediately after using this constructor.

See also error().

QBluetoothDeviceDiscoveryAgent::QBluetoothDeviceDiscoveryAgent(QObject *parent = nullptr)

Constructs a new Bluetooth device discovery agent with parent parent.

[slot, since 5.8] void QBluetoothDeviceDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)

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 LowEnergyMethod only. This may happen due to previously cached search results which may be incorporated into the search results.

This function was introduced in Qt 5.8.

[slot] void QBluetoothDeviceDiscoveryAgent::start()

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().

[slot] void QBluetoothDeviceDiscoveryAgent::stop()

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.

[virtual] QBluetoothDeviceDiscoveryAgent::~QBluetoothDeviceDiscoveryAgent()

Destructor for ~QBluetoothDeviceDiscoveryAgent()

QList<QBluetoothDeviceInfo> QBluetoothDeviceDiscoveryAgent::discoveredDevices() const

Returns a list of all discovered Bluetooth devices.

QBluetoothDeviceDiscoveryAgent::Error QBluetoothDeviceDiscoveryAgent::error() const

Returns the last error.

QString QBluetoothDeviceDiscoveryAgent::errorString() const

Returns a human-readable description of the last error.

[since 5.8] 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 stop().

This function was introduced in Qt 5.8.

See also setLowEnergyDiscoveryTimeout().

[since 5.8] 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 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 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.

This function was introduced in Qt 5.8.

See also lowEnergyDiscoveryTimeout().