QBluetoothServiceDiscoveryAgent Class

The QBluetoothServiceDiscoveryAgent class enables you to query for Bluetooth services. More...

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

This class was introduced in Qt 5.2.

Public Types

enum DiscoveryMode { MinimalDiscovery, FullDiscovery }
enum Error { NoError, PoweredOffError, InputOutputError, InvalidBluetoothAdapterError, UnknownError }

Detailed Description

The discovery process relies on the Bluetooth Service Discovery Process (SDP). The following steps are required to query the services provided by all contactable Bluetooth devices:

  • create an instance of QBluetoothServiceDiscoveryAgent,
  • connect to either the serviceDiscovered() or finished() signals,
  • and call start().
 void MyClass::startServiceDiscovery()
 {

     // Create a discovery agent and connect to its signals
     QBluetoothServiceDiscoveryAgent *discoveryAgent = new QBluetoothServiceDiscoveryAgent(this);
     connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
             this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));

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

     //...
 }

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

By default a minimal service discovery is performed. In this mode, the returned QBluetoothServiceInfo objects are guaranteed to contain only device and service UUID information. Depending on platform and device capabilities, other service information may also be available. The minimal service discovery mode relies on cached SDP data of the platform. Therefore it is possible that this discovery does not find a device although it is physically available. In such cases a full discovery must be performed to force an update of the platform cache. However for most use cases a minimal discovery is adequate as it is much quicker and other classes which require up-to-date information such as QBluetoothSocket::connectToService() will perform additional discovery if required. If the full service information is required, pass FullDiscovery as the discoveryMode parameter to start().

This class may internally utilize QBluetoothDeviceDiscoveryAgent to find unknown devices.

The service discovery may find Bluetooth Low Energy services too if the target device is a combination of a classic and Low Energy device. Those devices are required to advertise their Low Energy services via SDP. If the target device only supports Bluetooth Low Energy services, it is likely to not advertise them via SDP. The QLowEnergyController class should be utilized to perform the service discovery on Low Energy devices.

On iOS, this class cannot be used because the platform does not expose an API which may permit access to QBluetoothServiceDiscoveryAgent related features.

See also QBluetoothDeviceDiscoveryAgent and QLowEnergyController.

Member Type Documentation

enum QBluetoothServiceDiscoveryAgent::DiscoveryMode

This enum describes the service discovery mode.

ConstantValueDescription
QBluetoothServiceDiscoveryAgent::MinimalDiscovery0Performs a minimal service discovery. The QBluetoothServiceInfo objects returned may be incomplete and are only guaranteed to contain device and service UUID information. Since a minimal discovery relies on cached SDP data it may not find a physically existing device until a FullDiscovery is performed.
QBluetoothServiceDiscoveryAgent::FullDiscovery1Performs a full service discovery.

enum QBluetoothServiceDiscoveryAgent::Error

This enum describes errors that can occur during service discovery.

ConstantValueDescription
QBluetoothServiceDiscoveryAgent::NoErrorQBluetoothDeviceDiscoveryAgent::NoErrorNo error has occurred.
QBluetoothServiceDiscoveryAgent::PoweredOffErrorQBluetoothDeviceDiscoveryAgent::PoweredOffErrorThe Bluetooth adaptor is powered off, power it on before doing discovery.
QBluetoothServiceDiscoveryAgent::InputOutputErrorQBluetoothDeviceDiscoveryAgent::InputOutputErrorWriting or reading from the device resulted in an error.
QBluetoothServiceDiscoveryAgent::InvalidBluetoothAdapterErrorQBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterErrorThe passed local adapter address does not match the physical adapter address of any local Bluetooth device. This value was introduced by Qt 5.3.
QBluetoothServiceDiscoveryAgent::UnknownErrorQBluetoothDeviceDiscoveryAgent::UnknownErrorAn unknown error has occurred.