이 페이지에서

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)

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 를 반환합니다. 이 함수는 항상 기본 Bluetooth 어댑터를 사용해야 한다고 가정합니다.

서버 객체가 이미 수신 연결을 수신 대기 중인 경우, 이 함수는 유효하지 않은 ` 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 로 설정되면 서버 객체는 어떠한 인증이나 암호화도 적용하지 않습니다. 그 외의 모든 보안 플래그 조합은 보안 Bluetooth 연결을 활성화합니다.

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.