In questa pagina

QBluetoothServer Class

La classe QBluetoothServer utilizza il protocollo RFCOMM o L2cap per comunicare con un dispositivo Bluetooth. Per saperne di più...

Intestazione: #include <QBluetoothServer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Bluetooth)
target_link_libraries(mytarget PRIVATE Qt6::Bluetooth)
qmake: QT += bluetooth
Eredita da: QObject

Tipi pubblici

enum Error { NoError, UnknownError, PoweredOffError, InputOutputError, ServiceAlreadyRegisteredError, …, MissingPermissionsError }

Funzioni pubbliche

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)

Segnali

(since 6.2) void errorOccurred(QBluetoothServer::Error error)
void newConnection()

Descrizione dettagliata

QBluetoothServer viene utilizzato per implementare servizi Bluetooth su RFCOMM o L2cap.

Avvia l'ascolto delle connessioni in arrivo con listen(). Attendi che venga emesso il segnale newConnection() quando viene stabilita una nuova connessione, quindi chiama nextPendingConnection() per ottenere un QBluetoothSocket per la nuova connessione.

Per consentire ad altri dispositivi di individuare il proprio servizio, creare un QBluetoothServiceInfo con gli attributi applicabili al proprio servizio e registrarlo utilizzando QBluetoothServiceInfo::registerService(). Chiamare serverPort() per ottenere il numero del canale in uso.

Se l’ QBluetoothServiceInfo::Protocol non è supportato da una piattaforma, listen() restituirà false. Ad esempio, Android e WinRT supportano solo RFCOMM.

Su iOS, questa classe non può essere utilizzata poiché la piattaforma non espone un’API che consenta l’accesso alle funzionalità relative a QBluetoothServer.

Vedi anche QBluetoothServiceInfo e QBluetoothSocket.

Documentazione sui tipi dei membri

enum QBluetoothServer::Error

Questa enumerazione descrive i tipi di errore del server Bluetooth.

CostanteValoreDescrizione
QBluetoothServer::NoError0Nessun errore.
QBluetoothServer::UnknownError1Si è verificato un errore sconosciuto.
QBluetoothServer::PoweredOffError2L'adattatore Bluetooth è spento.
QBluetoothServer::InputOutputError3Si è verificato un errore di input/output.
QBluetoothServer::ServiceAlreadyRegisteredError4Il servizio o la porta era già registrato
QBluetoothServer::UnsupportedProtocolError5Protocol non è supportato su questa piattaforma.
QBluetoothServer::MissingPermissionsError (since Qt 6.4)6Il sistema operativo richiede autorizzazioni che non sono state concesse dall'utente.

Documentazione sulle funzioni membro

[explicit] QBluetoothServer::QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject *parent = nullptr)

Crea un server Bluetooth con parent e serverType.

[virtual noexcept] QBluetoothServer::~QBluetoothServer()

Elimina il server Bluetooth.

void QBluetoothServer::close()

Chiude e reimposta il socket in ascolto. Qualsiasi connessione QBluetoothSocket già stabilita continua a funzionare e deve essere chiusa separatamente tramite il comando ` closed`.

QBluetoothServer::Error QBluetoothServer::error() const

Restituisce l'ultimo errore dell'QBluetoothServer.

[signal, since 6.2] void QBluetoothServer::errorOccurred(QBluetoothServer::Error error)

Questo segnale viene emesso quando si verifica un'error.

Questa funzione è stata introdotta in Qt 6.2.

Vedi anche ` error()` e ` QBluetoothServer::Error`.

bool QBluetoothServer::hasPendingConnections() const

Restituisce true se è presente una connessione in sospeso, altrimenti false.

bool QBluetoothServer::isListening() const

Restituisce "true" se il server è in ascolto delle connessioni in entrata, altrimenti "false".

bool QBluetoothServer::listen(const QBluetoothAddress &address = QBluetoothAddress(), quint16 port = 0)

Avvia l'ascolto delle connessioni in entrata verso address su port. address deve essere l'indirizzo di un adattatore Bluetooth locale e port deve essere maggiore di zero e non deve essere già occupato da un altro oggetto server Bluetooth. Si raccomanda di evitare di impostare un numero di porta per consentire al sistema di sceglierne uno automaticamente.

Restituisce true se l’operazione ha avuto esito positivo e il server è in ascolto delle connessioni in entrata, altrimenti restituisce false.

Se l’oggetto server è già in ascolto delle connessioni in entrata, questa funzione restituisce sempre ` false`. È necessario chiamare ` close()` prima di chiamare questa funzione.

Vedi anche ` isListening()` e ` newConnection()`.

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

Funzione di comodità per la registrazione di un servizio SPP su uuid e serviceName. Poiché questa funzione registra già il servizio, l'oggetto QBluetoothServiceInfo restituito non può più essere modificato. Per arrestare il server in un secondo momento, è necessario chiamare QBluetoothServiceInfo::unregisterService() e close() su questo oggetto server.

Restituisce un'istanza registrata di ` QBluetoothServiceInfo ` in caso di esito positivo, altrimenti un oggetto non valido di tipo ` QBluetoothServiceInfo`. Questa funzione presuppone sempre che venga utilizzato l'adattatore Bluetooth predefinito.

Se l'oggetto server è già in ascolto per le connessioni in entrata, questa funzione restituisce un'istanza QBluetoothServiceInfo non valida.

Per un server RFCOMM questa funzione è equivalente al seguente frammento di codice.

    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();

Vedi anche isListening(), newConnection() e listen().

int QBluetoothServer::maxPendingConnections() const

Restituisce il numero massimo di connessioni in sospeso.

Vedi anche setMaxPendingConnections().

[signal] void QBluetoothServer::newConnection()

Questo segnale viene emesso quando è disponibile una nuova connessione.

Lo slot connesso dovrebbe chiamare nextPendingConnection() per ottenere un oggetto QBluetoothSocket con cui inviare e ricevere dati tramite la connessione.

Vedi anche nextPendingConnection() e hasPendingConnections().

QBluetoothSocket *QBluetoothServer::nextPendingConnection()

Restituisce un puntatore all'oggetto ` QBluetoothSocket ` relativo alla prossima connessione in sospeso. È responsabilità del chiamante eliminare il puntatore.

QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const

Restituisce i flag di sicurezza Bluetooth.

Vedi anche setSecurityFlags().

QBluetoothAddress QBluetoothServer::serverAddress() const

Restituisce l'indirizzo del server.

quint16 QBluetoothServer::serverPort() const

Restituisce il numero di porta del server.

QBluetoothServiceInfo::Protocol QBluetoothServer::serverType() const

Restituisce il tipo dell'QBluetoothServer.

void QBluetoothServer::setMaxPendingConnections(int numConnections)

Imposta il numero massimo di connessioni in sospeso su numConnections. Se il numero di socket in sospeso supera questo limite, i nuovi socket verranno rifiutati.

Vedi anche maxPendingConnections().

void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)

Imposta i flag di sicurezza Bluetooth su security. Questa funzione deve essere chiamata prima di chiamare listen(). Il collegamento Bluetooth sarà sempre crittografato quando si utilizzano dispositivi Bluetooth 2.1, poiché la crittografia è obbligatoria.

Sul backend BlueZ di Linux, i flag sono mappati ai livelli di sicurezza del kernel:

Il valore predefinito è ` QBluetooth::Security::Authorization`.

Android supporta solo due livelli di sicurezza (sicuro e non sicuro). Se questo flag è impostato su QBluetooth::Security::NoSecurity, l'oggetto server non utilizzerà alcuna autenticazione o crittografia. Qualsiasi altra combinazione di flag di sicurezza attiverà una connessione Bluetooth sicura.

Su macOS e Windows, i flag di sicurezza non sono supportati e verranno ignorati.

Vedi anche ` 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.