对Qt Bluetooth
Qt 6 的变更,是出于让该框架更高效、更易于使用的有意识努力。
我们力求在每个版本中保持所有公共 API 的二进制和源代码兼容性。但为了使 Qt 成为更好的框架,某些更改在所难免。
在本主题中,我们将总结QtBluetooth 中的这些变更,并提供处理这些变更的指导。
概述
- 已移除 qbluetoothglobal.h。qtbluetoothglobal.h 是具有相同用途的官方头文件。
- 所有对QVector 的调用均已替换为QList 。
- 已移除 Win32 后端。当使用 mingw 构建 Qt Bluetooth 时,将不再提供可用的蓝牙后端。
- 由于现在仅剩一个 Windows 后端,因此已移除日志类别 qt.bluetooth.winrt。相应的日志消息现已移至 qt.bluetooth.windows。
QBluetooth 命名空间
为了提高类型安全性,枚举类型 `QBluetooth::Security ` 和 `QBluetooth::AttAccessConstraint ` 已更改为带作用域的枚举。
QBluetoothDeviceDiscoveryAgent
已移除 QBluetoothDeviceDiscoveryAgent::inquiryType 属性及其相关的设置器/获取器与枚举。该 API 元素从未被实现和/或支持。
QBluetoothDeviceInfo
- manufacturerData() 返回QMultiHash 而非QHash 。自 Qt 6 起,QMultiHash 不再从QHash 派生。
- DataCompleteness已被移除且未被替代,因为相关的API元素已无实际用途
- setServiceUuids(constQList<QBluetoothUuid>, DataCompleteness) 已被 setServiceUuids(constQList<QBluetoothUuid> &uuids) 取代
- DataCompleteness serviceUuidsCompleteness() const 已被移除,且未提供替代函数。
- QList<QBluetoothUuid> serviceUuids(DataCompleteness *completeness = nullptr) const 已被QList<QBluetoothUuid> serviceUuids() const 取代。
QBluetoothLocalDevice
与配对代理相关的 API 已被移除,且未提供替代方案。这包括 pairingConfirmation(bool)、pairingDisplayPinCode(..) 和 pairingDisplayConfirmation(..)。除 Android 和 BlueZ 4 之外,该 API 从未被实现过。 Qt 6 中已移除对 BlueZ 4 的支持,且 Android 的实现至少在部分代码中需要无法获取的 BLUETOOTH_PRIVILEGED 权限。
QLowEnergyService
ServiceState 枚举值已重命名:
- DiscoveryRequired 变为RemoteService
- DiscoveringService 变为RemoteServiceDiscovering
- ServiceDiscovered 变为RemoteServiceDiscovered
此更改是出于对未来服务发现可能成为可选步骤的预期。
QBluetoothUuid
枚举ProtocolUuid 、ServiceClassUuid 、CharacteristicType 和DescriptorType 现已成为作用域枚举。用户代码中对枚举项的使用需相应调整。
QLowEnergyController
- 已移除带有远程QBluetoothAddress 参数的现有构造函数和create*()函数。在Apple系统中,仅使用QBluetoothAddress 不足以标识远程设备。该系统要求使用UUID为设备“命名”,这只能通过QBluetoothDeviceInfo 来满足。
- 剩余的QLowEnergyController 构造函数已被设为私有。应使用createPeripheral()和createCentral()函数来获取QLowEnergyController 实例。
QLowEnergyCharacteristic
- 引入了新方法 clientCharacteristicConfiguration(),以便于获取客户端特征配置描述符。
- 引入了常量CCCDDisable 、CCCDEnableNotification 和CCCDEnableIndication ,它们包含客户端特征配置描述符的可能取值。
- handle() 方法已不再属于蓝牙低功耗(Bluetooth LE)的公共接口。它作为特征标识符的功能已被取代,QLowEnergyCharacteristic 实例本身即作为标识符。
QLowEnergyDescriptor
handle() 方法已不再属于蓝牙低功耗(Bluetooth LE)的公共接口。其作为特征标识符的功能已被取代,QLowEnergyDescriptor 实例本身即作为标识符。
QBluetoothTransferManager
已移除 QBluetoothTransferManager、QBluetoothTransferRequest 和 QBluetoothTransferReply,且未提供替代方案。
QBluetoothSocket
QBluetoothSocket::SocketState 和QBluetoothSocket::SocketError 现已成为作用域枚举。用户代码中需相应调整枚举项的使用方式。
QML 接口
由于不建议通过 QML 进行套接字编程,因此已移除 QML 接口。QDeclarativeBluetoothService、QDeclarativeBluetoothSocket 和 QDeclarativeBluetoothDiscoveryModel(在 QML 中分别为:BluetoothService、BluetoothSocket 和 BluetoothDiscoveryModel)已被移除,且没有替代方案。
“error”信号
QBluetoothDeviceDiscoveryAgent 、QBluetoothLocalDevice 、QBluetoothServer 、QBluetoothServiceDiscoveryAgent 、QBluetoothSocket 、QLowEnergyController 以及QLowEnergyService 中的“error”信号已重命名为“errorOccurred”,以消除“error”信号与“error”获取器之间的歧义。
© 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.