Qt Bluetooth

蓝牙 API 提供蓝牙使能设备之间的连接。

目前,API 支持下列平台:

API 特征 Android iOS Linux (BlueZ 5.x) macOS Qt for Windows
经典蓝牙 x x x x
蓝牙 LE 中心 x x x x x
蓝牙 LE 外围 x x x x
蓝牙 LE 广告和扫描

Qt 5.14 adds a native Win32 port supporting Classic Bluetooth on Windows 7 or newer, and Bluetooth LE on Windows 8 or newer. It must be enabled at build time by configuration option -native-win32-bluetooth. The UWP backend is used by default if this option is not set and the Win32 target platform supports the required UWP APIs (minimal requirement is Windows 10 version 1507, with slightly improved service discovery since Windows 10 version 1607).

概述

Bluetooth is a short-range (less than 100 meters) wireless technology. It has a reasonably high data transfer rate of 2.1 Mbit/s, which makes it ideal for transferring data between devices. Bluetooth connectivity is based on basic device management, such as scanning for devices, gathering information about them, and exchanging data between them.

Qt Bluetooth supports Bluetooth Low Energy development for client/central role use cases. Further details can be found in the 低功耗蓝牙概述 章节。

使用模块

使用 Qt 模块要求直接或透过其它依赖链接到模块库。一些构建工具为此有贡献支持,包括 CMake and qmake .

构建采用 CMake

使用 find_package() 命令去定位所需模块组件,在 Qt6 包:

find_package(Qt6 COMPONENTS Bluetooth REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Bluetooth)
					

另请参阅 构建采用 CMake 概述。

构建采用 qmake

Add bluetooth QT 变量:

QT += bluetooth
					

构建 Qt Bluetooth

Despite the fact that the module can be built for all Qt platforms, the module is not ported to all of them. Not supported platforms employ a fake or dummy backend which is automatically selected when the platform is not supported. The dummy backend reports appropriate error messages and values which allow the Qt Bluetooth developer to detect at runtime that the current platform is not supported. The dummy backend is also selected on Linux if BlueZ development headers are not found during build time or Qt was built without Qt D-Bus support.

The usage of the dummy backend is highlighted via an appropriate warning while building and running.

macOS Specific

The Bluetooth API on macOS requires a certain type of event dispatcher that in Qt causes a dependency to QGuiApplication. However, you can set the environment variable QT_EVENT_DISPATCHER_CORE_FOUNDATION=1 to circumvent this issue.

Applications that don't use Classic Bluetooth will find a subset of QtBluetooth is available, as CoreBluetooth (Bluetooth LE) do not require either of QApplication or QGuiApplication.

指南

参考

日志类别

QtBluetooth 模块导出下列 日志类别 :

日志类别 描述
qt.bluetooth Enables logging of cross platform code path in QtBluetooth
qt.bluetooth.android Enables logging of the Android 实现
qt.bluetooth.bluez Enables logging of the BLuez/Linux implementation
qt.bluetooth.ios Enables logging of the iOS 实现
qt.bluetooth.osx Enables logging of the macOS 实现
qt.bluetooth.windows Enables logging of the Qt for Windows 实现

日志类别可用于启用额外警告和调试输出为 QtBluetooth 。可以找到有关日志的更多详细信息在 QLoggingCategory . A quick way to enable all QtBluetooth logging is to add the following line to the main() 函数:

QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
					

范例

模块演变

Changes to Qt Bluetooth 列出了 Qt 的 Qt 6 系列在模块 API 和功能上所做出的重要改变。

许可和归属

Qt Bluetooth 在商业许可下是可用的来自 Qt 公司 。此外,它是可用的根据 GNU LGPL (次一般公共许可) 第 3 版 ,或 GNU GPL (一般公共许可) 第 2 版 。见 Qt 许可 进一步了解细节。

On Linux, Qt Bluetooth uses a separate executable, sdpscanner , to integrate with the official Linux bluetooth protocol stack BlueZ. BlueZ is available under the GNU GPL (一般公共许可) 第 2 版 .

BlueZ GNU General Public License v2.0 only (This does not force user code to be GPL'ed. For more info see details.)