Qt Bluetooth 概要
Qt Bluetooth API の代表的なユースケースは以下の通りです:
- ローカルのBluetoothデバイスに関する情報を取得する。
- 通信範囲内にある他のBluetoothデバイスをスキャンし、それらの情報を取得する。
- OBEXオブジェクトプッシュプロファイル(OPP)を使用して、ファイルをリモートデバイスにプッシュする。
- シリアルポートプロファイル(SPP)を使用して、RFCOMMチャネル経由でリモートデバイスに接続する。
- SPP を使用して着信接続を許可する RFCOMM サーバーを作成します。
- Bluetooth Low Energy デバイスに関する仕様を取得します。
- Bluetooth Low Energy デバイスに接続します。
- Bluetooth Low Energy デバイスからのアドバタイズメントを受信します。
なお、Object Push Profile は Android および Windows ではサポートされていません。
注: Windows では、RFCOMM機能の一部を Qt を使用して設定することはできません。サービスの「ServiceClassIds 」および「ProtocolDescriptorList 」は自動的に設定されます。そのため、これらのフィールドにカスタム値を設定してサービスを登録しても、Windows では期待通りの結果が得られない場合があります。
注: 受信信号強度インジケータ (RSSI)およびBluetooth LEデバイスによってアドバタイズされるメーカー固有データは、Win32バックエンドではサポートされていません。また、Windowsの設定を通じて事前にペアリング済みのデバイスしか検出できません。
以下のセクションでは、上記のユースケースにおいてQt Bluetooth のC++ APIクラスをどのように使用するかを説明します。
ローカルデバイスの情報の取得
Qt Bluetooth API には、主に 3 つの目的があります。1 つ目は、ローカルおよびリモートのデバイス情報を取得することです。 デバイス情報を取得する最初のステップは、デバイスでBluetoothが利用可能かどうかを確認し、ローカルデバイスのアドレスと名前を読み取ることです。QBluetoothLocalDevice クラスは、これらすべての情報を提供します。さらに、このクラスを使用して、Bluetoothのオン/オフの切り替え、デバイスの可視性の設定、および現在の接続状況の確認を行うこともできます。
QBluetoothLocalDevice localDevice;
QString localDeviceName;
// Check if Bluetooth is available on this device
if (localDevice.isValid()) {
// Turn Bluetooth on
localDevice.powerOn();
// Read local device name
localDeviceName = localDevice.name();
// Make it visible to others
localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
// Get connected devices
QList<QBluetoothAddress> remotes;
remotes = localDevice.connectedDevices();
}Bluetooth デバイスのスキャン
QBluetoothLocalDevice と同様に、このAPIには、リモートデバイスに関する同様の情報を提供するQBluetoothDeviceInfo が用意されています。QBluetoothDeviceInfo オブジェクトを独自に作成してデータを設定することも可能ですが、より簡単な方法は、QBluetoothDeviceDiscoveryAgent を使用して、接続可能な範囲内にある検出可能なBluetoothデバイスの自動検索を開始することです。
voidMyClass::startDeviceDiscovery()
{
// ディスカバリーエージェントを作成し、そのシグナルに接続する
QBluetoothDeviceDiscoveryAgent*discoveryAgent = newQBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this,SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
// 検出を開始
discoveryAgent->start();
//...
}
// ローカルスロット内で、検出されたデバイスに関する情報を読み取る
voidMyClass::deviceDiscovered(constQBluetoothDeviceInfo&device)
{
qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
}デバイス間のデータ交換
2つのBluetooth対応デバイス間の通信において、より柔軟なアプローチは、仮想シリアルポート接続を確立し、その接続を介してデータを自由にやり取りすることです。これは、シリアルポートプロファイル(SPP)によって実現できます。シリアルポートプロファイルは、BluetoothトランスポートプロトコルであるRFCOMMを介してシリアル接続をエミュレートします。
着信するSPP接続を受け付けるには、QBluetoothServer を使用して着信接続をリッスンする必要があります。
rfcommServer= newQBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
connect(rfcommServer, &QBluetoothServer::newConnection,
this, QOverload<>::of(&ChatServer::clientConnected));
boolresult= rfcommServer->listen(localAdapter);
if(!result) {
qWarning() << "Cannot bind chat server to" << localAdapter.toString();
return;
}クライアントの役割を果たす別のデバイスから、QBluetoothSocket を使用してこのサーバーに接続します:
voidChatClient::startClient(constQBluetoothServiceInfo&remoteService)
{
if(socket)
return;
// サービスに接続
socket= newQBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
qDebug() << "Create socket";
socket->connectToService(remoteService);
qDebug() << "ConnectToService done";
connect(socket, &QBluetoothSocket::readyRead, this, &ChatClient::readSocket);
connect(socket, &QBluetoothSocket::connected, this, QOverload<>::of(&ChatClient::connected));
connect(socket, &QBluetoothSocket::disconnected, this, &ChatClient::disconnected);
connect(socket, &QBluetoothSocket::errorOccurred, this, &ChatClient::onSocketErrorOccurred);
}このような接続を使用することで、あらゆる形式のデータを双方向にやり取りすることができます。これは、ゲームや、2つのデバイス上のアプリケーションの2つのインスタンス間で状態を同期させる場合に最適です。 サーバーとクライアントの設定方法に関するより詳細な説明については、QBluetoothServer クラスおよびQBluetoothSocket クラスの詳細説明セクションを参照してください。SPP を始めるのに適した例としては、Bluetooth Chatのサンプルがあります。
Bluetooth Low Energy
Bluetooth Low Energy(Bluetooth Smartとも呼ばれる)は、低消費電力のデバイス同士が通信できるようにする新しい技術です。この技術および関連する Qt API に関する詳細は、「Bluetooth Low Energy の概要」をご覧ください。
© 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.