QBluetoothServiceInfo Class
QBluetoothServiceInfo 类允许访问蓝牙服务的属性。更多内容...
| 头文件: | #include <QBluetoothServiceInfo> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Bluetooth) target_link_libraries(mytarget PRIVATE Qt6::Bluetooth) |
| qmake: | QT += bluetooth |
公共类型
| class | Alternative |
| class | Sequence |
| enum | AttributeId { ServiceRecordHandle, ServiceClassIds, ServiceRecordState, ServiceId, ProtocolDescriptorList, …, ServiceProvider } |
| enum | Protocol { UnknownProtocol, L2capProtocol, RfcommProtocol } |
公共函数
| QBluetoothServiceInfo() | |
| QBluetoothServiceInfo(const QBluetoothServiceInfo &other) | |
| ~QBluetoothServiceInfo() | |
| QVariant | attribute(quint16 attributeId) const |
| QList<quint16> | attributes() const |
| bool | contains(quint16 attributeId) const |
| QBluetoothDeviceInfo | device() const |
| bool | isComplete() const |
| bool | isRegistered() const |
| bool | isValid() const |
| QBluetoothServiceInfo::Sequence | protocolDescriptor(QBluetoothUuid::ProtocolUuid protocol) const |
| int | protocolServiceMultiplexer() const |
| bool | registerService(const QBluetoothAddress &localAdapter = QBluetoothAddress()) |
| void | removeAttribute(quint16 attributeId) |
| int | serverChannel() const |
| quint8 | serviceAvailability() const |
| QList<QBluetoothUuid> | serviceClassUuids() const |
| QString | serviceDescription() const |
| QString | serviceName() const |
| QString | serviceProvider() const |
| QBluetoothUuid | serviceUuid() const |
| void | setAttribute(quint16 attributeId, const QBluetoothServiceInfo::Alternative &value) |
| void | setAttribute(quint16 attributeId, const QBluetoothServiceInfo::Sequence &value) |
| void | setAttribute(quint16 attributeId, const QBluetoothUuid &value) |
| void | setAttribute(quint16 attributeId, const QVariant &value) |
| void | setDevice(const QBluetoothDeviceInfo &device) |
| void | setServiceAvailability(quint8 availability) |
| void | setServiceDescription(const QString &description) |
| void | setServiceName(const QString &name) |
| void | setServiceProvider(const QString &provider) |
| void | setServiceUuid(const QBluetoothUuid &uuid) |
| QBluetoothServiceInfo::Protocol | socketProtocol() const |
| bool | unregisterService() |
| QBluetoothServiceInfo & | operator=(const QBluetoothServiceInfo &other) |
详细说明
QBluetoothServiceInfo 提供有关蓝牙设备所提供服务的信息。此外,它还可用于在本地设备上注册新服务。请注意,此类注册仅影响蓝牙 SDP 条目。 任何监听传入连接的服务器(例如 RFCOMM 服务器)必须在调用registerService() 之前启动。注销操作必须按相反顺序进行。
QBluetoothServiceInfo 并非传统意义上的值类型。同一服务信息对象的所有副本共享相同的数据,因为修改时不会将其分离。这确保了两个副本可以对同一蓝牙服务进行(注销)注册。
在 iOS 上,无法使用此类,因为该平台未提供允许访问 QBluetoothServiceInfo 相关功能的 API。
成员类型文档
enum QBluetoothServiceInfo::AttributeId
蓝牙服务属性。有关这些属性的更详细说明,请查阅《蓝牙核心规范》。
| 常量 | 值 | 描述 |
|---|---|---|
QBluetoothServiceInfo::ServiceRecordHandle | 0x0000 | 指定一个可用于检索属性的服务记录。 |
QBluetoothServiceInfo::ServiceClassIds | 0x0001 | 该服务所符合的服务类的 UUID。最常见的服务类在 (QBluetoothUuid::ServiceClassUuid) 中定义 |
QBluetoothServiceInfo::ServiceRecordState | 0x0002 | 当任何其他服务属性被添加、删除或修改时,该属性也会随之变化。 |
QBluetoothServiceInfo::ServiceId | 0x0003 | 唯一标识该服务的 UUID。 |
QBluetoothServiceInfo::ProtocolDescriptorList | 0x0004 | 该服务使用的协议列表。最常见的协议 UUID 定义在QBluetoothUuid::ProtocolUuid |
QBluetoothServiceInfo::BrowseGroupList | 0x0005 | 该服务所属的浏览组列表。 |
QBluetoothServiceInfo::LanguageBaseAttributeIdList | 0x0006 | 用于支持可读属性的语言基础属性ID列表。 |
QBluetoothServiceInfo::ServiceInfoTimeToLive | 0x0007 | 服务记录预计保持有效且不变的秒数。 |
QBluetoothServiceInfo::ServiceAvailability | 0x0008 | 指示服务可用性的值。 |
QBluetoothServiceInfo::BluetoothProfileDescriptorList | 0x0009 | 该服务所遵循的配置文件列表。 |
QBluetoothServiceInfo::DocumentationUrl | 0x000A | 指向该服务文档的 URL。 |
QBluetoothServiceInfo::ClientExecutableUrl | 0x000B | 指向可用于利用该服务的应用程序位置的 URL。 |
QBluetoothServiceInfo::IconUrl | 0x000C | 指向代表该服务的图标位置的 URL。 |
QBluetoothServiceInfo::AdditionalProtocolDescriptorList | 0x000D | 服务使用的其他协议。该属性扩展了ProtocolDescriptorList 。 |
QBluetoothServiceInfo::PrimaryLanguageBase | 0x0100 | 主要语言文本描述符的基索引。 |
QBluetoothServiceInfo::ServiceName | PrimaryLanguageBase + 0x0000 | 蓝牙服务在主语言中的名称。 |
QBluetoothServiceInfo::ServiceDescription | PrimaryLanguageBase + 0x0001 | 主语言中的蓝牙服务描述。 |
QBluetoothServiceInfo::ServiceProvider | PrimaryLanguageBase + 0x0002 | 提供该蓝牙服务主要语言的公司/实体名称。 注意:在 Windows平台上, 创建服务时,ServiceClassIds 和 ProtocolDescriptorList 会自动设置为默认值。手动设置这些属性的值将无法生效,并且可能会在此平台上导致意外结果。 |
enum QBluetoothServiceInfo::Protocol
此枚举描述了服务所使用的套接字协议。
| 常量 | 值 | 描述 |
|---|---|---|
QBluetoothServiceInfo::UnknownProtocol | 0 | 该服务使用未知套接字协议。 |
QBluetoothServiceInfo::L2capProtocol | 1 | 该服务使用 L2CAP 套接字协议。Android 不支持该协议用于直接套接字连接。 |
QBluetoothServiceInfo::RfcommProtocol | 2 | 该服务使用 RFCOMM 套接字协议。 |
成员函数文档
QBluetoothServiceInfo::QBluetoothServiceInfo()
构建一个新的无效的 QBluetoothServiceInfo 对象;
QBluetoothServiceInfo::QBluetoothServiceInfo(const QBluetoothServiceInfo &other)
创建一个新的QBluetoothServiceInfo对象,该对象是other 的副本。
这两个副本继续共享相同的底层数据,该数据在写入时不会分离。
[noexcept] QBluetoothServiceInfo::~QBluetoothServiceInfo()
销毁QBluetoothServiceInfo 对象。
QVariant QBluetoothServiceInfo::attribute(quint16 attributeId) const
返回属性attributeId 的值。
另请参阅 setAttribute()。
QList<quint16> QBluetoothServiceInfo::attributes() const
返回QBluetoothServiceInfo 对象所拥有的一切属性ID的列表。
bool QBluetoothServiceInfo::contains(quint16 attributeId) const
如果QBluetoothServiceInfo 对象包含attributeId 属性,则返回true;否则返回false。
QBluetoothDeviceInfo QBluetoothServiceInfo::device() const
返回提供此服务的蓝牙设备的地址。
另请参阅 setDevice()。
bool QBluetoothServiceInfo::isComplete() const
如果QBluetoothServiceInfo 对象被视为完整,则返回true;否则返回false。
一个完整的QBluetoothServiceInfo 对象包含一个ProtocolDescriptorList 属性。
bool QBluetoothServiceInfo::isRegistered() const
如果服务信息已在平台的“服务发现协议”(SDP)实现中注册,则返回 true;否则返回 false。
bool QBluetoothServiceInfo::isValid() const
如果QBluetoothServiceInfo 对象有效,则返回 true;否则返回 false。
无效的QBluetoothServiceInfo 对象将不包含任何属性。
QBluetoothServiceInfo::Sequence QBluetoothServiceInfo::protocolDescriptor(QBluetoothUuid::ProtocolUuid protocol) const
返回协议protocol 的协议参数,格式为QBluetoothServiceInfo::Sequence 。
如果不支持protocol ,则返回一个空的 `QBluetoothServiceInfo::Sequence `。
int QBluetoothServiceInfo::protocolServiceMultiplexer() const
这是一个便捷函数。对于支持 L2CAP 协议的服务,返回相应的协议/服务多路复用器;否则返回 -1。
该函数相当于从QBluetoothServiceInfo::attribute 返回的QBluetoothServiceInfo::Sequence 中提取信息(QBluetoothServiceInfo::ProtocolDescriptorList )。
bool QBluetoothServiceInfo::registerService(const QBluetoothAddress &localAdapter = QBluetoothAddress())
将该服务注册到平台的服務发现协议(SDP)实现中,使其在其他设备执行服务发现时可被发现。如果服务注册成功,则返回 true;否则返回 false。注册完成后,无法对记录进行修改。必须先注销该服务,然后带着修改后的内容重新注册。
localAdapter 参数用于指定服务应注册到的本地蓝牙适配器。若localAdapter 的值为null ,则使用默认的蓝牙适配器。如果该服务信息对象已通过某个本地适配器注册,而本次调用该函数时使用了不同的本地适配器,则会移除之前的注册,并使用新适配器重新注册该服务。
void QBluetoothServiceInfo::removeAttribute(quint16 attributeId)
从QBluetoothServiceInfo 对象中移除attributeId 属性。
如果该服务信息已在平台的 SDP 数据库中注册,则该数据库条目不会被更新,直到再次调用registerService() 方法为止。
int QBluetoothServiceInfo::serverChannel() const
这是一个便捷函数。对于支持 RFCOMM 协议的服务,返回其服务器通道;否则返回 -1。
该函数相当于从QBluetoothServiceInfo::attribute (QBluetootherServiceInfo::ProtocolDescriptorList)返回的QBluetoothServiceInfo::Sequence 中提取信息。
quint8 QBluetoothServiceInfo::serviceAvailability() const
这是一个便捷函数。它相当于调用 attribute(QBluetoothServiceInfo::ServiceAvailability).toUInt()。
返回服务的可用性。
另请参阅 setServiceAvailability() 和attribute()。
QList<QBluetoothUuid> QBluetoothServiceInfo::serviceClassUuids() const
返回一个 UUID 列表,描述了该服务所遵循的服务类。
这是一个便捷函数。它等同于调用 attribute(QBluetoothServiceInfo::ServiceClassIds).value<QBluetoothServiceInfo::Sequence>(),然后遍历其QBluetoothUuid 条目。
另请参阅 attribute()。
QString QBluetoothServiceInfo::serviceDescription() const
这是一个便捷函数。它相当于调用 attribute(QBluetoothServiceInfo::ServiceDescription).toString()。
返回主语言版本的服务描述。
另请参阅 setServiceDescription() 和attribute()。
QString QBluetoothServiceInfo::serviceName() const
这是一个便捷函数。它相当于调用 attribute(QBluetoothServiceInfo::ServiceName).toString()。
返回主语言中的服务名称。
另请参阅 setServiceName() 和attribute()。
QString QBluetoothServiceInfo::serviceProvider() const
这是一个便捷函数。它相当于调用 attribute(QBluetoothServiceInfo::ServiceProvider).toString()。
返回主语言的服务提供商。
另请参阅 setServiceProvider() 和attribute()。
QBluetoothUuid QBluetoothServiceInfo::serviceUuid() const
这是一个便捷函数。它等同于调用 attribute(QBluetoothServiceInfo::ServiceId).value<QBluetoothUuid>()。
返回该服务的自定义 UUID。该 UUID 可能为 null。基于Bluetooth SIG 标准的UUID 应通过serviceClassUuids() 获取。
另请参阅 setServiceUuid() 和attribute()。
void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QBluetoothServiceInfo::Alternative &value)
这是一个便捷函数。
将由attributeId 标识的属性设置为value 。
如果服务信息已在平台的 SDP 数据库中注册,则在再次调用registerService() 之前,数据库条目不会被更新。
另请参阅 attribute()。
void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QBluetoothServiceInfo::Sequence &value)
这是一个便捷函数。
将由attributeId 标识的属性设置为value 。
如果该服务信息已注册到平台的 SDP 数据库中,则在再次调用registerService() 之前,数据库条目不会被更新。
void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QBluetoothUuid &value)
这是一个便捷函数。
将由attributeId 标识的属性设置为value 。
如果该服务信息已在平台的 SDP 数据库中注册,则该数据库条目不会被更新,直到再次调用registerService() 为止。
void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QVariant &value)
将由attributeId 标识的属性设置为value 。
如果该服务信息已注册到平台的 SDP 数据库中,则在再次调用registerService() 之前,数据库条目不会被更新。
注意:如果 某个属性要求使用字节编码的值(例如蓝牙 HID 服务),则应将其设置为QByteArray 。
另请参阅 isRegistered() 和registerService()。
void QBluetoothServiceInfo::setDevice(const QBluetoothDeviceInfo &device)
将提供此服务的蓝牙设备设置为device 。
另请参阅 device()。
void QBluetoothServiceInfo::setServiceAvailability(quint8 availability)
这是一个便捷函数。它等同于调用setAttribute(QBluetoothServiceInfo::ServiceAvailability, availability)。
将服务的可用性设置为availability 。
另请参阅 serviceAvailability() 和setAttribute()。
void QBluetoothServiceInfo::setServiceDescription(const QString &description)
这是一个便捷函数。它相当于调用setAttribute(QBluetoothServiceInfo::ServiceDescription, description)。
将主语言下的服务描述设置为description 。
另请参阅 serviceDescription() 和setAttribute()。
void QBluetoothServiceInfo::setServiceName(const QString &name)
这是一个便捷函数。它等同于调用setAttribute(QBluetoothServiceInfo::ServiceName, name)。
将主语言中的服务名称设置为name 。
另请参阅 serviceName() 和setAttribute()。
void QBluetoothServiceInfo::setServiceProvider(const QString &provider)
这是一个便捷函数。它等同于调用setAttribute(QBluetoothServiceInfo::ServiceProvider, provider)。
将主语言中的服务提供商设置为provider 。
另请参阅 serviceProvider() 和setAttribute()。
void QBluetoothServiceInfo::setServiceUuid(const QBluetoothUuid &uuid)
这是一个便捷函数。它相当于调用setAttribute(QBluetoothServiceInfo::ServiceId, uuid)。
将自定义服务 UUID 设置为uuid 。不应使用此函数来设置标准化的服务 UUID。
另请参阅 serviceUuid() 和setAttribute()。
QBluetoothServiceInfo::Protocol QBluetoothServiceInfo::socketProtocol() const
返回QBluetoothServiceInfo 对象所使用的协议。
bool QBluetoothServiceInfo::unregisterService()
从平台的服務發現協議(SDP)实现中注销该服务。此后,其他设备将无法再通过服务发现找到该服务。
如果服务成功注销,则返回 true;否则返回 false。
QBluetoothServiceInfo &QBluetoothServiceInfo::operator=(const QBluetoothServiceInfo &other)
复制other ,并将其分配给此QBluetoothServiceInfo 对象。这两个副本将继续共享相同的服务和注册信息。
© 2026 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.