简体   繁体   中英

How to detect Bluetooth crash or shutdown with Qt?

I'm try to communicate with a Bluetooth Low Energy device with de QtBluetooth.dll. So, I've made an application that list every BLE devices, connect to it, read write and notify values and, finally, disconnect.

I listen to all state changement with the connect() function of Qt ; using SIGNALS/SLOTS system.

controller = new QLowEnergyController(currentDevice.getDevice());
    connect(controller, &QLowEnergyController::connected,
            this, &Device::deviceConnected);
    connect(controller, &QLowEnergyController::disconnected,
        this, &Device::deviceDisconnected);
    connect(controller, &QLowEnergyController::discoveryFinished,
        this, &Device::serviceScanDone);
    connect(controller, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
            this, &Device::errorReceived);
    connect(controller, &QLowEnergyController::serviceDiscovered,
            this, &Device::addLowEnergyService);
    connect(controller, &QLowEnergyController::stateChanged,
            this, &Device::deviceStateChanged);

That's works fine but there is a problem. When my device suddenly shutdown, I have absolutly no way to get the "crash" or the shutdown of my device. Even the "stateChanged" event of my controller object doesn't throw any state.

So how can I retrieve the crash when it comes up ? Is there an event listener I am missing ?

Thanks for your help !

[EDIT]

I've made a QThread to handle error and, even when I shut down my device, the qInfo() say this :

QLowEnergyController::Error(NoError)
QLowEnergyController::ControllerState(DiscoveredState)

the QThread :

QThread * errorThread = QThread::create([this]() {
        while (this->controller->error() != QLowEnergyController::NetworkError)
        {
            qInfo() << this->controller->error();
            qInfo() << this->controller->state();
            Sleep(1000);
        }
        qInfo() << this->controller->error();
        qInfo() << this->controller->state();
    });
    errorThread->start();

Which Qt version are you using? Since Qt 5.10, there is an error type QLowEnergyController::RemoteHostClosedError :

The remote device closed the connection. This value was introduced by Qt 5.10.

See http://doc.qt.io/qt-5/qlowenergycontroller.html#Error-enum for further information. This error will be emitted via the error -signal.

In earlier versions as well, I am pretty sure that you will receive a QLowEnergyController::NetworkError when reading from or writing to a crashed device.

Considering Qt supports BLE on Mac, iOS, Android and Windows 10, the only reliable way to tell if your connections are still alive is to peridocally do I/O with them and see if your Connection- or Serviceobjects signal an error or not. If you're exclusively using BLE/4.1 I'd suggest to request a read on the smallest gatt attribute, most devices have a 'battery' service that you're bound to check periodically anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM