Qt - 与 BLE 设备断开连接

Qt - disconnection from BLE device

我想了解为什么使用 Qt 重新连接到 BLE 设备失败。 我的系统是 Ubuntu 14.04,带有内置 BT 适配器,使用 Qt 5.5.0 beta(也发生在 Qt 5.4.0 中)。

基本上我想做的是在决定与 BLE 设备断开连接后重新连接到 BLE 设备,无论是相同的还是不同的。请注意,第一个连接很好并且可以正常工作。 我在执行 m_control->connectToDevice(); 之后得到的错误是 QLowEnergyController::UnknownError.

连接部分的存根(基于示例代码):

    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();

以及断线部分:

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

delete m_control;
m_control = NULL;

重新连接的唯一方法是重置蓝牙适配器或重置远程蓝牙设备。软件断开连接后我也无法扫描设备,所以我猜测它仍然与PC配对。

我是不是在这个过程中做错了什么?

您是否订阅了任何通知?我只看到断开连接部分但没有取消订阅部分。不知是不是因为你之前的连接让外设进入了不适合新连接的状态

您需要退订通知:

//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;
}