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

ナビゲーションに移動 検索に移動
666行目: 666行目:
* 1度ペアリングされたデバイスは、特別な設定がない限り、自動的に再接続可能になる。
* 1度ペアリングされたデバイスは、特別な設定がない限り、自動的に再接続可能になる。
<br>
<br>
==== 初期設定 ====
<code>QBluetoothLocalDevice</code>クラスのインスタンスを生成して、ローカルのBluetoothアダプタを初期化する。<br>
<br>
以下に示すペアリング関連のシグナルに対するスロットを接続する。<br>
* QBluetoothLocalDevice::pairingFinished
* QBluetoothLocalDevice::pairingDisplayConfirmation
* QBluetoothLocalDevice::pairingDisplayPinCode
* QBluetoothLocalDevice::error
<br>
これらの処理は非同期で行われており、シグナル / スロットメカニズムを通じて状態の変更が通知される。<br>
そのため、ユーザインターフェースと組み合わせる場合は、各シグナルに応じて適切なUIの更新を行う必要がある。<br>
<br>
==== ペアリング要求 ====
リモートデバイスのBluetoothアドレス (QBluetoothAddressクラス) を指定して、<code>QBluetoothLocalDevice::requestPairing</code>メソッドを実行する。<br>
この時、ペアリングモードとして<code>QBluetoothLocalDevice::Paired</code>を指定する。<br>
<br>
==== ペアリング処理中 ====
Bluetoothデバイスによっては、PINコードの表示や確認が必要になる。<br>
<br>
<code>QBluetoothLocalDevice::pairingDisplayPinCode</code>シグナルを受信した場合、ユーザにPINコードを表示する。<br>
<br>
<code>QBluetoothLocalDevice::pairingDisplayConfirmation</code>シグナルを受信した場合は、ユーザに確認を求めて、<code>QBluetoothLocalDevice::pairingConfirmation</code>メソッドで応答する。<br>
<br>
==== ペアリング完了 ====
<code>QBluetoothLocalDevice::pairingFinished</code>シグナルを受信して処理結果を確認する。<br>
<br>
エラーが発生した場合は、<code>QBluetoothLocalDevice::error</code>シグナルを受信する。<br>
<br>
==== ペアリング管理 ====
<code>QBluetoothLocalDevice::connectedDevices</code>メソッドを実行して、ペアリング済みデバイスの一覧を取得することが可能である。<br>
また、<code>QBluetoothLocalDevice::pairingStatus</code>メソッドを実行して、特定デバイスのペアリング状態を確認できる。<br>
<br>
ペアリングを解除する場合は、ペアリングモードを<code>QBluetoothLocalDevice::Unpaired</code>に指定して<code>QBluetoothLocalDevice::requestPairing</code>メソッドを実行する。<br>
<br>
==== 組み合わせ ====
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  #include <QBluetoothLocalDevice>
  #include <QBluetoothLocalDevice>
  #include <QDebug>
  #include <QDebug>
#include <memory>
   
   
  class PairingManager : public QObject
  class PairingManager : public QObject
676行目: 710行目:
   
   
  private:
  private:
     std::unique_ptr<QBluetoothLocalDevice> localDevice;
     QBluetoothLocalDevice localDevice;
   
   
     // ペアリング状態を文字列に変換するヘルパー関数
     // ペアリング状態
     QString pairingStatusToString(QBluetoothLocalDevice::Pairing status)
     QString pairingStatusToString(QBluetoothLocalDevice::Pairing status)
     {
     {
690行目: 724行目:
   
   
  public:
  public:
     explicit PairingManager(QObject* parent = nullptr) : QObject(parent)
     explicit PairingManager(QObject *parent = nullptr) : QObject(parent), localDevice(this)  
     {
     {
      try {
        // 各種シグナルとスロットの接続
          // ローカルBluetoothデバイスの初期化
        connect(&localDevice, &QBluetoothLocalDevice::pairingFinished, this, &PairingManager::onPairingFinished);
          localDevice = std::make_unique<QBluetoothLocalDevice>(this);
        connect(&localDevice, &QBluetoothLocalDevice::error, this, &PairingManager::onError);
        connect(&localDevice, &QBluetoothLocalDevice::pairingDisplayConfirmation, this, &PairingManager::onPairingConfirmationRequest);
          // 各種シグナルとスロットの接続
        connect(&localDevice, &QBluetoothLocalDevice::pairingDisplayPinCode, this, &PairingManager::onPairingDisplayPinCode);
          connect(localDevice.get(), &QBluetoothLocalDevice::pairingFinished, this, &PairingManager::onPairingFinished);
          connect(localDevice.get(), &QBluetoothLocalDevice::error, this, &PairingManager::onError);
          connect(localDevice.get(), &QBluetoothLocalDevice::pairingDisplayConfirmation, this, &PairingManager::onPairingConfirmationRequest);
          connect(localDevice.get(), &QBluetoothLocalDevice::pairingDisplayPinCode, this, &PairingManager::onPairingDisplayPinCode);
      }
      catch (const std::exception &e) {
          qDebug() << "初期化エラー:" << e.what();
          throw;
      }
     }
     }
   
   
711行目: 736行目:
     void requestPairing(const QBluetoothAddress& address)
     void requestPairing(const QBluetoothAddress& address)
     {
     {
       try {
       qDebug() << "ペアリングを開始..." << address.toString();
          qDebug() << "ペアリングを開始..." << address.toString();
      localDevice.requestPairing(address, QBluetoothLocalDevice::Paired);
          localDevice->requestPairing(address, QBluetoothLocalDevice::Paired);
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング開始エラー: " << e.what();
          throw;
      }
     }
     }
   
   
724行目: 743行目:
     void removePairing(const QBluetoothAddress& address)
     void removePairing(const QBluetoothAddress& address)
     {
     {
       try {
       qDebug() << "ペアリングを解除..." << address.toString();
          qDebug() << "ペアリングを解除..." << address.toString();
      localDevice.requestPairing(address, QBluetoothLocalDevice::Unpaired);
          localDevice->requestPairing(address, QBluetoothLocalDevice::Unpaired);
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング解除エラー:" << e.what();
          throw;
      }
     }
     }
   
   
737行目: 750行目:
     QBluetoothLocalDevice::Pairing getPairingStatus(const QBluetoothAddress &address)
     QBluetoothLocalDevice::Pairing getPairingStatus(const QBluetoothAddress &address)
     {
     {
       try {
       return localDevice.pairingStatus(address);
          return localDevice->pairingStatus(address);
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング状態確認エラー:" << e.what();
          throw;
      }
     }
     }
   
   
749行目: 756行目:
     QList<QBluetoothAddress> getPairedDevices()
     QList<QBluetoothAddress> getPairedDevices()
     {
     {
       try {
       return localDevice.connectedDevices();
          return localDevice->connectedDevices();
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング済みデバイス取得エラー: " << e.what();
          throw;
      }
     }
     }
   
   
  private slots:
  private slots:
     // ペアリング完了時のスロット
     // ペアリング完了時
     void onPairingFinished(const QBluetoothAddress& address, QBluetoothLocalDevice::Pairing status)
     void onPairingFinished(const QBluetoothAddress& address, QBluetoothLocalDevice::Pairing status)
     {
     {
767行目: 768行目:
     }
     }
   
   
     // エラー発生時のスロット
     // エラー発生時
     void onError()
     void onError()
     {
     {
773行目: 774行目:
     }
     }
   
   
     // ペアリング確認要求時のスロット
     // ペアリング確認要求時
     void onPairingConfirmationRequest(const QBluetoothAddress& address, QString pin)
     void onPairingConfirmationRequest(const QBluetoothAddress& address, QString pin)
     {
     {
782行目: 783行目:
       // ここに、ユーザに確認を求めるUIを表示する
       // ここに、ユーザに確認を求めるUIを表示する
       // 以下の例では、自動的に確認している
       // 以下の例では、自動的に確認している
       localDevice->pairingConfirmation(true);
       localDevice.pairingConfirmation(true);
     }
     }
   
   
     // PINコード表示要求時のスロット
     // PINコード表示要求時
     void onPairingDisplayPinCode(const QBluetoothAddress &address, QString pin)
     void onPairingDisplayPinCode(const QBluetoothAddress &address, QString pin)
     {
     {

案内メニュー