【问题标题】:Qt - disconnection from BLE deviceQt - 与 BLE 设备断开连接
【发布时间】:2015-06-15 07:23:49
【问题描述】:

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

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

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

    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;

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

我在这个过程中做错了吗?

【问题讨论】:

    标签: c++ qt bluetooth-lowenergy qtcore


    【解决方案1】:

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

    您需要取消订阅通知:

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

    【讨论】:

    • 启用通知会有这种效果吗?那么是的,应用程序订阅了通知
    • 你说得对。我还需要在断开连接之前等待写入确认(descriptorWritten 信号发射)
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2020-10-17
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多