QBluetoothServer Class
QBluetoothServer 类使用 RFCOMM 或 L2cap 协议与蓝牙设备通信。更多
Header: | #include <QBluetoothServer> |
qmake: | QT += bluetooth |
Inherits: | QObject |
公共类型
enum | Error { NoError, UnknownError, PoweredOffError, InputOutputError, ServiceAlreadyRegisteredError, …, MissingPermissionsError } |
公共函数
QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject *parent = nullptr) | |
virtual | ~QBluetoothServer() |
void | close() |
QBluetoothServer::Error | error() const |
bool | hasPendingConnections() const |
bool | isListening() const |
bool | listen(const QBluetoothAddress &address = QBluetoothAddress(), quint16 port = 0) |
QBluetoothServiceInfo | listen(const QBluetoothUuid &uuid, const QString &serviceName = QString()) |
int | maxPendingConnections() const |
QBluetoothSocket * | nextPendingConnection() |
QBluetooth::SecurityFlags | securityFlags() const |
QBluetoothAddress | serverAddress() const |
quint16 | serverPort() const |
QBluetoothServiceInfo::Protocol | serverType() const |
void | setMaxPendingConnections(int numConnections) |
void | setSecurityFlags(QBluetooth::SecurityFlags security) |
信号
(since 6.2) void | errorOccurred(QBluetoothServer::Error error) |
void | newConnection() |
详细说明
QBluetoothServer 用于通过 RFCOMM 或 L2cap 实现蓝牙服务。
使用listen() 开始监听传入连接。等到建立新连接时发出newConnection() 信号,然后调用nextPendingConnection() 获取新连接的QBluetoothSocket 。
为使其他设备能找到您的服务,请为您的服务创建一个具有适用属性的QBluetoothServiceInfo ,并使用QBluetoothServiceInfo::registerService() 进行注册。调用serverPort() 获取正在使用的频道号。
如果某个平台不支持QBluetoothServiceInfo::Protocol ,listen() 将返回false
。例如,Android 和 WinRT 只支持 RFCOMM。
在 iOS 上,该类不能使用,因为该平台没有公开允许访问 QBluetoothServer 相关功能的 API。
另请参阅 QBluetoothServiceInfo 和QBluetoothSocket 。
成员类型文档
enum QBluetoothServer::Error
该枚举描述了蓝牙服务器错误类型。
常量 | 值 | 说明 |
---|---|---|
QBluetoothServer::NoError | 0 | 无错误。 |
QBluetoothServer::UnknownError | 1 | 发生未知错误。 |
QBluetoothServer::PoweredOffError | 2 | 蓝牙适配器电源已关闭。 |
QBluetoothServer::InputOutputError | 3 | 发生输入输出错误。 |
QBluetoothServer::ServiceAlreadyRegisteredError | 4 | 服务或端口已注册 |
QBluetoothServer::UnsupportedProtocolError | 5 | 该平台不支持Protocol 。 |
QBluetoothServer::MissingPermissionsError (since Qt 6.4) | 6 | 操作系统请求用户未授予的权限。 |
成员函数文档
[explicit]
QBluetoothServer::QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject *parent = nullptr)
通过parent 和serverType 构建蓝牙服务器。
[virtual noexcept]
QBluetoothServer::~QBluetoothServer()
销毁蓝牙服务器。
void QBluetoothServer::close()
关闭并重置监听套接字。任何已建立的QBluetoothSocket 将继续运行,必须单独closed 。
QBluetoothServer::Error QBluetoothServer::error() const
返回QBluetoothServer 中的最后一个错误信息。
[signal, since 6.2]
void QBluetoothServer::errorOccurred(QBluetoothServer::Error error)
error 时发出该信号。
该函数在 Qt 6.2 中引入。
另请参阅 error() 和QBluetoothServer::Error 。
bool QBluetoothServer::hasPendingConnections() const
如果连接待处理,则返回 true,否则返回 false。
bool QBluetoothServer::isListening() const
如果服务器正在监听传入连接,则返回 true,否则返回 false。
bool QBluetoothServer::listen(const QBluetoothAddress &address = QBluetoothAddress(), quint16 port = 0)
开始监听port 上address 的传入连接。address 必须是本地蓝牙适配器地址,port 必须大于 0 且未被其他蓝牙服务器对象占用。建议避免设置端口号,以便系统自动选择端口。
如果操作成功且服务器正在监听传入连接,则返回true
,否则返回false
。
如果服务器对象已在监听传入连接,该函数将始终返回false
。调用该函数前应调用close() 。
另请参阅 isListening() 和newConnection()。
QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName = QString())
向uuid 和serviceName 注册 SPP 服务的方便函数。由于该函数已经注册了服务,因此不能再更改返回的QBluetoothServiceInfo 对象。以后要关闭服务器,需要在该服务器对象上调用QBluetoothServiceInfo::unregisterService() 和close() 。
如果成功,则返回已注册的QBluetoothServiceInfo 实例,否则返回无效的QBluetoothServiceInfo 。该函数始终假定应使用默认的蓝牙适配器。
如果服务器对象已在监听传入连接,该函数将返回无效的QBluetoothServiceInfo 。
对于 RFCOMM 服务器,该函数相当于以下代码片段。
QBluetoothServiceInfo serviceInfo; serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, serviceName); QBluetoothServiceInfo::Sequence browseSequence; browseSequence << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::PublicBrowseGroup)); serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, browseSequence); QBluetoothServiceInfo::Sequence profileSequence; QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort)); classId << QVariant::fromValue(quint16(0x100)); profileSequence.append(QVariant::fromValue(classId)); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, profileSequence); classId.clear(); //Android requires custom uuid to be set as service class classId << QVariant::fromValue(uuid); classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); serviceInfo.setServiceUuid(uuid); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::L2cap)); if (d->serverType == QBluetoothServiceInfo::L2capProtocol) protocol << QVariant::fromValue(serverPort()); protocolDescriptorList.append(QVariant::fromValue(protocol)); protocol.clear(); protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::Rfcomm)) << QVariant::fromValue(quint8(serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); bool result = serviceInfo.registerService();
另请参阅 isListening()、newConnection() 和listen()。
int QBluetoothServer::maxPendingConnections() const
返回待处理连接的最大数量。
另请参阅 setMaxPendingConnections()。
[signal]
void QBluetoothServer::newConnection()
当有新连接可用时,就会发出该信号。
连接槽应调用nextPendingConnection() 获取QBluetoothSocket 对象,以便通过连接收发数据。
另请参阅 nextPendingConnection() 和hasPendingConnections()。
QBluetoothSocket *QBluetoothServer::nextPendingConnection()
返回下一个待处理连接的QBluetoothSocket 指针。调用者有责任删除该指针。
QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const
返回蓝牙安全标记。
另请参阅 setSecurityFlags().
QBluetoothAddress QBluetoothServer::serverAddress() const
返回服务器地址。
quint16 QBluetoothServer::serverPort() const
返回服务器端口号。
QBluetoothServiceInfo::Protocol QBluetoothServer::serverType() const
返回QBluetoothServer 的类型。
void QBluetoothServer::setMaxPendingConnections(int numConnections)
将待处理连接的最大数量设置为numConnections 。如果待处理套接字的数量超过此限制,新的套接字将被拒绝。
另请参阅 maxPendingConnections() 。
void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
将蓝牙安全标记设置为security 。必须在调用listen() 之前调用此函数。在使用蓝牙 2.1 设备时,蓝牙链接将始终加密,因为加密是强制性的。
Android 只支持两种安全级别(安全和非安全)。如果该标志设置为QBluetooth::Security::NoSecurity ,服务器对象将不采用任何身份验证或加密。任何其他安全标记组合都将触发安全蓝牙连接。
在 macOS 上,不支持安全标记并将被忽略。
另请参阅 securityFlags()。
© 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.