Qt - disconnection from BLE device

2.6k views Asked by At

I'm trying to understand why reconnection to BLE device fails with Qt. My system is Ubuntu 14.04 with built-in BT adapter, using Qt 5.5.0 beta (happened also with Qt 5.4.0).

Basically what I'm trying to do is to reconnect to a BLE device, either the same or a different one, after deciding to disconnect from a BLE device. Note that the first connection is fine and works as it should be. The error I'm getting right after doing m_control->connectToDevice(); is QLowEnergyController::UnknownError.

Stub of the connection part (based on the example code):

    m_control = new QLowEnergyController(QBluetoothAddress(connection_string), this);
connect(m_control, SIGNAL(serviceDiscovered(QBluetoothUuid)),
        this, SLOT(serviceDiscovered(QBluetoothUuid)));
connect(m_control, SIGNAL(discoveryFinished()),
        this, SLOT(serviceScanDone()));
connect(m_control, SIGNAL(error(QLowEnergyController::Error)),
        this, SLOT(controllerError(QLowEnergyController::Error)));
connect(m_control, SIGNAL(connected()),
        this, SLOT(deviceConnected()));
connect(m_control, SIGNAL(disconnected()),
        this, SLOT(deviceDisconnected()));
    m_control->connectToDevice();

And the disconnection part:

if (m_control->state() != QLowEnergyController::UnconnectedState) {
    m_control->disconnectFromDevice();
}

delete m_control;
m_control = NULL;

The only way to reconnect is to reset the BT adapter or reset the remote BT device. I'm also unable to scan the device after software disconnection, so I'm guessing that it's still paired to the PC.

Am I doing something wrong in the process?

1

There are 1 answers

2
I am user3179295 On BEST ANSWER

Did you subscribe to any notifications? I only see the disconnection part but no unsubscribing part. I wonder if it is because your previous connection has put the peripheral into a state that is not suitable for a new connection.

You need to unsubscribe from the notification:

//disable notifications
if (m_notificationDesc.isValid() && m_service) {
    m_service->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0000"));
} else {
    m_control->disconnectFromDevice();
    delete m_service;
    m_service = 0;
}