13,032
回編集
 (→使用例)  | 
				 (→使用例)  | 
				||
| 1,104行目: | 1,104行目: | ||
  #include <QLowEnergyCharacteristic>  |   #include <QLowEnergyCharacteristic>  | ||
  #include <QTimer>  |   #include <QTimer>  | ||
  #include <QDebug>  |   #include <QDebug>  | ||
| 1,112行目: | 1,111行目: | ||
  private:  |   private:  | ||
     controller;  | |||
     reconnectTimer;  | |||
     QBluetoothDeviceInfo currentDevice;  |      QBluetoothDeviceInfo currentDevice;  | ||
     bool autoReconnect = false;  |      bool autoReconnect = false;  | ||
| 1,119行目: | 1,118行目: | ||
     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);  | ||
     }  |      }  | ||
| 1,234行目: | 1,233行目: | ||
     {  |      {  | ||
        // 再接続タイマの初期化  |         // 再接続タイマの初期化  | ||
        reconnectTimer   |         reconnectTimer.setInterval(5000);  // 5秒間隔で再接続  | ||
        reconnectTimer.setSingleShot(true);  | |||
        reconnectTimer  | |||
        connect(reconnectTimer.get(), &QTimer::timeout, this, &BLEConnection::onReconnectTimeout);  |         connect(reconnectTimer.get(), &QTimer::timeout, this, &BLEConnection::onReconnectTimeout);  | ||
| 1,248行目: | 1,246行目: | ||
           // コントローラの初期化  |            // コントローラの初期化  | ||
           connectControllerSignals();  |            connectControllerSignals();  | ||
           // 接続パラメータの設定  |            // 接続パラメータの設定  | ||
           controller  |            controller.setRemoteAddressType(QLowEnergyController::PublicAddress);  | ||
           // 接続開始  |            // 接続開始  | ||
           controller  |            controller.connectToDevice();  | ||
           currentDevice = device;  |            currentDevice = device;  | ||
        }  |         }  | ||
| 1,270行目: | 1,267行目: | ||
        try {  |         try {  | ||
           if (controller) {  |            if (controller) {  | ||
              controller  |               controller.disconnectFromDevice();  | ||
              reconnectTimer  |               reconnectTimer.stop();  | ||
           }  |            }  | ||
        }  |         }  | ||
| 1,286行目: | 1,283行目: | ||
        autoReconnect = enable;  |         autoReconnect = enable;  | ||
        if (!enable) {  |         if (!enable) {  | ||
           reconnectTimer  |            reconnectTimer.stop();  | ||
        }  |         }  | ||
     }  |      }  | ||
| 1,301行目: | 1,298行目: | ||
     {  |      {  | ||
        qDebug() << "デバイスに接続";  |         qDebug() << "デバイスに接続";  | ||
        reconnectTimer  |         reconnectTimer.stop();  | ||
        emit connected();  |         emit connected();  | ||
        // サービスの探索を開始  |         // サービスの探索を開始  | ||
        controller  |         controller.discoverServices();  | ||
     }  |      }  | ||
| 1,316行目: | 1,313行目: | ||
        if (autoReconnect) {  |         if (autoReconnect) {  | ||
           qDebug() << "再接続を試行...";  |            qDebug() << "再接続を試行...";  | ||
           reconnectTimer  |            reconnectTimer.start();  | ||
        }  |         }  | ||
     }  |      }  | ||
| 1,324行目: | 1,321行目: | ||
        qDebug() << "サービスを発見: " << uuid.toString();  |         qDebug() << "サービスを発見: " << uuid.toString();  | ||
        QLowEnergyService* service = controller  |         QLowEnergyService* service = controller.createServiceObject(uuid, this);  | ||
        if (service) {  |         if (service) {  | ||
           connectServiceSignals(service);  |            connectServiceSignals(service);  | ||
| 1,337行目: | 1,334行目: | ||
        emit errorOccurred(errorMessage);  |         emit errorOccurred(errorMessage);  | ||
        if (autoReconnect && error != QLowEnergyController::InvalidBluetoothAdapterError) reconnectTimer  |         if (autoReconnect && error != QLowEnergyController::InvalidBluetoothAdapterError) reconnectTimer.start();  | ||
     }  |      }  | ||
| 1,350行目: | 1,347行目: | ||
        if (autoReconnect && currentDevice.isValid()) {  |         if (autoReconnect && currentDevice.isValid()) {  | ||
           qDebug() << "再接続を試行...";  |            qDebug() << "再接続を試行...";  | ||
           controller  |            controller.connectToDevice();  | ||
        }  |         }  | ||
     }  |      }  | ||