QBluetoothDeviceDiscoveryAgent Class

The QBluetoothDeviceDiscoveryAgent class discovers the Bluetooth devices nearby. 更多...

头: #include <QBluetoothDeviceDiscoveryAgent>
qmake: QT += bluetooth
Since: Qt 5.2
继承: QObject

该类在 Qt 5.2 引入。

公共类型

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

特性

公共函数

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

公共槽

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

信号

void canceled ()
void deviceDiscovered (const QBluetoothDeviceInfo & info )
void deviceUpdated (const QBluetoothDeviceInfo & info , QBluetoothDeviceInfo::Fields updatedFields )
void error (QBluetoothDeviceDiscoveryAgent::Error error )
void finished ()

静态公共成员

QBluetoothDeviceDiscoveryAgent::DiscoveryMethods supportedDiscoveryMethods ()

详细描述

To discover the nearby Bluetooth devices:

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 () 信号。

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.

注意: Since Android 6.0 the ability to detect devices requires ACCESS_COARSE_LOCATION.

注意: Due to API limitations it is only possible to find devices that have been paired using Windows' settings on Windows.

注意: 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.

成员类型文档编制

enum QBluetoothDeviceDiscoveryAgent:: DiscoveryMethod
flags QBluetoothDeviceDiscoveryAgent:: DiscoveryMethods

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

常量 描述
QBluetoothDeviceDiscoveryAgent::NoMethod 0x0 The discovery is not possible. None of the available methods are supported.
QBluetoothDeviceDiscoveryAgent::ClassicMethod 0x01 The discovery process searches for Bluetooth Classic (BaseRate) devices.
QBluetoothDeviceDiscoveryAgent::LowEnergyMethod 0x02 The discovery process searches for Bluetooth Low Energy devices.

该枚举在 Qt 5.8 引入或被修改。

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

另请参阅 supportedDiscoveryMethods ().

enum QBluetoothDeviceDiscoveryAgent:: Error

Indicates all possible error conditions found during Bluetooth device discovery.

常量 描述
QBluetoothDeviceDiscoveryAgent::NoError 0 没有出现错误。
QBluetoothDeviceDiscoveryAgent::PoweredOffError 2 The Bluetooth adaptor is powered off, power it on before doing discovery.
QBluetoothDeviceDiscoveryAgent::InputOutputError 1 Writing or reading from the device resulted in an error.
QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError 3 The passed local adapter address does not match the physical adapter address of any local Bluetooth device.
QBluetoothDeviceDiscoveryAgent::UnsupportedPlatformError 4 Device 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::UnsupportedDiscoveryMethod 5 One of the requested discovery methods is not supported by the current platform. This value was introduced by Qt 5.8.
QBluetoothDeviceDiscoveryAgent::UnknownError 100 出现未知错误。

enum QBluetoothDeviceDiscoveryAgent:: InquiryType

This enum describes the inquiry type used while discovering Bluetooth devices.

常量 描述
QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry 0 A general unlimited inquiry. Discovers all visible Bluetooth devices in the local vicinity.
QBluetoothDeviceDiscoveryAgent::LimitedInquiry 1 A limited inquiry discovers devices that are in limited inquiry mode.

LimitedInquiry is not supported on all platforms. If it is requested on a platform that does not support it, GeneralUnlimitedInquiry will be used instead. Setting LimitedInquiry is useful for multi-player Bluetooth-based games that needs faster communication between the devices. The phone scans for devices in LimitedInquiry and Service Discovery is done on one or two devices to speed up the service scan. After the game has connected to the device it intended to, the device returns to GeneralUnlimitedInquiry.

特性文档编制

inquiryType : QBluetoothDeviceDiscoveryAgent::InquiryType

type of inquiry scan to be used while discovering devices

This property affects the type of inquiry scan which is performed while discovering devices.

默认情况下,此特性被设为 GeneralUnlimitedInquiry .

Not all platforms support LimitedInquiry .

访问函数:

QBluetoothDeviceDiscoveryAgent::InquiryType inquiryType () const
void setInquiryType (QBluetoothDeviceDiscoveryAgent::InquiryType type )

另请参阅 InquiryType .

成员函数文档编制

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

Constructs a new Bluetooth device discovery agent with parent .

它使用 deviceAdapter for the device search. If deviceAdapter is default constructed the resulting QBluetoothDeviceDiscoveryAgent object will use the local default Bluetooth adapter.

deviceAdapter is specified that is not a local adapter error () 会被设为 InvalidBluetoothAdapterError . Therefore it is recommended to test the error flag immediately after using this constructor.

另请参阅 error ().

QBluetoothDeviceDiscoveryAgent:: QBluetoothDeviceDiscoveryAgent ( QObject * parent = nullptr)

Constructs a new Bluetooth device discovery agent with parent parent .

[signal] void QBluetoothDeviceDiscoveryAgent:: canceled ()

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

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

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 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 discoveredDevices () once the discovery has finished or listen to the deviceUpdated () 信号。

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 lowEnergyDiscoveryTimeout 被设为 0 . A timeout of 0 expresses the desire to monitor the appearance and disappearance of Low Energy devices over time. Under this condition the deviceDiscovered() signal is emitted even if info has not changed since the last signal emission.

另请参阅 QBluetoothDeviceInfo::rssi () 和 lowEnergyDiscoveryTimeout ().

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

This signal is emitted when the agent receives additional information about the Bluetooth device described by info updatedFields flags tell which information has been updated.

During discovery, some information can change dynamically, such as signal strength and 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.

另请参阅 QBluetoothDeviceInfo::rssi () 和 lowEnergyDiscoveryTimeout ().

[signal] void QBluetoothDeviceDiscoveryAgent:: error ( QBluetoothDeviceDiscoveryAgent::Error error )

此信号发射当 error occurs during Bluetooth device discovery. The error parameter describes the error that occurred.

注意: 信号 error 在此类中被重载。通过使用函数指针句法连接到此信号,Qt 提供用于获得如此范例展示的函数指针的方便帮助程序:

connect(bluetoothDeviceDiscoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error),
    [=](QBluetoothDeviceDiscoveryAgent::Error error){ /* ... */ });
					

另请参阅 error () 和 errorString ().

[signal] void QBluetoothDeviceDiscoveryAgent:: finished ()

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.

[slot] 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.

注意: 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.

该函数在 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.

另请参阅 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.

[虚拟] 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.

bool QBluetoothDeviceDiscoveryAgent:: isActive () const

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

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

该函数在 Qt 5.8 引入。

另请参阅 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 stop () 被调用。

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 .

该函数在 Qt 5.8 引入。

另请参阅 lowEnergyDiscoveryTimeout ().

[static] QBluetoothDeviceDiscoveryAgent::DiscoveryMethods QBluetoothDeviceDiscoveryAgent:: supportedDiscoveryMethods ()

This function returns the discovery methods supported by the current platform. It can be used to limit the scope of the device discovery.

该函数在 Qt 5.8 引入。