本页内容

QBluetoothServer Class

QBluetoothServer 类使用 RFCOMM 或 L2cap 协议与蓝牙设备进行通信。更多内容...

头文件: #include <QBluetoothServer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Bluetooth)
target_link_libraries(mytarget PRIVATE Qt6::Bluetooth)
qmake: QT += bluetooth
继承自: 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::NoError0无错误。
QBluetoothServer::UnknownError1发生未知错误。
QBluetoothServer::PoweredOffError2蓝牙适配器已关闭。
QBluetoothServer::InputOutputError3发生了一个输入/输出错误。
QBluetoothServer::ServiceAlreadyRegisteredError4该服务或端口已注册
QBluetoothServer::UnsupportedProtocolError5此平台不支持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)

开始监听发送到address 的传入连接,地址为port 。address 必须是本地蓝牙适配器的地址,port 必须大于零,且未被其他蓝牙服务器对象占用。建议不要设置端口号,以便系统自动选择端口。

如果操作成功且服务器正在监听传入连接,则返回true ;否则返回false 。

如果服务器对象已经在监听传入连接,则该函数始终返回 `false`。在调用此函数之前,应先调用 `close()`。

另请参阅 isListening() 和newConnection()。

QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName = QString())

用于将 SPP 服务注册到uuid 和serviceName 的便捷函数。由于该函数已将服务注册完毕,因此返回的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 设备时,蓝牙连接将始终处于加密状态,因为加密是强制要求的。

在 Linux BlueZ 后端中,这些标志映射到内核安全级别:

默认值为QBluetooth::Security::Authorization 。

Android 仅支持两级安全级别(安全与非安全)。如果将此标志设置为QBluetooth::Security::NoSecurity ,服务器对象将不会采用任何身份验证或加密措施。任何其他安全标志组合都将触发安全的蓝牙连接。

在 macOS 和 Windows 上,不支持安全标志,且会被忽略。

另请参阅 securityFlags()。

© 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.