QBluetoothServiceDiscoveryAgent Class

QBluetoothServiceDiscoveryAgent 类可让您查询蓝牙服务。更多

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

公共类型

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

公共函数

QBluetoothServiceDiscoveryAgent(QObject *parent = nullptr)
QBluetoothServiceDiscoveryAgent(const QBluetoothAddress &deviceAdapter, QObject *parent = nullptr)
virtual ~QBluetoothServiceDiscoveryAgent()
QList<QBluetoothServiceInfo> discoveredServices() const
QBluetoothServiceDiscoveryAgent::Error error() const
QString errorString() const
bool isActive() const
QBluetoothAddress remoteAddress() const
bool setRemoteAddress(const QBluetoothAddress &address)
void setUuidFilter(const QBluetoothUuid &uuid)
void setUuidFilter(const QList<QBluetoothUuid> &uuids)
QList<QBluetoothUuid> uuidFilter() const

公共插槽

void clear()
void start(QBluetoothServiceDiscoveryAgent::DiscoveryMode mode = MinimalDiscovery)
void stop()

信号

void canceled()
(since 6.2) void errorOccurred(QBluetoothServiceDiscoveryAgent::Error error)
void finished()
void serviceDiscovered(const QBluetoothServiceInfo &info)

详细说明

发现过程依赖于蓝牙服务发现过程(SDP)。查询所有可联系蓝牙设备提供的服务需要以下步骤:

voidMyClass::startServiceDiscovery() {// 创建一个发现代理并连接其信号   QBluetoothServiceDiscoveryAgent*discoveryAgent = newQBluetoothServiceDiscoveryAgent(this); connect(discoveryAgent,SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this,SLOT(serviceDiscovered(QBluetoothServiceInfo)));// 启动发现 discoveryAgent->start();//...}// 在本地槽中,读取发现设备的信息voidMyClass::serviceDiscovered(constQBluetoothServiceInfo服务){
    qDebug() << "Found new service:" << service.serviceName()
            << '('<<service.device().address().toString()<< ')'; }

默认情况下,将执行最低限度的服务发现。在这种模式下,返回的QBluetoothServiceInfo 对象保证只包含设备和服务 UUID 信息。根据平台和设备功能的不同,可能还会提供其他服务信息。最小服务发现模式依赖于平台的缓存 SDP 数据。因此,尽管设备物理上是可用的,但这种发现有可能找不到设备。在这种情况下,必须执行全面发现,以强制更新平台缓存。不过,对于大多数用例来说,最低限度的发现就足够了,因为它更快,而且其他需要最新信息的类,如QBluetoothSocket::connectToService() 会在需要时执行额外的发现。如果需要完整的服务信息,请将FullDiscovery 作为 discoveryMode 参数传递给start()。

该类可在内部利用QBluetoothDeviceDiscoveryAgent 查找未知设备。

如果目标设备是传统设备和低能耗设备的组合,服务发现也可以找到蓝牙低能耗服务。这些设备必须通过 SDP 宣传其低能耗服务。如果目标设备只支持蓝牙低能耗服务,则很可能不会通过 SDP 宣传这些服务。应使用QLowEnergyController 类对低能耗设备进行服务发现。

在 iOS 平台上,由于该平台没有公开允许访问 QBluetoothServiceDiscoveryAgent 相关功能的 API,因此不能使用该类。

另请参阅 QBluetoothDeviceDiscoveryAgentQLowEnergyController

成员类型文档

enum QBluetoothServiceDiscoveryAgent::DiscoveryMode

该枚举描述服务发现模式。

常量描述
QBluetoothServiceDiscoveryAgent::MinimalDiscovery0执行最小服务发现。返回的QBluetoothServiceInfo 对象可能不完整,只能保证包含设备和服务 UUID 信息。由于最小发现依赖于缓存的 SDP 数据,因此在执行FullDiscovery 之前可能找不到实际存在的设备。
QBluetoothServiceDiscoveryAgent::FullDiscovery1执行完全服务发现。

enum QBluetoothServiceDiscoveryAgent::Error

该枚举描述了服务发现过程中可能发生的错误。

常量说明
QBluetoothServiceDiscoveryAgent::NoErrorQBluetoothDeviceDiscoveryAgent::NoError未发生错误。
QBluetoothServiceDiscoveryAgent::PoweredOffErrorQBluetoothDeviceDiscoveryAgent::PoweredOffError蓝牙适配器电源已关闭,请先打开电源再进行发现。
QBluetoothServiceDiscoveryAgent::InputOutputErrorQBluetoothDeviceDiscoveryAgent::InputOutputError写入或读取设备导致错误。
QBluetoothServiceDiscoveryAgent::InvalidBluetoothAdapterError (since Qt 5.3)QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError传入的本地适配器地址与任何本地蓝牙设备的物理适配器地址不匹配。
QBluetoothServiceDiscoveryAgent::MissingPermissionsError (since Qt 6.4)QBluetoothDeviceDiscoveryAgent::MissingPermissionsError操作系统请求用户未授予的权限。
QBluetoothServiceDiscoveryAgent::UnknownErrorQBluetoothDeviceDiscoveryAgent::UnknownError发生未知错误。

成员函数文档

[explicit] QBluetoothServiceDiscoveryAgent::QBluetoothServiceDiscoveryAgent(QObject *parent = nullptr)

通过parent 构建一个新的 QBluetoothServiceDiscoveryAgent。搜索通过本地默认蓝牙适配器进行。

[explicit] QBluetoothServiceDiscoveryAgent::QBluetoothServiceDiscoveryAgent(const QBluetoothAddress &deviceAdapter, QObject *parent = nullptr)

deviceAdapterparent 构建一个新的 QBluetoothServiceDiscoveryAgent。

它使用deviceAdapter 进行服务搜索。如果deviceAdapter 是默认构造,生成的 QBluetoothServiceDiscoveryAgent 对象将使用本地默认蓝牙适配器。

如果指定的deviceAdapter 不是本地适配器,error() 将被设置为InvalidBluetoothAdapterError 。因此,建议在使用此构造函数后立即测试错误标志。

注: 在 WinRT 上,传递的适配器地址将被忽略。

注: 在 Android 上,传递任何deviceAdapter 地址都没有意义,因为 Android 6.0 或更高版本不再发布本地蓝牙地址。因此,传递的适配器地址永远无法与本地适配器地址相匹配。因此,对start() 的后续调用将始终触发InvalidBluetoothAdapterError

另请参阅 error() 。

[virtual noexcept] QBluetoothServiceDiscoveryAgent::~QBluetoothServiceDiscoveryAgent()

的析构函数QBluetoothServiceDiscoveryAgent

[signal] void QBluetoothServiceDiscoveryAgent::canceled()

当通过调用stop() 取消服务发现时,就会触发该信号。

[slot] void QBluetoothServiceDiscoveryAgent::clear()

清除以前的服务发现结果并重置uuidFilter()。该函数在正在进行的服务发现过程中不起作用(请参阅isActive() )。

另请参阅 discoveredServices()。

QList<QBluetoothServiceInfo> QBluetoothServiceDiscoveryAgent::discoveredServices() const

返回所有已发现服务的列表。

该服务列表累积了多次调用start() 新发现的服务。除非调用clear() ,否则该列表的大小不会减少。这就意味着,如果远程蓝牙设备在两次调用start() 的间隙离开了服务范围,列表中可能会包含陈旧的条目。

注意: 在更改发现模式之前,应始终清除服务列表。

另请参阅 clear()。

QBluetoothServiceDiscoveryAgent::Error QBluetoothServiceDiscoveryAgent::error() const

返回最后发生的错误类型。如果服务发现是针对单个remoteAddress() 进行的,它将返回在试图发现该设备上的服务时发生的错误。如果未设置remoteAddress() 且设备是通过扫描发现的,则不会保存单个设备在服务发现过程中发生的错误,也不会发出任何信号。在这种情况下,出错是很正常的,因为有些设备可能对发现没有反应,或者可能不再在范围内。此类错误会被抑制。如果没有返回服务,则可以认为无法发现服务。

之前可能出现的错误会在重新启动发现时清除。

[signal, since 6.2] void QBluetoothServiceDiscoveryAgent::errorOccurred(QBluetoothServiceDiscoveryAgent::Error error)

error 发生错误时会发出该信号。error 参数描述了发生的错误。

此函数在 Qt 6.2 中引入。

QString QBluetoothServiceDiscoveryAgent::errorString() const

返回服务发现过程中发生的最后一次错误的可读描述。

另请参阅 error() 和errorOccurred()。

[signal] void QBluetoothServiceDiscoveryAgent::finished()

蓝牙服务发现完成后会发出该信号。

QBluetoothDeviceDiscoveryAgent::finished() 信号不同的是,该信号甚至会在服务发现过程中发生错误时发出。因此,建议检查errorOccurred() 信号,以评估服务发现是否成功。

bool QBluetoothServiceDiscoveryAgent::isActive() const

如果服务发现当前处于活动状态,则返回true ;否则返回false 。调用stop() 可停止活动的发现。

QBluetoothAddress QBluetoothServiceDiscoveryAgent::remoteAddress() const

返回远程设备地址。如果未调用setRemoteAddress() ,函数将返回默认构造的QBluetoothAddress

另请参阅 setRemoteAddress()。

[signal] void QBluetoothServiceDiscoveryAgent::serviceDiscovered(const QBluetoothServiceInfo &info)

当发现info 所描述的蓝牙服务时,就会发出该信号。

注: 如果目标设备通过 SDP 宣传蓝牙低功耗服务,则传递的QBluetoothServiceInfo 参数可能包含蓝牙低功耗服务。同时支持传统蓝牙(BaseRate)和低能耗服务的设备需要这样做。

另请参阅 QBluetoothDeviceInfo::coreConfigurations().

bool QBluetoothServiceDiscoveryAgent::setRemoteAddress(const QBluetoothAddress &address)

将远程设备地址设置为address 。如果address 为默认构造,则将在所有可联系的蓝牙设备上发现服务。只有在没有服务发现的情况下才能设置新的远程地址,否则该函数将返回 false。

在某些平台上,服务发现可能会导致配对请求。因此不建议在所有设备上进行服务发现。此函数可用于将服务发现限制在特定设备上。

另请参阅 remoteAddress()。

void QBluetoothServiceDiscoveryAgent::setUuidFilter(const QBluetoothUuid &uuid)

这是一个重载成员函数,提供方便。

将 UUID 过滤器设置为包含单一元素uuid 的列表。匹配适用于服务的ServiceIdServiceClassIds 属性。

另请参阅 uuidFilter() 。

void QBluetoothServiceDiscoveryAgent::setUuidFilter(const QList<QBluetoothUuid> &uuids)

将 UUID 过滤器设置为uuids 。只有与uuids 中的 UUID 匹配的服务才会被返回。匹配适用于服务的ServiceIdServiceClassIds 属性。

空 UUID 列表等同于只包含QBluetoothUuid::ServiceClassUuid::PublicBrowseGroup 的列表。

另请参阅 uuidFilter()。

[slot] void QBluetoothServiceDiscoveryAgent::start(QBluetoothServiceDiscoveryAgent::DiscoveryMode mode = MinimalDiscovery)

mode 指定要执行的服务发现类型。

在某些平台上,设备发现可能会导致配对请求。

另请参阅 DiscoveryMode

[slot] void QBluetoothServiceDiscoveryAgent::stop()

停止服务发现进程。搜索停止后,将发出canceled() 信号。

QList<QBluetoothUuid> QBluetoothServiceDiscoveryAgent::uuidFilter() const

返回 UUID 过滤器。

另请参见 setUuidFilter()。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.