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

ナビゲーションに移動 検索に移動
(ページの作成:「== 概要 == QtのBluetoothサポートは、Qt Bluetoothモジュールを通じて提供されている。<br> Qt Bluetoothモジュールは、クロスプラットフォームなBluetooth通信機能を実現するための包括的なAPIセットとなっている。<br> <br> 主要なコンポーネントとして、Classic BluetoothとBluetooth Low Energy (BLE) の両方をサポートしている。 * Classic Bluetooth *: 従来型の高帯域幅通信 * BLE…」)
 
47行目: 47行目:
スキャン中は、各デバイスの基本情報 (デバイス名、MACアドレス、デバイスクラス、信号強度等) を取得できる。<br>
スキャン中は、各デバイスの基本情報 (デバイス名、MACアドレス、デバイスクラス、信号強度等) を取得できる。<br>
また、スキャン時間や範囲を設定可能であり、バッテリー消費とスキャン精度のバランスを取ることができる。<br>
また、スキャン時間や範囲を設定可能であり、バッテリー消費とスキャン精度のバランスを取ることができる。<br>
<br>
<syntaxhighlight lang="c++">
#include <QObject>
#include <QBluetoothDeviceDiscoveryAgent>
#include <memory>
#include <stdexcept>
#include <QDebug>
class BluetoothScanner : public QObject
{
    Q_OBJECT
private:
    std::unique_ptr<QBluetoothDeviceDiscoveryAgent> discoveryAgent;
    // デバイスタイプを文字列に変換するヘルパー関数
    QString deviceTypeToString(QBluetoothDeviceInfo::MajorDeviceClass type)
    {
      switch (type) {
          case QBluetoothDeviceInfo::MiscellaneousDevice: return "その他";
          case QBluetoothDeviceInfo::ComputerDevice:      return "コンピュータ";
          case QBluetoothDeviceInfo::PhoneDevice:        return "電話";
          case QBluetoothDeviceInfo::AudioVideoDevice:    return "オーディオ/ビデオ";
          case QBluetoothDeviceInfo::NetworkDevice:      return "ネットワーク";
          case QBluetoothDeviceInfo::PeripheralDevice:    return "周辺機器";
          case QBluetoothDeviceInfo::ImagingDevice:      return "イメージング";
          case QBluetoothDeviceInfo::WearableDevice:      return "ウェアラブル";
          case QBluetoothDeviceInfo::ToyDevice:          return "おもちゃ";
          case QBluetoothDeviceInfo::HealthDevice:        return "ヘルスケア";
          default: return "不明";
        }
    }
    // エラーコードを文字列に変換するヘルパー関数
    QString errorToString(QBluetoothDeviceDiscoveryAgent::Error error)
    {
      switch (error) {
          case QBluetoothDeviceDiscoveryAgent::NoError:                      return "エラーなし";
          case QBluetoothDeviceDiscoveryAgent::InputOutputError:            return "I/Oエラー";
          case QBluetoothDeviceDiscoveryAgent::PoweredOffError:              return "Bluetoothがオフ";
          case QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError: return "無効なアダプタ";
          case QBluetoothDeviceDiscoveryAgent::UnsupportedPlatformError:    return "未対応のプラットフォーム";
          case QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod:  return "未対応の探索方法";
          default: return "不明なエラー";
        }
    }
public:
    explicit BluetoothScanner(QObject* parent = nullptr) : QObject(parent)
    {
      try {
          // デバイス探索エージェントの作成
          discoveryAgent = std::make_unique<QBluetoothDeviceDiscoveryAgent>(this);
          // 各種シグナルとスロットの接続
          connect(discoveryAgent.get(), &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothScanner::onDeviceDiscovered);
          connect(discoveryAgent.get(), &QBluetoothDeviceDiscoveryAgent::finished, this, &BluetoothScanner::onScanFinished);
          connect(discoveryAgent.get(), static_cast<void(QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error),
                  this, &BluetoothScanner::onError);
      }
      catch (const std::exception &e) {
          qDebug() << "初期化エラー: " << e.what();
          throw;
      }
    }
    // スキャン開始メソッド
    void startScan()
    {
      try {
          qDebug() << "デバイススキャンを開始...";
          discoveryAgent->start();
      }
      catch (const std::exception &e) {
          qDebug() << "スキャン開始エラー: " << e.what();
          throw;
      }
    }
    // スキャン停止メソッド
    void stopScan()
    {
      try {
          qDebug() << "デバイススキャンを停止...";
          discoveryAgent->stop();
      }
      catch (const std::exception &e) {
          qDebug() << "スキャン停止エラー: " << e.what();
          throw;
      }
    }
private slots:
    // デバイスが存在する場合のスロット
    void onDeviceDiscovered(const QBluetoothDeviceInfo& device)
    {
      qDebug() << "デバイスの探索に成功:";
      qDebug() << "  名前: "        << device.name();
      qDebug() << "  アドレス: "      << device.address().toString();
      qDebug() << "  RSSI: "        << device.rssi();
      qDebug() << "  デバイスタイプ: " << deviceTypeToString(device.majorDeviceClass());
    }
    // スキャン完了時のスロット
    void onScanFinished()
    {
        qDebug() << "デバイススキャンが完了";
    }
    // エラー発生時のスロット
    void onError(QBluetoothDeviceDiscoveryAgent::Error error)
    {
        qDebug() << "エラーが発生: " << errorToString(error);
    }
};
</syntaxhighlight>
<br><br>
<br><br>


案内メニュー