gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:42:05 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com