QLowEnergyCharacteristic Class

QLowEnergyCharacteristic 类存储有关蓝牙低功耗服务特性的信息。更多

Header: #include <QLowEnergyCharacteristic>
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().要获取该特性的规格和信息,必须使用QLowEnergyServiceQLowEnergyController 类连接到设备。

特性值可通过管理该特性所属服务的QLowEnergyService 实例写入。QLowEnergyService::writeCharacteristic() 函数将写入新值。成功后将发出QLowEnergyService::characteristicWritten() 信号。该对象的value() 会自动相应更新。

特征可能不包含描述符,也可能包含一个或多个描述符。可以使用descriptor() 函数单独检索这些描述符。descriptors() 函数以列表形式返回所有描述符。描述符的一般目的是为特征添加上下文信息。例如,描述符可以提供格式或范围信息,指定如何解释特征值。

另请参阅 QLowEnergyServiceQLowEnergyDescriptor

成员类型文档

枚举 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其他特征属性在特征的扩展属性描述符中定义。

不建议在同一特性上同时设置通知和指示属性,因为不同平台的底层蓝牙堆栈行为各不相同。请参阅QLowEnergyCharacteristic::clientCharacteristicConfiguration

PropertyTypes 类型是QFlags<PropertyType> 的类型定义。它存储 PropertyType 值的 OR 组合。

另请参阅 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::CCCDDisableQLowEnergyCharacteristic::CCCDEnableNotificationQLowEnergyCharacteristic::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 Characteristics 下找到。

如果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

写入客户端特性配置描述符(Client Characteristic Configuration Descriptor)以禁用通知和指示的位模式。

此文档在 Qt 6.2 中引入。

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration

[since 6.2] const QByteArray QLowEnergyCharacteristic::CCCDEnableIndication

写入客户端特性配置描述符(Client Characteristic Configuration Descriptor)以启用指示的位模式。

此文档在 Qt 6.2 中引入。

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration

[since 6.2] const QByteArray QLowEnergyCharacteristic::CCCDEnableNotification

写入客户端特性配置描述符(Client Characteristic Configuration Descriptor)以启用通知的位模式。

此文档在 Qt 6.2 中引入。

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration

相关非成员

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

如果ab 不相等,则返回true ;否则返回false

如果两个 QLowEnergyCharcteristic 实例指向同一远程蓝牙低功耗设备上的相同特性,或两个实例都是默认构建的,则视为相等。

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

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

如果两个QLowEnergyCharacteristic 实例指向同一远程蓝牙低功耗设备上的相同特性,或两个实例都是默认构建的,则视为相等。

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