c++ - Qt - 与 BLE 设备断开连接

标签 c++ qt bluetooth-lowenergy qtcore

我试图理解为什么使用 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 配对。

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

最佳答案

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

您需要取消订阅通知:

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

关于c++ - Qt - 与 BLE 设备断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30839455/

相关文章:

Android BLE 熄屏时断开连接(不带充电器)

c++ - 使用基类指针调用带有派生类参数的模板化函数

C++ - 模板子类继承自多个模板类

c++ - 文件传输C++

ios - Swift 中 BLE 的后台扫描

android - 外围设备模式下的 Nexus 9 不接受来自客户端的连接或不响应客户端请求?

c++ - 同时学习 Visual C++ 2008 和 C++?有什么资源可以推荐吗?

Qt - 创建小部件的平滑圆角

qt - 如何在 QML 中按下回车键时触发按钮单击

qt - qmake 项目文件中的 INCLUDEPATH 不起作用