QBluetoothServer Class
La classe QBluetoothServer utilise le protocole RFCOMM ou L2cap pour communiquer avec un périphérique Bluetooth. En savoir plus...
| En-tête : | #include <QBluetoothServer> |
| CMake : | find_package(Qt6 REQUIRED COMPONENTS Bluetooth) target_link_libraries(mytarget PRIVATE Qt6::Bluetooth) |
| qmake : | QT += bluetooth |
| Hérite de : | QObject |
Types publics
| enum | Error { NoError, UnknownError, PoweredOffError, InputOutputError, ServiceAlreadyRegisteredError, …, MissingPermissionsError } |
Fonctions publiques
| 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) |
Signaux
(since 6.2) void | errorOccurred(QBluetoothServer::Error error) |
| void | newConnection() |
Description détaillée
QBluetoothServer est utilisé pour implémenter des services Bluetooth via RFCOMM ou L2cap.
Commencez à écouter les connexions entrantes avec ` listen()`. Attendez que le signal ` newConnection()` soit émis lorsqu’une nouvelle connexion est établie, puis appelez ` nextPendingConnection()` pour obtenir un ` QBluetoothSocket ` pour cette nouvelle connexion.
Pour permettre à d’autres appareils de trouver votre service, créez une QBluetoothServiceInfo avec les attributs applicables à votre service et enregistrez-la à l’aide de QBluetoothServiceInfo::registerService(). Appelez serverPort() pour obtenir le numéro de canal utilisé.
Si l'QBluetoothServiceInfo::Protocol n'est pas prise en charge par une plateforme, la méthode listen() renverra false. Android et WinRT ne prennent en charge que RFCOMM, par exemple.
Sous iOS, cette classe ne peut pas être utilisée car la plateforme n’expose pas d’API permettant d’accéder aux fonctionnalités liées à QBluetoothServer.
Voir également QBluetoothServiceInfo et QBluetoothSocket.
Documentation sur les types de membres
enum QBluetoothServer::Error
Cette énumération décrit les types d'erreurs du serveur Bluetooth.
| Constante | Valeur | Description |
|---|---|---|
QBluetoothServer::NoError | 0 | Aucune erreur. |
QBluetoothServer::UnknownError | 1 | Une erreur inconnue s'est produite. |
QBluetoothServer::PoweredOffError | 2 | L'adaptateur Bluetooth est hors tension. |
QBluetoothServer::InputOutputError | 3 | Une erreur d'entrée-sortie s'est produite. |
QBluetoothServer::ServiceAlreadyRegisteredError | 4 | Le service ou le port était déjà enregistré |
QBluetoothServer::UnsupportedProtocolError | 5 | L'Protocol n'est pas prise en charge sur cette plateforme. |
QBluetoothServer::MissingPermissionsError (since Qt 6.4) | 6 | Le système d'exploitation demande des autorisations qui n'ont pas été accordées par l'utilisateur. |
Documentation sur les fonctions membres
[explicit] QBluetoothServer::QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject *parent = nullptr)
Crée un serveur Bluetooth à l'aide de parent et serverType.
[virtual noexcept] QBluetoothServer::~QBluetoothServer()
Supprime le serveur Bluetooth.
void QBluetoothServer::close()
Ferme et réinitialise le socket d'écoute. Toute connexion déjà établie ( QBluetoothSocket ) continue de fonctionner et doit être fermée séparément ( closed).
QBluetoothServer::Error QBluetoothServer::error() const
Renvoie la dernière erreur de l'QBluetoothServer.
[signal, since 6.2] void QBluetoothServer::errorOccurred(QBluetoothServer::Error error)
Ce signal est émis lorsqu'une « error » se produit.
Cette fonction a été introduite dans Qt 6.2.
Voir également ` error()` et ` QBluetoothServer::Error`.
bool QBluetoothServer::hasPendingConnections() const
Renvoie « true » si une connexion est en attente, sinon « false ».
bool QBluetoothServer::isListening() const
Renvoie « true » si le serveur est à l'écoute des connexions entrantes, sinon « false ».
bool QBluetoothServer::listen(const QBluetoothAddress &address = QBluetoothAddress(), quint16 port = 0)
Commence à écouter les connexions entrantes vers address sur port. address doit être l'adresse d'un adaptateur Bluetooth local et port doit être supérieur à zéro et ne pas être déjà utilisé par un autre objet serveur Bluetooth. Il est recommandé de ne pas définir de numéro de port afin de permettre au système de choisir automatiquement un port.
Renvoie « true » si l'opération a réussi et que le serveur est à l'écoute des connexions entrantes ; sinon, renvoie « false ».
Si l’objet serveur est déjà à l’écoute des connexions entrantes, cette fonction renvoie toujours « false ». La fonction « close() » doit être appelée avant d’appeler cette fonction.
Voir également isListening() et newConnection().
QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName = QString())
Fonction pratique permettant d’enregistrer un service SPP auprès de uuid et serviceName. Étant donné que cette fonction enregistre déjà le service, l’objet QBluetoothServiceInfo renvoyé ne peut plus être modifié. Pour arrêter le serveur ultérieurement, il est nécessaire d’appeler les méthodes QBluetoothServiceInfo::unregisterService() et close() sur cet objet serveur.
Renvoie une instance QBluetoothServiceInfo enregistrée en cas de succès, sinon un QBluetoothServiceInfo invalide. Cette fonction part toujours du principe que l'adaptateur Bluetooth par défaut doit être utilisé.
Si l'objet serveur est déjà à l'écoute des connexions entrantes, cette fonction renvoie une valeur QBluetoothServiceInfo non valide.
Pour un serveur RFCOMM, cette fonction équivaut à l’extrait de code suivant.
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();Voir également isListening(), newConnection() et listen().
int QBluetoothServer::maxPendingConnections() const
Renvoie le nombre maximal de connexions en attente.
Voir aussi setMaxPendingConnections().
[signal] void QBluetoothServer::newConnection()
Ce signal est émis lorsqu'une nouvelle connexion est disponible.
Le slot connecté doit appeler la fonction nextPendingConnection() afin d’obtenir un objet QBluetoothSocket permettant d’envoyer et de recevoir des données via cette connexion.
Voir également nextPendingConnection() et hasPendingConnections().
QBluetoothSocket *QBluetoothServer::nextPendingConnection()
Renvoie un pointeur vers l'objet ` QBluetoothSocket ` correspondant à la prochaine connexion en attente. Il incombe à l'appelant de supprimer ce pointeur.
QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const
Renvoie les indicateurs de sécurité Bluetooth.
Voir aussi setSecurityFlags().
QBluetoothAddress QBluetoothServer::serverAddress() const
Renvoie l'adresse du serveur.
quint16 QBluetoothServer::serverPort() const
Renvoie le numéro de port du serveur.
QBluetoothServiceInfo::Protocol QBluetoothServer::serverType() const
Renvoie le type de l’ QBluetoothServer.
void QBluetoothServer::setMaxPendingConnections(int numConnections)
Définit le nombre maximal de connexions en attente à numConnections. Si le nombre de sockets en attente dépasse cette limite, les nouveaux sockets seront rejetés.
Voir également maxPendingConnections().
void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
Définit les indicateurs de sécurité Bluetooth sur « security ». Cette fonction doit être appelée avant d'appeler ` listen()`. La liaison Bluetooth sera toujours chiffrée lors de l'utilisation de périphériques Bluetooth 2.1, le chiffrement étant obligatoire.
Sur le backend BlueZ sous Linux, ces indicateurs sont mis en correspondance avec les niveaux de sécurité du noyau :
- QBluetooth::Security::Authorization correspond à «
BT_SECURITY_LOW» ; - QBluetooth::Security::Encryption correspond à
BT_SECURITY_MEDIUM; - QBluetooth::Security::Secure correspond à
BT_SECURITY_HIGH; - QBluetooth::Security::Authentication n’est pas utilisé par le noyau et est ignoré.
La valeur par défaut est « QBluetooth::Security::Authorization ».
Android ne prend en charge que deux niveaux de sécurité (sécurisé et non sécurisé). Si cet indicateur est défini sur QBluetooth::Security::NoSecurity, l’objet serveur n’utilisera ni authentification ni chiffrement. Toute autre combinaison d’indicateurs de sécurité déclenchera une connexion Bluetooth sécurisée.
Sous macOS et Windows, les indicateurs de sécurité ne sont pas pris en charge et seront ignorés.
Voir également 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.