📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の8版が非表示) | |||
| 144行目: | 144行目: | ||
* AD Data (可変長) | * AD Data (可変長) | ||
<br> | <br> | ||
[[ファイル:Qt BLE 3.png|フレームなし|中央]] | |||
<br> | |||
==== その他 (通信特性 / セキュリティ等) ==== | ==== その他 (通信特性 / セキュリティ等) ==== | ||
* 通信特性 | * 通信特性 | ||
| 165行目: | 168行目: | ||
<br> | <br> | ||
==== 使用例 ==== | ==== 使用例 ==== | ||
Qt Bluetoothモジュールを使用したBLEスキャンの処理を以下に示す。<br> | |||
<br> | |||
* QBluetoothDeviceDiscoveryAgentクラス | |||
* QBluetoothDeviceInfoクラス | |||
<br> | |||
===== デバイス探索エージェントの作成 ===== | |||
QBluetoothDeviceDiscoveryAgentのインスタンスを生成する。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
#include <QBluetoothDeviceDiscoveryAgent> | |||
QBluetoothDeviceDiscoveryAgent discoveryAgent; | |||
</syntaxhighlight> | |||
<br> | |||
===== シグナル / スロット接続 ===== | |||
* QBluetoothDeviceDiscoveryAgent::deviceDiscoveredシグナル | |||
*: デバイスを発見した時に送信される。 | |||
* QBluetoothDeviceDiscoveryAgent::finished | |||
*: スキャンが完了した時に送信される。 | |||
* QBluetoothDeviceDiscoveryAgent::error | |||
*: エラーが発生した時に送信される。 | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// デバイス発見時 | |||
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &MyClass::onDeviceDiscovered); | |||
// スキャン完了時 | |||
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &MyClass::onScanFinished); | |||
// エラー発生時 | |||
// エラーハンドリングは必ず実装する | |||
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::error, this, &MyClass::onError); | |||
</syntaxhighlight> | |||
<br> | |||
===== スキャン開始 ===== | |||
<code>QBluetoothDeviceDiscoveryAgent::LowEnergyMethod</code>メソッドを指定して、<code>QBluetoothDeviceDiscoveryAgent::start</code>メソッドを実行する。<br> | |||
これによりBLEデバイスのスキャンが開始する。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); | |||
</syntaxhighlight> | |||
<br> | |||
===== デバイス発見時 ===== | |||
<code>QBluetoothDeviceDiscoveryAgent::deviceDiscovered</code>シグナルで通知する。<br> | |||
<code>QBluetoothDeviceInfo</code>クラスを使用して、以下に示す情報を取得することが可能である。<br> | |||
* デバイス名 | |||
* アドレス | |||
* 信号強度 (RSSI値) | |||
* サービスUUID | |||
* マニファクチャラーデータ | |||
<br> | |||
<u>ただし、これらの情報を取得する前は、必ず存在確認を行う。</u><br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
void onDeviceDiscovered(const QBluetoothDeviceInfo &device) | |||
{ | |||
// デバイス名の取得 | |||
QString name = device.name(); | |||
// アドレスの取得 | |||
QString address = device.address().toString(); | |||
// RSSI値の取得 | |||
qint16 rssi = device.rssi(); | |||
// サービスUUIDの取得 | |||
QList<QBluetoothUuid> services = device.serviceUuids(); | |||
// マニファクチャラーデータの取得 | |||
QMap<quint16, QByteArray> manufacturerData = device.manufacturerData(); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
===== スキャン完了時 ===== | |||
<code>QBluetoothDeviceDiscoveryAgent::finished</code>シグナルで送信する。<br> | |||
<br> | |||
スキャン完了時の処理を記述する。<br> | |||
また、必要に応じて再スキャンを開始する。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
void onScanFinished() | |||
{ | |||
// 必要に応じて再スキャン | |||
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
===== エラー発生時 ===== | |||
リソースの解放は適切に行う。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
void onError(QBluetoothDeviceDiscoveryAgent::Error error) | |||
{ | |||
switch (error) { | |||
case QBluetoothDeviceDiscoveryAgent::NoError: | |||
break; | |||
case QBluetoothDeviceDiscoveryAgent::InputOutputError: | |||
break; | |||
case QBluetoothDeviceDiscoveryAgent::PoweredOffError: | |||
break; | |||
case QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError: | |||
break; | |||
case QBluetoothDeviceDiscoveryAgent::UnsupportedPlatformError: | |||
break; | |||
case QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod: | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
===== スキャン停止 ===== | |||
<syntaxhighlight lang="c++"> | |||
discoveryAgent->stop(); | |||
</syntaxhighlight> | |||
<br> | |||
===== 組み合わせ ===== | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// BLEデバイスのスキャンを管理するクラス | // BLEデバイスのスキャンを管理するクラス | ||
| 408行目: | 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デバイスへの接続, サービスの探索と監視 | |||
#include <QObject> | #include <QObject> | ||
#include <QLowEnergyController> | #include <QLowEnergyController> | ||
#include <QLowEnergyService> | #include <QLowEnergyService> | ||
#include <QTimer> | #include <QTimer> | ||
#include <QDebug> | #include <QDebug> | ||
| 421行目: | 576行目: | ||
private: | private: | ||
QLowEnergyController controller; // BLE接続とサービス探索を制御するコントローラ | |||
QTimer discoveryTimeout; // サービス探索のタイムアウトを管理するタイマ | |||
// コントローラの各種シグナルを接続 | |||
// 接続 / 切断, サービス探索の進行状況, 状態変更, エラー | |||
void connectControllerSignals() | void connectControllerSignals() | ||
{ | { | ||
connect(controller | connect(&controller, &QLowEnergyController::connected, this, &BLEServiceDiscovery::onConnected); | ||
connect(controller | connect(&controller, &QLowEnergyController::disconnected, this, &BLEServiceDiscovery::onDisconnected); | ||
connect(controller | connect(&controller, &QLowEnergyController::serviceDiscovered, this, &BLEServiceDiscovery::onServiceDiscovered); | ||
connect(controller | connect(&controller, &QLowEnergyController::discoveryFinished, this, &BLEServiceDiscovery::onDiscoveryFinished); | ||
connect(controller | connect(&controller, static_cast<void(QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error), this, &BLEServiceDiscovery::onError); | ||
connect(controller | connect(&controller, &QLowEnergyController::stateChanged, this, &BLEServiceDiscovery::onStateChanged); | ||
} | } | ||
// 個別のBLEサービスに対するシグナル | |||
// 状態変更とエラーイベントを監視してログを出力する | |||
void connectServiceSignals(QLowEnergyService *service) | void connectServiceSignals(QLowEnergyService *service) | ||
{ | { | ||
| 445行目: | 604行目: | ||
} | } | ||
// エラーコードの変換 | |||
QString getErrorMessage(QLowEnergyController::Error error) | QString getErrorMessage(QLowEnergyController::Error error) | ||
{ | { | ||
| 501行目: | 661行目: | ||
{ | { | ||
// タイムアウトタイマの初期化 | // タイムアウトタイマの初期化 | ||
discoveryTimeout | discoveryTimeout.setInterval(10000); // 10秒のタイムアウト | ||
discoveryTimeout.setSingleShot(true); | |||
discoveryTimeout | |||
connect(discoveryTimeout | connect(&discoveryTimeout, &QTimer::timeout, this, &BLEServiceDiscovery::onDiscoveryTimeout); | ||
} | } | ||
// デバイスへの接続とサービス探索の開始 | // デバイスへの接続とサービス探索の開始 | ||
void startDiscovery(const QBluetoothDeviceInfo& device) | // コントローラの初期化, シグナルの接続, デバイスへの接続開始, タイムアウトタイマの開始 | ||
void startDiscovery(const QBluetoothDeviceInfo &device) | |||
{ | { | ||
try { | try { | ||
| 515行目: | 675行目: | ||
// コントローラの初期化 | // コントローラの初期化 | ||
connectControllerSignals(); | connectControllerSignals(); | ||
// 接続開始 | // 接続開始 | ||
controller | controller.connectToDevice(); | ||
discoveryTimeout | discoveryTimeout.start(); | ||
} | } | ||
catch (const std::exception &e) { | catch (const std::exception &e) { | ||
| 529行目: | 688行目: | ||
} | } | ||
// 探索を停止 | // 探索を停止 (タイムアウトタイマも停止) | ||
void stopDiscovery() | void stopDiscovery() | ||
{ | { | ||
try { | try { | ||
if (controller) { | if (controller) { | ||
controller | controller.disconnectFromDevice(); | ||
discoveryTimeout | discoveryTimeout.stop(); | ||
} | } | ||
} | } | ||
| 546行目: | 705行目: | ||
signals: | signals: | ||
void serviceDiscovered(QLowEnergyService *service); | void serviceDiscovered(QLowEnergyService *service); // 新しいサービスが発見された時に発行 | ||
void discoveryComplete(); | void discoveryComplete(); // 全てのサービス探索が完了した時に発行 | ||
void errorOccurred(const QString &error); | void errorOccurred(const QString &error); // エラーが発生した時に発行 | ||
void connectionStateChanged(QLowEnergyController::ControllerState state); | void connectionStateChanged(QLowEnergyController::ControllerState state); // 接続状態が変更された時に発行 | ||
private slots: | private slots: | ||
// デバイスへの接続が完了した時 (タイムアウトタイマを停止して、サービス探索を開始) | |||
void onConnected() | void onConnected() | ||
{ | { | ||
qDebug() << "デバイスに接続"; | qDebug() << "デバイスに接続"; | ||
discoveryTimeout | discoveryTimeout.stop(); | ||
// サービス探索の開始 | // サービス探索の開始 | ||
controller | controller.discoverServices(); | ||
} | } | ||
// デバイスから切断された時 (タイムアウトタイマを停止) | |||
void onDisconnected() | void onDisconnected() | ||
{ | { | ||
qDebug() << "デバイスから切断"; | qDebug() << "デバイスから切断"; | ||
discoveryTimeout | discoveryTimeout.stop(); | ||
} | } | ||
// 新しいサービスが発見された時 | |||
void onServiceDiscovered(const QBluetoothUuid& uuid) | void onServiceDiscovered(const QBluetoothUuid& uuid) | ||
{ | { | ||
| 572行目: | 734行目: | ||
// サービスオブジェクトの作成 | // サービスオブジェクトの作成 | ||
QLowEnergyService* service = controller | QLowEnergyService* service = controller.createServiceObject(uuid, this); | ||
if (service) { | if (service) { | ||
connectServiceSignals(service); | connectServiceSignals(service); | ||
| 579行目: | 741行目: | ||
} | } | ||
// サービス探索が完了した時 | |||
void onDiscoveryFinished() | void onDiscoveryFinished() | ||
{ | { | ||
qDebug() << "サービス探索が完了"; | qDebug() << "サービス探索が完了"; | ||
discoveryTimeout | discoveryTimeout.stop(); | ||
emit discoveryComplete(); | emit discoveryComplete(); | ||
} | } | ||
// エラーが発生した時 | |||
void onError(QLowEnergyController::Error error) | void onError(QLowEnergyController::Error error) | ||
{ | { | ||
| 593行目: | 757行目: | ||
} | } | ||
// タイムアウトが発生した時 (探索を停止して、エラーとして通知) | |||
void onDiscoveryTimeout() | void onDiscoveryTimeout() | ||
{ | { | ||
| 600行目: | 765行目: | ||
} | } | ||
// 接続状態が変更された時 (状態の変更をログ出力して、通知) | |||
void onStateChanged(QLowEnergyController::ControllerState state) | void onStateChanged(QLowEnergyController::ControllerState state) | ||
{ | { | ||
| 674行目: | 840行目: | ||
<br> | <br> | ||
==== 使用例 ==== | ==== 使用例 ==== | ||
===== ローカルデバイスの初期化 ===== | |||
BLEデバイスを利用するため、QBluetoothLocalDeviceクラスを使用して初期化する。<br> | |||
初期化時にBLEデバイスが有効かどうかの確認を行う。<br> | |||
<br> | |||
* QBluetoothLocalDeviceクラス | |||
* QBluetoothAddressクラス | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
#include <QBluetoothLocalDevice> | |||
#include <QBluetoothAddress> | |||
QBluetoothLocalDevice localDevice; | |||
if (!localDevice.isValid()) { | |||
qDebug() << "Bluetoothデバイスが利用できません"; | |||
return; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
===== ペアリング状態の変化を監視 ===== | |||
ペアリングの結果を受信すため、<code>QBluetoothLocalDevice::pairingFinished</code>シグナルに接続する。<br> | |||
これにより、ペアリング成功 / 失敗の通知を受け取ることができる。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
connect(&localDevice, &QBluetoothLocalDevice::pairingFinished, [](const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) { | |||
if (pairing == QBluetoothLocalDevice::Paired) { | |||
qDebug() << "ペアリング成功:" << address.toString(); | |||
} | |||
else { | |||
qDebug() << "ペアリング失敗:" << address.toString(); | |||
} | |||
}); | |||
</syntaxhighlight> | |||
<br> | |||
===== PINコード確認のシグナル (必要な場合のみ) ===== | |||
PINコードが必要なデバイスは、<code>BluetoothLocalDevice::pairingDisplayConfirmation</code>シグナルに接続する。<br> | |||
これにより、PINコードの確認が必要な場合に通知を受け取ることができる。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
connect(&localDevice, &QBluetoothLocalDevice::pairingDisplayConfirmation, [](const QBluetoothAddress &address, QString pin) { | |||
qDebug() << "PINコード確認: " << address.toString() << pin; | |||
}); | |||
</syntaxhighlight> | |||
<br> | |||
===== ペアリング要求 ===== | |||
ペアリングを開始するため、ペアリングするデバイスのBluetoothアドレスを指定して、<code>QBluetoothLocalDevice::requestPairing</code>メソッドを実行する。<br> | |||
<br> | |||
ペアリングの結果は、<code>QBluetoothLocalDevice::pairingFinished</code>シグナルで受信して、成功 / 失敗に応じた処理を行う。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
QBluetoothAddress targetAddress("XX:XX:XX:XX:XX:XX"); // 接続先アドレス | |||
localDevice.requestPairing(targetAddress, QBluetoothLocalDevice::Paired); | |||
</syntaxhighlight> | |||
<br> | |||
===== 組み合わせ ===== | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// BLEデバイスとのペアリング操作を管理するクラス | |||
// ペアリングの開始と解除, ペアリング状態の監視, PINコード認証 | |||
#include <QObject> | #include <QObject> | ||
#include <QBluetoothLocalDevice> | #include <QBluetoothLocalDevice> | ||
#include <QBluetoothAddress> | #include <QBluetoothAddress> | ||
#include <QDebug> | #include <QDebug> | ||
| 686行目: | 908行目: | ||
private: | private: | ||
QBluetoothLocalDevice localDevice; // ローカルのBLEデバイスを管理するオブジェクト | |||
// ペアリング状態を表す文字列 | |||
QString getPairingStatusString(QBluetoothLocalDevice::Pairing status) | QString getPairingStatusString(QBluetoothLocalDevice::Pairing status) | ||
{ | { | ||
| 696行目: | 919行目: | ||
default: return "不明な状態"; | default: return "不明な状態"; | ||
} | } | ||
} | |||
// BLEデバイスが使用可能な状態かどうかを確認 | |||
bool isDeviceReady() const | |||
{ | |||
if (!localDevice || !localDevice->isValid()) return false; | |||
return localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff; | |||
} | |||
// BLEデバイスのホストモードを設定 | |||
void setHostMode(QBluetoothLocalDevice::HostMode mode) | |||
{ | |||
if (isDeviceReady()) localDevice.setHostMode(mode); | |||
} | |||
// ホスト側においてBLEが利用可能かどうかを確認 | |||
bool isBluetoothAvailable() const | |||
{ | |||
return QBluetoothLocalDevice::allDevices().count() > 0; | |||
} | } | ||
public: | public: | ||
explicit BLEPairing(QObject* parent = nullptr) : QObject(parent) | explicit BLEPairing(QObject *parent = nullptr) : QObject(parent) | ||
{ | { | ||
try { | try { | ||
// シグナルの接続 | // シグナルの接続 | ||
connect(localDevice | connect(&localDevice, &QBluetoothLocalDevice::pairingFinished, this, &BLEPairing::onPairingFinished); | ||
connect(localDevice | connect(&localDevice, &QBluetoothLocalDevice::error, this, &BLEPairing::onError); | ||
connect(localDevice | connect(&localDevice, &QBluetoothLocalDevice::pairingDisplayConfirmation, this, &BLEPairing::onPairingDisplayConfirmation); | ||
connect(localDevice | connect(&localDevice, &QBluetoothLocalDevice::pairingDisplayPinCode, this, &BLEPairing::onPairingDisplayPinCode); | ||
} | } | ||
catch (const std::exception &e) { | catch (const std::exception &e) { | ||
| 717行目: | 956行目: | ||
} | } | ||
// | ~BLEPairing() | ||
{ | |||
if (localDevice) { | |||
disconnect(&localDevice); // 全ての接続を解除 | |||
} | |||
} | |||
// 指定したデバイスとのペアリングを開始 | |||
// 30秒のタイムアウトを設定 (失敗時はエラーを通知) | |||
void requestPairing(const QBluetoothAddress &address) | void requestPairing(const QBluetoothAddress &address) | ||
{ | { | ||
try { | try { | ||
if (! | if (!isDeviceReady()) { | ||
emit errorOccurred("ローカルBluetoothデバイスが無効です"); | emit errorOccurred("ローカルBluetoothデバイスが無効です"); | ||
return; | return; | ||
} | } | ||
// タイムアウトの設定 | |||
QTimer::singleShot(30000, this, [this, address]() { | |||
if (getPairingStatus(address) != QBluetoothLocalDevice::Paired) { | |||
emit errorOccurred("ペアリングがタイムアウトしました"); | |||
} | |||
}); | |||
qDebug() << "ペアリングを開始: " << address.toString(); | qDebug() << "ペアリングを開始: " << address.toString(); | ||
localDevice | localDevice.requestPairing(address, QBluetoothLocalDevice::Paired); | ||
} | } | ||
catch (const std::exception &e) { | catch (const std::exception &e) { | ||
emit errorOccurred("ペアリング開始時に予期せぬエラーが発生しました"); | |||
} | } | ||
} | } | ||
// | // 指定したBLEデバイスとのペアリングを解除 | ||
void removePairing(const QBluetoothAddress& address) | void removePairing(const QBluetoothAddress& address) | ||
{ | { | ||
try { | try { | ||
if (!localDevice | if (!localDevice.isValid()) { | ||
emit errorOccurred("ローカルBluetoothデバイスが無効"); | emit errorOccurred("ローカルBluetoothデバイスが無効"); | ||
return; | return; | ||
| 746行目: | 998行目: | ||
qDebug() << "ペアリングを解除: " << address.toString(); | qDebug() << "ペアリングを解除: " << address.toString(); | ||
localDevice | localDevice.requestPairing(address, QBluetoothLocalDevice::Unpaired); | ||
} | } | ||
catch (const std::exception &e) { | catch (const std::exception &e) { | ||
| 755行目: | 1,007行目: | ||
} | } | ||
// | // 指定したBLEデバイスとのペアリング状態を確認 | ||
QBluetoothLocalDevice::Pairing getPairingStatus(const QBluetoothAddress &address) | QBluetoothLocalDevice::Pairing getPairingStatus(const QBluetoothAddress &address) | ||
{ | { | ||
try { | try { | ||
if (!localDevice | if (!localDevice.isValid()) { | ||
emit errorOccurred("ローカルBluetoothデバイスが無効"); | emit errorOccurred("ローカルBluetoothデバイスが無効"); | ||
return QBluetoothLocalDevice::Unpaired; | return QBluetoothLocalDevice::Unpaired; | ||
} | } | ||
return localDevice | return localDevice.pairingStatus(address); | ||
} | } | ||
catch (const std::exception &e) { | catch (const std::exception &e) { | ||
| 775行目: | 1,027行目: | ||
signals: | signals: | ||
void pairingComplete(const QBluetoothAddress& address, bool success); | void pairingComplete(const QBluetoothAddress& address, bool success); // ペアリング完了通知 | ||
void pairingConfirmationRequired(const QBluetoothAddress& address, QString pin); | void pairingConfirmationRequired(const QBluetoothAddress& address, QString pin); // PIN確認要求通知 | ||
void errorOccurred(const QString& error); | void errorOccurred(const QString& error); // エラー通知 | ||
private slots: | private slots: | ||
// ペアリング完了時 | |||
void onPairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) | void onPairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) | ||
{ | { | ||
| 787行目: | 1,040行目: | ||
} | } | ||
// BLEエラー発生時 | |||
void onError() | void onError() | ||
{ | { | ||
QString errorMsg = " | QBluetoothLocalDevice::Error error = localDevice->error(); | ||
qDebug() << errorMsg; | QString errorMsg; | ||
switch (error) { | |||
case QBluetoothLocalDevice::NoError: | |||
errorMsg = "エラーなし"; | |||
break; | |||
case QBluetoothLocalDevice::PairingError: | |||
errorMsg = "ペアリングエラー"; | |||
break; | |||
case QBluetoothLocalDevice::UnknownError: | |||
default: | |||
errorMsg = "不明なエラー"; | |||
break; | |||
} | |||
qDebug() << "Bluetoothエラー: " << errorMsg; | |||
emit errorOccurred(errorMsg); | emit errorOccurred(errorMsg); | ||
} | } | ||
// ペアリング時のPIN確認要求処理 | |||
void onPairingDisplayConfirmation(const QBluetoothAddress &address, QString pin) | void onPairingDisplayConfirmation(const QBluetoothAddress &address, QString pin) | ||
{ | { | ||
| 802行目: | 1,072行目: | ||
} | } | ||
// PINコード表示要求時 | |||
void onPairingDisplayPinCode(const QBluetoothAddress &address, QString pin) | void onPairingDisplayPinCode(const QBluetoothAddress &address, QString pin) | ||
{ | { | ||
| 836行目: | 1,107行目: | ||
#include <QLowEnergyCharacteristic> | #include <QLowEnergyCharacteristic> | ||
#include <QTimer> | #include <QTimer> | ||
#include <QDebug> | #include <QDebug> | ||
| 844行目: | 1,114行目: | ||
private: | private: | ||
QLowEnergyController controller; | |||
QTimer reconnectTimer; | |||
QBluetoothDeviceInfo currentDevice; | QBluetoothDeviceInfo currentDevice; | ||
bool autoReconnect = false; | bool autoReconnect = false; | ||
| 851行目: | 1,121行目: | ||
void connectControllerSignals() | void connectControllerSignals() | ||
{ | { | ||
connect(controller | connect(&controller, &QLowEnergyController::connected, this, &BLEConnection::onConnected); | ||
connect(controller | connect(&controller, &QLowEnergyController::disconnected, this, &BLEConnection::onDisconnected); | ||
connect(controller | connect(&controller, &QLowEnergyController::serviceDiscovered, this, &BLEConnection::onServiceDiscovered); | ||
connect(controller | connect(&controller, static_cast<void(QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error), this, &BLEConnection::onError); | ||
connect(controller | connect(&controller, &QLowEnergyController::stateChanged, this, &BLEConnection::onStateChanged); | ||
} | } | ||
| 966行目: | 1,236行目: | ||
{ | { | ||
// 再接続タイマの初期化 | // 再接続タイマの初期化 | ||
reconnectTimer | reconnectTimer.setInterval(5000); // 5秒間隔で再接続 | ||
reconnectTimer.setSingleShot(true); | |||
reconnectTimer | |||
connect(reconnectTimer | connect(&reconnectTimer &QTimer::timeout, this, &BLEConnection::onReconnectTimeout); | ||
} | } | ||
| 980行目: | 1,249行目: | ||
// コントローラの初期化 | // コントローラの初期化 | ||
connectControllerSignals(); | connectControllerSignals(); | ||
// 接続パラメータの設定 | // 接続パラメータの設定 | ||
controller | controller.setRemoteAddressType(QLowEnergyController::PublicAddress); | ||
// 接続開始 | // 接続開始 | ||
controller | controller.connectToDevice(); | ||
currentDevice = device; | currentDevice = device; | ||
} | } | ||
| 1,002行目: | 1,270行目: | ||
try { | try { | ||
if (controller) { | if (controller) { | ||
controller | controller.disconnectFromDevice(); | ||
reconnectTimer | reconnectTimer.stop(); | ||
} | } | ||
} | } | ||
| 1,018行目: | 1,286行目: | ||
autoReconnect = enable; | autoReconnect = enable; | ||
if (!enable) { | if (!enable) { | ||
reconnectTimer | reconnectTimer.stop(); | ||
} | } | ||
} | } | ||
| 1,033行目: | 1,301行目: | ||
{ | { | ||
qDebug() << "デバイスに接続"; | qDebug() << "デバイスに接続"; | ||
reconnectTimer | reconnectTimer.stop(); | ||
emit connected(); | emit connected(); | ||
// サービスの探索を開始 | // サービスの探索を開始 | ||
controller | controller.discoverServices(); | ||
} | } | ||
| 1,048行目: | 1,316行目: | ||
if (autoReconnect) { | if (autoReconnect) { | ||
qDebug() << "再接続を試行..."; | qDebug() << "再接続を試行..."; | ||
reconnectTimer | reconnectTimer.start(); | ||
} | } | ||
} | } | ||
| 1,056行目: | 1,324行目: | ||
qDebug() << "サービスを発見: " << uuid.toString(); | qDebug() << "サービスを発見: " << uuid.toString(); | ||
QLowEnergyService* service = controller | QLowEnergyService* service = controller.createServiceObject(uuid, this); | ||
if (service) { | if (service) { | ||
connectServiceSignals(service); | connectServiceSignals(service); | ||
| 1,069行目: | 1,337行目: | ||
emit errorOccurred(errorMessage); | emit errorOccurred(errorMessage); | ||
if (autoReconnect && error != QLowEnergyController::InvalidBluetoothAdapterError) reconnectTimer | if (autoReconnect && error != QLowEnergyController::InvalidBluetoothAdapterError) reconnectTimer.start(); | ||
} | } | ||
| 1,082行目: | 1,350行目: | ||
if (autoReconnect && currentDevice.isValid()) { | if (autoReconnect && currentDevice.isValid()) { | ||
qDebug() << "再接続を試行..."; | qDebug() << "再接続を試行..."; | ||
controller | controller.connectToDevice(); | ||
} | } | ||
} | } | ||