📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

 
(同じ利用者による、間の4版が非表示)
144行目: 144行目:
* AD Data (可変長)
* AD Data (可変長)
<br>
<br>
[[ファイル:Qt BLE 3.png|フレームなし|中央]]
<br>
==== その他 (通信特性 / セキュリティ等) ====
==== その他 (通信特性 / セキュリティ等) ====
* 通信特性
* 通信特性
526行目: 529行目:
<br>
<br>
==== 使用例 ====
==== 使用例 ====
===== QLowEnergyControllerクラスのインスタンスの生成 =====
まず、<code>QBluetoothDeviceDiscoveryAgent</code>クラスを使用して、スキャンを実行する。<br>
<br>
次に、接続するデバイスの<code>QBluetoothDeviceInfo</code>クラスのデバイス情報を取得する。<br>
取得したデバイス情報を元に、<code>QLowEnergyController</code>クラスのインスタンスを生成する。<br>
<br>
===== コントローラのシグナル / スロット接続 =====
最低限必要なシグナルを示す。<br>
* connected
*: デバイスへの接続完了を通知
* disconnected
*: デバイスとの切断を通知 
* serviceDiscovered
*: 新しいサービスの発見を通知
* discoveryFinished
*: サービス探索の完了を通知
<br>
===== BLEデバイスの接続 =====
connectToDeviceメソッドを実行してBLEデバイスに接続する。<br>
connectedシグナルの受信を待つ。<br>
<br>
===== サービスの探索 =====
BLEデバイスへ接続後、discoverServicesメソッドを実行してサービスの探索を開始する。<br>
<br>
* serviceDiscoveredシグナルで個々のサービスが見つかる度に通知される。
* discoveryFinishedシグナルで探索完了が通知される。
<br>
===== サービスの取得 =====
createServiceObjectメソッドをを実行して、探索で発見したサービスのQLowEnergyServiceオブジェクトを生成する。<br>
このオブジェクトを使用して、特性 (Characteristic) や ディスクリプタにアクセスできる。<br>
<br>
===== 組み合わせ =====
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  // BLEのサービス探索を管理するクラス
  // BLEのサービス探索を管理するクラス
626行目: 661行目:
     {
     {
       // タイムアウトタイマの初期化
       // タイムアウトタイマの初期化
       discoveryTimeout = std::make_unique<QTimer>(this);
       discoveryTimeout.setInterval(10000);  // 10秒のタイムアウト
      discoveryTimeout->setInterval(10000);  // 10秒のタイムアウト
       discoveryTimeout.setSingleShot(true);
       discoveryTimeout->setSingleShot(true);
   
   
       connect(discoveryTimeout.get(), &QTimer::timeout, this, &BLEServiceDiscovery::onDiscoveryTimeout);
       connect(&discoveryTimeout, &QTimer::timeout, this, &BLEServiceDiscovery::onDiscoveryTimeout);
     }
     }
   
   
641行目: 675行目:
   
   
           // コントローラの初期化
           // コントローラの初期化
          controller = std::make_unique<QLowEnergyController>(device, this);
           connectControllerSignals();
           connectControllerSignals();
   
   
           // 接続開始
           // 接続開始
           controller->connectToDevice();
           controller.connectToDevice();
           discoveryTimeout->start();
           discoveryTimeout.start();
       }
       }
       catch (const std::exception &e) {
       catch (const std::exception &e) {
660行目: 693行目:
       try {
       try {
           if (controller) {
           if (controller) {
             controller->disconnectFromDevice();
             controller.disconnectFromDevice();
             discoveryTimeout->stop();
             discoveryTimeout.stop();
           }
           }
       }
       }
682行目: 715行目:
     {
     {
       qDebug() << "デバイスに接続";
       qDebug() << "デバイスに接続";
       discoveryTimeout->stop();
       discoveryTimeout.stop();
   
   
       // サービス探索の開始
       // サービス探索の開始
       controller->discoverServices();
       controller.discoverServices();
     }
     }
   
   
692行目: 725行目:
     {
     {
       qDebug() << "デバイスから切断";
       qDebug() << "デバイスから切断";
       discoveryTimeout->stop();
       discoveryTimeout.stop();
     }
     }
   
   
701行目: 734行目:
   
   
       // サービスオブジェクトの作成
       // サービスオブジェクトの作成
       QLowEnergyService* service = controller->createServiceObject(uuid, this);
       QLowEnergyService* service = controller.createServiceObject(uuid, this);
       if (service) {
       if (service) {
           connectServiceSignals(service);
           connectServiceSignals(service);
712行目: 745行目:
     {
     {
       qDebug() << "サービス探索が完了";
       qDebug() << "サービス探索が完了";
       discoveryTimeout->stop();
       discoveryTimeout.stop();
       emit discoveryComplete();
       emit discoveryComplete();
     }
     }
1,074行目: 1,107行目:
  #include <QLowEnergyCharacteristic>
  #include <QLowEnergyCharacteristic>
  #include <QTimer>
  #include <QTimer>
#include <memory>
  #include <QDebug>
  #include <QDebug>
   
   
1,082行目: 1,114行目:
   
   
  private:
  private:
     std::unique_ptr<QLowEnergyController> controller;
     QLowEnergyController controller;
     std::unique_ptr<QTimer> reconnectTimer;
     QTimer               reconnectTimer;
     QBluetoothDeviceInfo currentDevice;
     QBluetoothDeviceInfo currentDevice;
     bool autoReconnect = false;
     bool autoReconnect = false;
1,089行目: 1,121行目:
     void connectControllerSignals()
     void connectControllerSignals()
     {
     {
       connect(controller.get(), &QLowEnergyController::connected, this, &BLEConnection::onConnected);
       connect(&controller, &QLowEnergyController::connected, this, &BLEConnection::onConnected);
       connect(controller.get(), &QLowEnergyController::disconnected, this, &BLEConnection::onDisconnected);
       connect(&controller, &QLowEnergyController::disconnected, this, &BLEConnection::onDisconnected);
       connect(controller.get(), &QLowEnergyController::serviceDiscovered, this, &BLEConnection::onServiceDiscovered);
       connect(&controller, &QLowEnergyController::serviceDiscovered, this, &BLEConnection::onServiceDiscovered);
       connect(controller.get(), static_cast<void(QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error), this, &BLEConnection::onError);
       connect(&controller, static_cast<void(QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error), this, &BLEConnection::onError);
       connect(controller.get(), &QLowEnergyController::stateChanged, this, &BLEConnection::onStateChanged);
       connect(&controller, &QLowEnergyController::stateChanged, this, &BLEConnection::onStateChanged);
     }
     }
   
   
1,204行目: 1,236行目:
     {
     {
       // 再接続タイマの初期化
       // 再接続タイマの初期化
       reconnectTimer = std::make_unique<QTimer>(this);
       reconnectTimer.setInterval(5000);  // 5秒間隔で再接続
      reconnectTimer->setInterval(5000);  // 5秒間隔で再接続
       reconnectTimer.setSingleShot(true);
       reconnectTimer->setSingleShot(true);


       connect(reconnectTimer.get(), &QTimer::timeout, this, &BLEConnection::onReconnectTimeout);
       connect(&reconnectTimer &QTimer::timeout, this, &BLEConnection::onReconnectTimeout);
     }
     }
   
   
1,218行目: 1,249行目:


           // コントローラの初期化
           // コントローラの初期化
          controller = std::make_unique<QLowEnergyController>(device, this);
           connectControllerSignals();
           connectControllerSignals();


           // 接続パラメータの設定
           // 接続パラメータの設定
           controller->setRemoteAddressType(QLowEnergyController::PublicAddress);
           controller.setRemoteAddressType(QLowEnergyController::PublicAddress);


           // 接続開始
           // 接続開始
           controller->connectToDevice();
           controller.connectToDevice();
           currentDevice = device;
           currentDevice = device;
       }
       }
1,240行目: 1,270行目:
       try {
       try {
           if (controller) {
           if (controller) {
             controller->disconnectFromDevice();
             controller.disconnectFromDevice();
             reconnectTimer->stop();
             reconnectTimer.stop();
           }
           }
       }
       }
1,256行目: 1,286行目:
       autoReconnect = enable;
       autoReconnect = enable;
       if (!enable) {
       if (!enable) {
           reconnectTimer->stop();
           reconnectTimer.stop();
       }
       }
     }
     }
1,271行目: 1,301行目:
     {
     {
       qDebug() << "デバイスに接続";
       qDebug() << "デバイスに接続";
       reconnectTimer->stop();
       reconnectTimer.stop();
       emit connected();
       emit connected();
   
   
       // サービスの探索を開始
       // サービスの探索を開始
       controller->discoverServices();
       controller.discoverServices();
     }
     }
   
   
1,286行目: 1,316行目:
       if (autoReconnect) {
       if (autoReconnect) {
           qDebug() << "再接続を試行...";
           qDebug() << "再接続を試行...";
           reconnectTimer->start();
           reconnectTimer.start();
       }
       }
     }
     }
1,294行目: 1,324行目:
       qDebug() << "サービスを発見: " << uuid.toString();
       qDebug() << "サービスを発見: " << uuid.toString();
   
   
       QLowEnergyService* service = controller->createServiceObject(uuid, this);
       QLowEnergyService* service = controller.createServiceObject(uuid, this);
       if (service) {
       if (service) {
           connectServiceSignals(service);
           connectServiceSignals(service);
1,307行目: 1,337行目:
       emit errorOccurred(errorMessage);
       emit errorOccurred(errorMessage);
   
   
       if (autoReconnect && error != QLowEnergyController::InvalidBluetoothAdapterError) reconnectTimer->start();
       if (autoReconnect && error != QLowEnergyController::InvalidBluetoothAdapterError) reconnectTimer.start();
     }
     }


1,320行目: 1,350行目:
       if (autoReconnect && currentDevice.isValid()) {
       if (autoReconnect && currentDevice.isValid()) {
           qDebug() << "再接続を試行...";
           qDebug() << "再接続を試行...";
           controller->connectToDevice();
           controller.connectToDevice();
       }
       }
     }
     }