本页内容

QLowEnergyCharacteristic Class

QLowEnergyCharacteristic 类用于存储有关蓝牙低功耗服务特征的信息。更多内容...

头文件: #include <QLowEnergyCharacteristic>
CMake: find_package(Qt6 REQUIRED COMPONENTS Bluetooth)
target_link_libraries(mytarget PRIVATE Qt6::Bluetooth)
qmake: QT += bluetooth

公共类型

enum PropertyType { Unknown, Broadcasting, Read, WriteNoResponse, Write, …, ExtendedProperty }
flags PropertyTypes

公共函数

QLowEnergyCharacteristic()
QLowEnergyCharacteristic(const QLowEnergyCharacteristic &other)
~QLowEnergyCharacteristic()
(since 6.2) QLowEnergyDescriptor clientCharacteristicConfiguration() const
QLowEnergyDescriptor descriptor(const QBluetoothUuid &uuid) const
QList<QLowEnergyDescriptor> descriptors() const
bool isValid() const
QString name() const
QLowEnergyCharacteristic::PropertyTypes properties() const
QBluetoothUuid uuid() const
QByteArray value() const
QLowEnergyCharacteristic &operator=(const QLowEnergyCharacteristic &other)

静态公共成员

(since 6.2) const QByteArray CCCDDisable
(since 6.2) const QByteArray CCCDEnableIndication
(since 6.2) const QByteArray CCCDEnableNotification
bool operator!=(const QLowEnergyCharacteristic &a, const QLowEnergyCharacteristic &b)
bool operator==(const QLowEnergyCharacteristic &a, const QLowEnergyCharacteristic &b)

详细说明

QLowEnergyCharacteristic 提供了有关蓝牙低功耗服务特征的name()、uuid()、value()、properties() 和descriptors() 的信息。要获取该特征的规范和信息,必须使用QLowEnergyService 和QLowEnergyController 类连接到设备。

特征值可通过管理该特征所属服务的QLowEnergyService 实例进行写入。QLowEnergyService::writeCharacteristic()函数用于写入新值。操作成功时会触发QLowEnergyService::characteristicWritten()信号。该对象的value()方法会据此自动更新。

特征可能包含零个、一个或多个描述符。可使用descriptor()函数单独检索它们。descriptors()函数将所有描述符作为列表返回。描述符的一般用途是为特征添加上下文信息。 例如,描述符可能提供格式或范围信息,以指定应如何解释该特征的值。

另请参阅 QLowEnergyService 和QLowEnergyDescriptor 。

成员类型文档

enum QLowEnergyCharacteristic::PropertyType
flags QLowEnergyCharacteristic::PropertyTypes

此枚举描述了特征的属性。

常量值描述
QLowEnergyCharacteristic::Unknown0x00类型未知。
QLowEnergyCharacteristic::Broadcasting0x01允许广播通用属性(GATT)特征值。
QLowEnergyCharacteristic::Read0x02允许读取特征值。
QLowEnergyCharacteristic::WriteNoResponse0x04允许写入不返回响应的特征值。
QLowEnergyCharacteristic::Write0x08允许写入特征值。
QLowEnergyCharacteristic::Notify0x10允许通知特征值。
QLowEnergyCharacteristic::Indicate0x20允许指示特征值。
QLowEnergyCharacteristic::WriteSigned0x40允许对 GATT 特征值进行带签名的写入。
QLowEnergyCharacteristic::ExtendedProperty0x80其他特征属性在特征的扩展属性描述符中定义。

不建议在同一特征上同时设置“Notify”和“Indicate”属性,因为底层蓝牙堆栈的行为因平台而异。请参阅QLowEnergyCharacteristic::clientCharacteristicConfiguration

PropertyTypes 类型是QFlags<PropertyType> 的 typedef。它存储 PropertyType 值的“或”组合。

另请参阅 properties()。

成员函数文档

QLowEnergyCharacteristic::QLowEnergyCharacteristic()

创建一个新的 QLowEnergyCharacteristic 对象。该类的默认构造实例始终无效。

另请参阅 isValid()。

QLowEnergyCharacteristic::QLowEnergyCharacteristic(const QLowEnergyCharacteristic &other)

构建一个新的 QLowEnergyCharacteristic,该对象是other 的副本。

这两个副本将继续共享相同的底层数据,该数据在写入时不会被分离。

[noexcept] QLowEnergyCharacteristic::~QLowEnergyCharacteristic()

销毁QLowEnergyCharacteristic 对象。

[since 6.2] QLowEnergyDescriptor QLowEnergyCharacteristic::clientCharacteristicConfiguration() const

返回客户端特征配置描述符;如果不存在客户端特征配置描述符,则返回一个无效的QLowEnergyDescriptor 实例。

BTLE特性可支持通知和/或指示。在这两种情况下,外设都会在特性值发生每次变化时通知主设备。在BTLE属性协议中,通知消息无需主设备确认,而指示消息则需要确认。通知速度较快但可靠性较低,而指示速度较慢但可靠性更高。

如果某个特性支持通知或指示,可以通过向客户端特性配置描述符写入特殊的位模式来启用这些功能。为方便起见,这些位模式分别定义为QLowEnergyCharacteristic::CCCDDisable 、QLowEnergyCharacteristic::CCCDEnableNotification 和QLowEnergyCharacteristic::CCCDEnableIndication 。

例如,要在名为myservice 的服务中为名为mycharacteristic 的特征启用通知功能,可使用以下代码。

auto cccd = mycharacteristic.clientCharacteristicConfiguration();
if (!cccd.isValid()) {
    // your error handling
    return error;
}
myservice->writeDescriptor(cccd, QLowEnergyCharacteristic::CCCDEnableNotification);

注意:调用 characteristic.clientCharacteristicConfiguration() 等同于调用characteristic.descriptor(QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration) 。

注意:不建议 对同一特征同时使用通知和指示。这既适用于服务器端配置特征时,也适用于客户端启用特征时。 不同平台的蓝牙协议栈行为各不相同,跨平台行为可能不一致。例如,如果同时支持这两种机制,Bluez Linux 客户端可能会无条件地尝试同时启用它们,而 macOS 客户端则可能会无条件地仅启用通知功能。如果需要同时启用这两种机制,请考虑创建两个独立的特征。

此函数于 Qt 6.2 中引入。

另请参阅 descriptor()。

QLowEnergyDescriptor QLowEnergyCharacteristic::descriptor(const QBluetoothUuid &uuid) const

返回uuid 或一个无效的QLowEnergyDescriptor 实例的描述符。

另请参阅 descriptors()。

QList<QLowEnergyDescriptor> QLowEnergyCharacteristic::descriptors() const

返回属于该特征的描述符列表;否则返回一个空列表。

另请参阅 descriptor()。

bool QLowEnergyCharacteristic::isValid() const

如果QLowEnergyCharacteristic 对象有效,则返回true ;否则返回false 。

无效的特征对象要么未与任何服务相关联(默认构造),要么由于与底层蓝牙低功耗设备断开连接等原因,其关联的服务已不再有效。一旦对象失效,便无法恢复有效状态。

注意:如果 QLowEnergyCharacteristic 实例因与底层设备断开连接而失效,当前实例封装的信息将保持断开连接时的状态。因此,在断开连接事件发生后仍可检索该信息。

QString QLowEnergyCharacteristic::name() const

返回该特征的人类可读名称。

该名称基于特征的uuid(),该名称必须已标准化。特征类型的完整列表可在Bluetooth.org的“特征”部分中找到。

如果uuid() 的值未知,则返回的字符串为空。

另请参阅 QBluetoothUuid::characteristicToString()。

QLowEnergyCharacteristic::PropertyTypes QLowEnergyCharacteristic::properties() const

返回该特征的属性。

这些属性定义了该特征的访问权限。

QBluetoothUuid QLowEnergyCharacteristic::uuid() const

如果 `isValid()` 返回 `true`,则返回该特征的 UUID;否则返回 `null ` 的 UUID。

QByteArray QLowEnergyCharacteristic::value() const

返回该特征的缓存值。

如果该特性的properties()允许写入新值,则可使用QLowEnergyService::writeCharacteristic()更新该值。

缓存会在相关服务的detail discovery 期间、read/write 操作成功时,或者收到更新通知时进行更新。

如果特征不具备read permission ,则返回的QByteArray 始终为空。在这种情况下,只有QLowEnergyService::characteristicChanged()或QLowEnergyService::characteristicWritten()才能提供有关该特征值的信息。

QLowEnergyCharacteristic &QLowEnergyCharacteristic::operator=(const QLowEnergyCharacteristic &other)

创建other 的副本,并将其赋值给此QLowEnergyCharacteristic 对象。这两个副本继续共享相同的服务和控制器详细信息。

成员变量文档

[since 6.2] const QByteArray QLowEnergyCharacteristic::CCCDDisable

写入到“客户端特性配置描述符”中的位模式,用于同时禁用通知和指示。

该变量在 Qt 6.2 中引入。

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration 。

[since 6.2] const QByteArray QLowEnergyCharacteristic::CCCDEnableIndication

写入到“客户端特性配置描述符”中的位模式,用于启用指示功能。

该变量在 Qt 6.2 中引入。

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration 。

[since 6.2] const QByteArray QLowEnergyCharacteristic::CCCDEnableNotification

写入“客户端特性配置描述符”以启用通知的位模式。

该变量在 Qt 6.2 中引入。

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration 。

相关的非成员

bool operator!=(const QLowEnergyCharacteristic &a, const QLowEnergyCharacteristic &b)

如果 `a ` 和 `b ` 不相等,则返回 `true `;否则返回 `false`。

如果两个 QLowEnergyCharcteristic 实例指代同一台远程蓝牙低功耗设备上的同一特性,或者这两个实例均由默认构造函数构造,则认为它们相等。

bool operator==(const QLowEnergyCharacteristic &a, const QLowEnergyCharacteristic &b)

如果 `a ` 等于 `b`,则返回 `true `;否则返回 `false`。

如果两个 `QLowEnergyCharacteristic ` 实例指代同一台远程蓝牙低功耗设备上的同一特征,或者这两个实例均已通过默认构造函数初始化,则视为相等。

© 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.