Qt Bluetooth 蓝牙低功耗概述
Qt Bluetooth API 的典型用例是
- 检索本地蓝牙设备的信息。
- 扫描范围内的其他蓝牙设备并获取相关信息。
- 使用 OBEX 对象推送配置文件(OPP)向远程设备推送文件
- 使用串行端口配置文件 (SPP) 通过 RFCOMM 通道连接远程设备。
- 使用 SPP 创建一个允许传入连接的 RFCOMM 服务器。
- 检索蓝牙低功耗设备的规范。
- 连接到蓝牙低功耗设备。
- 接收来自低功耗蓝牙设备的广告。
请注意,Android 和 Windows 不支持对象推送规范。
注意: Qt 无法在 Windows 上配置 RFCOMM 的部分功能。服务的ServiceClassIds 和ProtocolDescriptorList 会自动填写。因此,在 Windows 上使用这些字段的自定义值注册服务可能不会产生预期结果。
注意: Win32 后端不支持蓝牙 LE 设备公布的接收信号强度指示器(RSSI)和制造商特定数据。此外,只有通过 Windows 设置才能找到以前配对过的设备。
下文将介绍如何使用Qt Bluetooth C++ API 类来处理上述用例。
检索本地设备信息
Qt Bluetooth API 有三个主要用途。第一个目的是获取本地和远程设备信息。检索设备信息的第一步是检查设备上的蓝牙是否可用,并读取本地设备地址和名称。QBluetoothLocalDevice 是提供所有这些信息的类。此外,您还可以用它来打开/关闭蓝牙、设置设备的可见性并确定当前连接。
QBluetoothLocalDevice localDevice; QString localDeviceName; // Check if Bluetooth is available on this device if (localDevice.isValid()) { // Turn Bluetooth on localDevice.powerOn(); // Read local device name localDeviceName = localDevice.name(); // Make it visible to others localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable); // Get connected devices QList<QBluetoothAddress> remotes; remotes = localDevice.connectedDevices(); }
扫描蓝牙设备
与QBluetoothLocalDevice 类似,API 也提供了QBluetoothDeviceInfo ,可为远程设备提供类似信息。虽然您可以自行创建QBluetoothDeviceInfo 对象并将数据填入其中,但更简单的方法是使用QBluetoothDeviceDiscoveryAgent 开始自动搜索可连接范围内的可见蓝牙设备。
voidMyClass::startDeviceDiscovery() {// 创建一个发现代理并连接其信号 QBluetoothDeviceDiscoveryAgent*discoveryAgent = newQBluetoothDeviceDiscoveryAgent(this); connect(discoveryAgent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this,SLOT(deviceDiscovered(QBluetoothDeviceInfo)));// 启动发现 discoveryAgent->start();//...}// 在本地槽中,读取找到的设备信息voidMyClass::deviceDiscovered(constQBluetoothDeviceInfo&device){ qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')'; }
在设备之间交换数据
两个支持蓝牙的设备之间更灵活的通信方式是创建一个虚拟串行端口连接,并通过该连接自由交换数据。这可以通过串行端口配置文件(SPP)来实现。串行端口配置文件通过蓝牙传输协议 RFCOMM 模拟串行连接。
要接收传入的 SPP 连接,需要使用QBluetoothServer 监听传入的连接。
rfcommServer= newQBluetoothServer(QBluetoothServiceInfoRfcommProtocol, this);connect(rfcommServer, &QBluetoothServernewConnection, this, QOverload<>::of(&ChatServer::clientConnected));boolresult= rfcommServer->listen(localAdapter);if(!result) { qWarning() << "Cannot bind chat server to" << localAdapter.toString(); return; }
使用QBluetoothSocket 从另一个扮演客户端角色的设备连接到该服务器:
voidChatClient::startClient(constQBluetoothServiceInfo&remoteService) {if(socket)return;// Connect to servicesocket= newQBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); qDebug() << "Create socket"; socket->connectToService(remoteService); qDebug() << "ConnectToService done"; connect(socket, &QBluetoothSocket::readyRead, this, &ChatClient::readSocket); connect(socket, &QBluetoothSocketconnect(socket, &::disconnected, this, QOverload<>::of(&ChatClient::connected)); connect(socket, &QBluetoothSocket连接(socket, &::connected,this,& ChatClient::disconnected); connect(socket, &QBluetoothSocket连接(socket, &::errorOccurred, this,&ChatClient::onSocketErrorOccurred);}
使用这种连接可以双向交换任何形式的数据。它非常适合游戏或在两个设备上的两个应用程序实例之间同步状态。有关如何配置服务器和客户端的详细说明,请参阅QBluetoothServer 和QBluetoothSocket 类中的详细说明部分。蓝牙聊天示例是开始使用 SPP 的一个好例子。
蓝牙低功耗
蓝牙低功耗(也称蓝牙智能)是一种新技术,可让低能耗设备相互通信。有关该技术和相关 Qt API 的更多详情,请参阅蓝牙低能耗概述。
© 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.