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

ナビゲーションに移動 検索に移動
308行目: 308行目:
* ペアリング状態の管理 (新規ペアリング、ペアリング解除、ペアリング状態の確認等) が含まれる。
* ペアリング状態の管理 (新規ペアリング、ペアリング解除、ペアリング状態の確認等) が含まれる。
* 1度ペアリングされたデバイスは、特別な設定がない限り、自動的に再接続可能になる。
* 1度ペアリングされたデバイスは、特別な設定がない限り、自動的に再接続可能になる。
<br>
<syntaxhighlight lang="c++">
#include <QBluetoothLocalDevice>
#include <QDebug>
#include <memory>
class PairingManager : public QObject
{
    Q_OBJECT
private:
    std::unique_ptr<QBluetoothLocalDevice> localDevice;
    // ペアリング状態を文字列に変換するヘルパー関数
    QString pairingStatusToString(QBluetoothLocalDevice::Pairing status)
    {
      switch (status) {
          case QBluetoothLocalDevice::Unpaired:        return "未ペアリング";
          case QBluetoothLocalDevice::Paired:          return "ペアリング済み";
          case QBluetoothLocalDevice::AuthorizedPaired: return "認証済みペアリング";
          default:                                      return "不明な状態";
      }
    }
public:
    explicit PairingManager(QObject* parent = nullptr) : QObject(parent)
    {
      try {
          // ローカルBluetoothデバイスの初期化
          localDevice = std::make_unique<QBluetoothLocalDevice>(this);
          // 各種シグナルとスロットの接続
          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;
      }
    }
    // ペアリングを開始
    void requestPairing(const QBluetoothAddress& address)
    {
      try {
          qDebug() << "ペアリングを開始..." << address.toString();
          localDevice->requestPairing(address, QBluetoothLocalDevice::Paired);
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング開始エラー: " << e.what();
          throw;
      }
    }
    // ペアリングの解除
    void removePairing(const QBluetoothAddress& address)
    {
      try {
          qDebug() << "ペアリングを解除..." << address.toString();
          localDevice->requestPairing(address, QBluetoothLocalDevice::Unpaired);
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング解除エラー:" << e.what();
          throw;
      }
    }
    // ペアリング状態の確認
    QBluetoothLocalDevice::Pairing getPairingStatus(const QBluetoothAddress &address)
    {
      try {
          return localDevice->pairingStatus(address);
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング状態確認エラー:" << e.what();
          throw;
      }
    }
    // ペアリング済みデバイスの一覧を取得
    QList<QBluetoothAddress> getPairedDevices()
    {
      try {
          return localDevice->connectedDevices();
      }
      catch (const std::exception &e) {
          qDebug() << "ペアリング済みデバイス取得エラー: " << e.what();
          throw;
      }
    }
private slots:
    // ペアリング完了時のスロット
    void onPairingFinished(const QBluetoothAddress& address, QBluetoothLocalDevice::Pairing status)
    {
      qDebug() << "ペアリング処理が完了:";
      qDebug() << "  デバイス:" << address.toString();
      qDebug() << "  状態:" << pairingStatusToString(status);
    }
    // エラー発生時のスロット
    void onError()
    {
        qDebug() << "ペアリング処理でエラーが発生";
    }
    // ペアリング確認要求時のスロット
    void onPairingConfirmationRequest(const QBluetoothAddress& address, QString pin)
    {
      qDebug() << "ペアリング確認要求を受信:";
      qDebug() << "  デバイス:" << address.toString();
      qDebug() << "  確認コード:" << pin;
      // ここに、ユーザに確認を求めるUIを表示する
      // 以下の例では、自動的に確認している
      localDevice->pairingConfirmation(true);
    }
    // PINコード表示要求時のスロット
    void onPairingDisplayPinCode(const QBluetoothAddress &address, QString pin)
    {
      qDebug() << "PINコード表示要求を受信:";
      qDebug() << "  デバイス:" << address.toString();
      qDebug() << "  PINコード:" << pin;
    }
};
</syntaxhighlight>
<br><br>
<br><br>


案内メニュー