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 で設定することはできません。サービスのServiceClassIdsProtocolDescriptorList は自動的に入力されます。そのため、これらのフィールドにカスタム値を指定してサービスを登録すると、Windows では期待した結果が得られない場合があります。

注意: RSSI(Received Signal Strength Indicator)とBluetooth LEデバイスによってアドバタイズされるManufacturer Specific DataはWin32バックエンドではサポートされていません。また、Windowsの設定を通してのみ、以前にペアリングされたデバイスを見つけることができます。

以下のセクションでは、上記のユースケースに対してQt Bluetooth C++ API クラスを使用する方法について説明します。

ローカル・デバイス情報の取得

Qt Bluetooth API には 3 つの主な目的があります。最初の目的は、ローカルおよびリモートのデバイス情報を取得することです。デバイス情報を取得する最初のステップは、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デバイス){
    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";

    コネクト(ソケット, &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つのアプリケーション・インスタンス間の状態の同期に最適です。サーバーとクライアントの設定方法の詳細については、QBluetoothServerQBluetoothSocket クラスの詳細説明セクションを参照してください。SPPを使い始めるのに良い例は、Bluetooth Chatの例です。

Bluetooth Low Energy

Bluetooth Smartとしても知られるBluetooth Low Energyは、エネルギー消費の少ないデバイス同士の通信を可能にする新しい技術です。この技術と関連する Qt API の詳細については、Bluetooth Low Energy Overview を参照してください。

© 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.