QBluetoothServer Class
QBluetoothServer クラスは、RFCOMM または L2cap プロトコルを使用して Bluetooth デバイスと通信します。詳細...
Header: | #include <QBluetoothServer> |
qmake: | QT += bluetooth |
Inherits: | 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 でブルートゥースサーバーを構築する。
[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 はローカルの Bluetooth アダプタ・アドレスで、port は 0 より大きく、他の Bluetooth サーバ・オブジェクトに既に取られていない必要があります。システムが自動的にポートを選択できるように、ポート番号は設定しないことをお勧めします。
操作が成功し、サーバーが着信接続をリッスンしている場合は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 を返します。 この関数は常に、デフォルトの 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 リンクは常に暗号化されます。
Android は 2 つのセキュリティ・レベル(セキュアと非セキュア)しかサポートしていません。このフラグがQBluetooth::Security::NoSecurity に設定されている場合、サーバー・オブジェクトは認証や暗号化を行いません。その他のセキュリティ・フラグの組み合わせは、セキュアなBluetooth接続をトリガーします。
macOSでは、セキュリティ・フラグはサポートされておらず、無視されます。
securityFlags()も参照してください 。
© 2025 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.