QBluetoothServer Class
QBluetoothServer クラスは、RFCOMM または L2cap プロトコルを使用して Bluetooth デバイスと通信します。詳細...
| ヘッダー: | #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 上で Bluetooth サービスを実装するために使用されます。
listen() を使用して、着信接続のリスニングを開始します。新しい接続が確立されてnewConnection() シグナルが発信されるまで待機し、nextPendingConnection() を呼び出して、新しい接続のQBluetoothSocket を取得します。
他のデバイスがあなたのサービスを見つけられるようにするには、サービスに適した属性を持つQBluetoothServiceInfo を作成し、QBluetoothServiceInfo::registerService()を使用して登録します。serverPort()を呼び出して、使用中のチャネル番号を取得します。
QBluetoothServiceInfo::Protocol がプラットフォームでサポートされていない場合、listen()はfalse を返します。たとえば、AndroidとWinRTではRFCOMMのみがサポートされています。
iOSでは、プラットフォーム側がQBluetoothServer関連の機能へのアクセスを許可するAPIを公開していないため、このクラスを使用することはできません。
QBluetoothServiceInfo およびQBluetoothSocketも参照してください 。
メンバ型のドキュメント
enum QBluetoothServer::Error
この列挙型は、Bluetoothサーバーのエラー種別を定義します。
| 定数 | 値 | 説明 |
|---|---|---|
QBluetoothServer::NoError | 0 | エラーなし。 |
QBluetoothServer::UnknownError | 1 | 不明なエラーが発生しました。 |
QBluetoothServer::PoweredOffError | 2 | Bluetooth アダプタの電源がオフになっています。 |
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 を使用して、Bluetooth サーバーを構築します。
[virtual noexcept] QBluetoothServer::~QBluetoothServer()
Bluetoothサーバーを終了します。
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 はローカルの Bluetooth アダプタのアドレスである必要があり、port は 0 より大きく、かつ他の Bluetooth サーバーオブジェクトによってすでに使用されていない必要があります。システムが自動的にポートを選択できるようにするため、ポート番号の設定は避けることをお勧めします。
操作が成功し、サーバーが着信接続を待機している場合は `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
Bluetoothのセキュリティフラグを返します。
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)
Bluetoothのセキュリティフラグをsecurity に設定します。この関数は、listen()を呼び出す前に呼び出す必要があります。Bluetooth 2.1対応デバイスを使用する場合、暗号化が必須であるため、Bluetoothリンクは常に暗号化されます。
LinuxのBlueZバックエンドでは、これらのフラグはカーネルのセキュリティレベルに次のように対応付けられています:
- QBluetooth::Security::Authorization
BT_SECURITY_LOWにマッピングされます; - QBluetooth::Security::Encryption
BT_SECURITY_MEDIUMにマッピングされます; - QBluetooth::Security::Secure
BT_SECURITY_HIGHに対応します; - QBluetooth::Security::Authentication はカーネルでは使用されず、無視されます。
デフォルト値は `QBluetooth::Security::Authorization` です。
Android は 2 段階のセキュリティレベル(セキュアと非セキュア)のみをサポートしています。このフラグが `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.