「Qtの基礎 - Classic Bluetooth」の版間の差分

ナビゲーションに移動 検索に移動
127行目: 127行目:
また、スキャン時間や範囲を設定可能であり、バッテリー消費とスキャン精度のバランスを取ることができる。<br>
また、スキャン時間や範囲を設定可能であり、バッテリー消費とスキャン精度のバランスを取ることができる。<br>
<br>
<br>
Classic Bluetoothでは、<code>QBluetoothDeviceDiscoveryAgent</code>を使用して、全てのBluetoothデバイスを検出する。<br>
Bluetooth Classicでは、<code>QBluetoothDeviceDiscoveryAgent</code>を使用して、全てのBluetoothデバイスを検出する。<br>
<br>
==== Bluetoothアダプタの初期化 / デバイス検出用エージェントの生成 ====
Bluetoothアダプタを初期化する。<br>
デバイス検出用エージェントの生成する。<br>
<br>
また、BLEデバイスは検出対象から除外する。<br>
<br>
<br>
<syntaxhighlight lang="c++">
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
// Bluetoothアダプタの初期化とデバイス検出エージェントの生成
QBluetoothDeviceDiscoveryAgent discoveryAgent;
// Bluetooth Classicのみをスキャン (BLEデバイスを除外)
discoveryAgent.setDiscoveryModes(QBluetoothDeviceDiscoveryAgent::ClassicMethod);
// 検出時にもBLEデバイスをフィルタリング
connect(&discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, [](const QBluetoothDeviceInfo &device) {
    // BLEデバイスの場合
    if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
      return;
    }
    // Bluetooth Classicデバイスの場合
    // ...略
});
</syntaxhighlight>
<br>
==== デバイス検出イベントの設定 ====
デバイスが見つかった時のイベントする。<br>
スキャン完了時のイベント処理を設定する。<br>
エラー発生時のイベント処理を設定する。<br>
<br>
<syntaxhighlight lang="c++">
// デバイス発見時のイベント
connect(&discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, [](const QBluetoothDeviceInfo &device) {
    // デバイス発見時の処理
});
// スキャン完了時のイベント
connect(&discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, []() {
    // スキャン完了時の処理
});
// エラー発生時のイベント
connect(&discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, [](QBluetoothDeviceDiscoveryAgent::Error error) {
    // エラー発生時の処理
});
</syntaxhighlight>
<br>
==== スキャンの開始 ====
スキャン中の場合は停止する。<br>
新しいスキャンを開始する。<br>
<br>
<syntaxhighlight lang="c++">
// アクティブなスキャンがある場合は停止
if (discoveryAgent.isActive()) {
    discoveryAgent.stop();
}
// スキャン開始
discoveryAgent.start();
</syntaxhighlight>
<br>
==== デバイス検出時 ====
検出したデバイスがBLEかどうかを確認する。
Bluetooth Classicの場合は、デバイス名、MACアドレスを取得する。
<br>
<syntaxhighlight lang="c++">
void handleDeviceDiscovered(const QBluetoothDeviceInfo &device)
{
    // BLEデバイスをスキップ
    if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
      return;
    }
    // デバイス情報の取得
    QString name    = device.name();
    QString address = device.address().toString();
    qDebug() << "Found device : " << name << address;
}
</syntaxhighlight>
<br>
==== スキャン完了時 ====
スキャンの完了を通知する。<br>
必要に応じてスキャンを再開または終了する。<br>
<br>
<syntaxhighlight lang="c++">
void handleScanFinished()
{
    qDebug() << "スキャン完了";
    // 必要に応じてスキャンを再開
    // discoveryAgent.start();
}
</syntaxhighlight>
<br>
==== エラー発生時 ====
Bluetooth Classicでは、以下に示すようなエラー処理がある。<br>
* Bluetoothの電源が入っていない。
* Bluetoothアダプタが見つからない。
* 入出力エラー
* その他のエラー
<br>
<syntaxhighlight lang="c++">
void handleError(QBluetoothDeviceDiscoveryAgent::Error error)
{
    switch (error) {
      case QBluetoothDeviceDiscoveryAgent::PoweredOffError:
          qDebug() << "Bluetoothの電源が入っていません";
          break;
      case QBluetoothDeviceDiscoveryAgent::InputOutputError:
          qDebug() << "I/Oエラー";
          break;
      case QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError:
          qDebug() << "Bluetoothアダプタが無効です";
          break;
      default:
          qDebug() << "不明なエラー";
          break;
    }
}
</syntaxhighlight>
<br>
==== 組み合わせ ====
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  #include <QObject>
  #include <QObject>
  #include <QBluetoothDeviceDiscoveryAgent>
  #include <QBluetoothDeviceDiscoveryAgent>
  #include <memory>
  #include <QBluetoothDeviceInfo>
  #include <stdexcept>
  #include <stdexcept>
  #include <QDebug>
  #include <QDebug>
161行目: 287行目:
     }
     }
   
   
     // エラーコードを文字列に変換するヘルパー関数
     // エラーコード
     QString errorToString(QBluetoothDeviceDiscoveryAgent::Error error)
     QString errorToString(QBluetoothDeviceDiscoveryAgent::Error error)
     {
     {
176行目: 302行目:
   
   
  public:
  public:
     explicit BluetoothScanner(QObject* parent = nullptr) : QObject(parent)
     explicit BluetoothScanner(QObject *parent = nullptr) : QObject(parent)
     {
     {
       try {
       try {
212行目: 338行目:
       try {
       try {
           qDebug() << "デバイススキャンを停止...";
           qDebug() << "デバイススキャンを停止...";
           discoveryAgent->stop();
           discoveryAgent.stop();
       }
       }
       catch (const std::exception &e) {
       catch (const std::exception &e) {
222行目: 348行目:
  private slots:
  private slots:
     // デバイスが存在する場合のスロット
     // デバイスが存在する場合のスロット
     void onDeviceDiscovered(const QBluetoothDeviceInfo& device)
     void onDeviceDiscovered(const QBluetoothDeviceInfo &device)
     {
     {
       qDebug() << "デバイスの探索に成功:";
       qDebug() << "デバイスの探索に成功:";
239行目: 365行目:
     // エラー発生時のスロット
     // エラー発生時のスロット
     void onError(QBluetoothDeviceDiscoveryAgent::Error error)
     void onError(QBluetoothDeviceDiscoveryAgent::Error error)
    {
    {
         qDebug() << "エラーが発生: " << errorToString(error);
         qDebug() << "エラーが発生: " << errorToString(error);
     }
     }

案内メニュー