QLowEnergyCharacteristic Class

The QLowEnergyCharacteristic class stores information about a Bluetooth Low Energy service characteristic. 更多...

头: #include <QLowEnergyCharacteristic>
qmake: QT += bluetooth

公共类型

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

公共函数

QLowEnergyCharacteristic ()
QLowEnergyCharacteristic (const QLowEnergyCharacteristic & other )
~QLowEnergyCharacteristic ()
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 )

静态公共成员

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

详细描述

QLowEnergyCharacteristic provides information about a Bluetooth Low Energy service characteristic's name (), uuid (), value (), properties (),和 descriptors (). To obtain the characteristic's specification and information, it is necessary to connect to the device using the QLowEnergyService and QLowEnergyController 类。

The characteristic value may be written via the QLowEnergyService instance that manages the service to which this characteristic belongs. The QLowEnergyService::writeCharacteristic () function writes the new value. The QLowEnergyService::characteristicWritten () signal is emitted upon success. The value () of this object is automatically updated accordingly.

Characteristics may contain none, one or more descriptors. They can be individually retrieved using the descriptor () 函数。 descriptors () function returns all descriptors as a list. The general purpose of a descriptor is to add contextual information to the characteristic. For example, the descriptor might provide format or range information specifying how the characteristic's value is to be interpreted.

另请参阅 QLowEnergyService and QLowEnergyDescriptor .

成员类型文档编制

enum QLowEnergyCharacteristic:: PropertyType
flags QLowEnergyCharacteristic:: PropertyTypes

This enum describes the properties of a characteristic.

常量 描述
QLowEnergyCharacteristic::Unknown 0x00 The type is not known.
QLowEnergyCharacteristic::Broadcasting 0x01 Allow for the broadcasting of Generic Attributes (GATT) characteristic values.
QLowEnergyCharacteristic::Read 0x02 Allow the characteristic values to be read.
QLowEnergyCharacteristic::WriteNoResponse 0x04 Allow characteristic values without responses to be written.
QLowEnergyCharacteristic::Write 0x08 Allow for characteristic values to be written.
QLowEnergyCharacteristic::Notify 0x10 Permits notification of characteristic values.
QLowEnergyCharacteristic::Indicate 0x20 Permits indications of characteristic values.
QLowEnergyCharacteristic::WriteSigned 0x40 Permits signed writes of the GATT characteristic values.
QLowEnergyCharacteristic::ExtendedProperty 0x80 Additional characteristic properties are defined in the characteristic's extended properties descriptor.

It is not recommended to set both Notify and Indicate properties on the same characteristic as the underlying Bluetooth stack behaviors differ from platform to platform. Please see QLowEnergyCharacteristic::clientCharacteristicConfiguration

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

另请参阅 properties ().

成员函数文档编制

QLowEnergyCharacteristic:: QLowEnergyCharacteristic ()

Construct a new QLowEnergyCharacteristic. A default-constructed instance of this class is always invalid.

另请参阅 isValid ().

QLowEnergyCharacteristic:: QLowEnergyCharacteristic (const QLowEnergyCharacteristic & other )

Construct a new QLowEnergyCharacteristic that is a copy of other .

The two copies continue to share the same underlying data which does not detach upon write.

QLowEnergyCharacteristic:: ~QLowEnergyCharacteristic ()

销毁 QLowEnergyCharacteristic 对象。

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

Returns the Client Characteristic Configuration Descriptor or an invalid QLowEnergyDescriptor instance if no Client Characteristic Configuration Descriptor exists.

BTLE characteristics can support notifications and/or indications. In both cases, the peripheral will inform the central on each change of the characteristic's value. In the BTLE attribute protocol, notification messages are not confirmed by the central, while indications are confirmed. Notifications are considered faster, but unreliable, while indications are slower and more reliable.

If a characteristic supports notification or indication, these can be enabled by writing special bit patterns to the Client Characteristic Configuration Descriptor. For convenience, these bit patterns are provided as QLowEnergyCharacteristic::CCCDDisable , QLowEnergyCharacteristic::CCCDEnableNotification ,和 QLowEnergyCharacteristic::CCCDEnableIndication .

Enabling e.g. notification for a characteristic named mycharacteristic in a service called myservice could be done using the following code.

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

注意: 调用 characteristic.clientCharacteristicConfiguration() 相当于调用 characteristic.descriptor(QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration) .

注意: It is not recommended to use both notifications and indications on the same characteristic. This applies to both server-side when setting up the characteristics, as well as client-side when enabling them. The bluetooth stack behavior differs from platform to platform and the cross-platform behavior will likely be inconsistent. As an example a Bluez Linux client might unconditionally try to enable both mechanisms if both are supported, whereas a macOS client might unconditionally enable just the notifications. If both are needed consider creating two separate characteristics.

该函数在 Qt 6.2 引入。

另请参阅 descriptor ().

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

Returns the descriptor for uuid or an invalid QLowEnergyDescriptor 实例。

另请参阅 descriptors ().

QList < QLowEnergyDescriptor > QLowEnergyCharacteristic:: descriptors () const

Returns the list of descriptors belonging to this characteristic; otherwise an empty list.

另请参阅 descriptor ().

bool QLowEnergyCharacteristic:: isValid () const

返回 true QLowEnergyCharacteristic object is valid, otherwise returns false .

An invalid characteristic object is not associated with any service (default-constructed) or the associated service is no longer valid due to a disconnect from the underlying Bluetooth Low Energy device, for example. Once the object is invalid it cannot become valid anymore.

注意: QLowEnergyCharacteristic instance turns invalid due to a disconnect from the underlying device, the information encapsulated by the current instance remains as it was at the time of the disconnect. Therefore it can be retrieved after the disconnect event.

QString QLowEnergyCharacteristic:: name () const

Returns the human-readable name of the characteristic.

The name is based on the characteristic's uuid () which must have been standardized. The complete list of characteristic types can be found under Bluetooth.org Characteristics .

The returned string is empty if the uuid () is unknown.

另请参阅 QBluetoothUuid::characteristicToString ().

QLowEnergyCharacteristic::PropertyTypes QLowEnergyCharacteristic:: properties () const

Returns the properties of the characteristic.

The properties define the access permissions for the characteristic.

QBluetoothUuid QLowEnergyCharacteristic:: uuid () const

Returns the UUID of the characteristic if isValid () 返回 true ; otherwise a null UUID.

QByteArray QLowEnergyCharacteristic:: value () const

Returns the cached value of the characteristic.

If the characteristic's properties () permit writing of new values, the value can be updated using QLowEnergyService::writeCharacteristic ().

The cache is updated during the associated service's detail discovery , a successful read / write operation or when an update notification is received.

返回的 QByteArray always remains empty if the characteristic does not have the read permission . In such cases only the QLowEnergyService::characteristicChanged () 或 QLowEnergyService::characteristicWritten () may provice information about the value of this characteristic.

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

制作副本为 other 并将其赋值给此 QLowEnergyCharacteristic object. The two copies continue to share the same service and controller details.

成员变量文档编制

[since 6.2] const QByteArray QLowEnergyCharacteristic:: CCCDDisable

Bit pattern to write into Client Characteristic Configuration Descriptor to disable both notification and indication.

This documentation was introduced in Qt 6.2.

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration .

[since 6.2] const QByteArray QLowEnergyCharacteristic:: CCCDEnableIndication

Bit pattern to write into Client Characteristic Configuration Descriptor to enable indication.

This documentation was introduced in Qt 6.2.

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration .

[since 6.2] const QByteArray QLowEnergyCharacteristic:: CCCDEnableNotification

Bit pattern to write into Client Characteristic Configuration Descriptor to enable notification.

This documentation was introduced in Qt 6.2.

另请参阅 QLowEnergyCharacteristic::clientCharacteristicConfiguration .

相关非成员

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

返回 true if a and b are not equal; otherwise false .

Two QLowEnergyCharcteristic instances are considered to be equal if they refer to the same characteristic on the same remote Bluetooth Low Energy device or both instances have been default-constructed.

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

返回 true if a 等于 b , otherwise false .

Two QLowEnergyCharacteristic instances are considered to be equal if they refer to the same characteristic on the same remote Bluetooth Low Energy device or both instances have been default-constructed.