Qt蓝牙LE - 资源暂时无法使用

时间:2016-11-17 10:52:27

标签: qt bluetooth-lowenergy bluez

我开始玩蓝牙编程将Flic按钮连接到我的Ubuntu 16.04。为此我使用Qt及其BLE库。但是,当我尝试连接到我的按钮时,我收到此错误:

  

qt.bluetooth.bluez:void QBluetoothSocketPrivate :: _ q_readNotify()7错误:-1“资源暂时不可用”

我也尝试在同一个按钮上直接连接hcitool和gatttool,它可以工作几秒钟,然后我收到错误

  

(gatttool:24877):GLib-WARNING **:文件描述符无效。

如果我尝试使用其他按钮,我在Qt中会遇到相同的错误,但是

  

错误:连接错误:由对等方重置连接(104)

在命令行中。

有谁可以向我解释发生了什么?我有Ubuntu 16.04,blueZ 5.37和Qt 5.7。这是我的程序的一个最小例子:

的main.cpp

#include <QCoreApplication>
#include "device.h"
#include "bledevice.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //connecting to the device
    BLEDevice theButton;
    return a.exec();
}

bledevice.cpp

#include "bledevice.h"
#include <QBluetoothAddress>
#include <QBluetoothLocalDevice>
#include <QBluetoothServiceDiscoveryAgent>
#include <QLowEnergyController>

#include <QDebug>

BLEDevice::BLEDevice(QObject *parent) : QObject(parent)
{
    QBluetoothAddress address((QString("xx:xx:xx:xx:xx:xx")));
    controller = new QLowEnergyController(address);


    connect(controller, SIGNAL(connected()),
            this, SLOT(deviceConnected()));
    connect(controller, SIGNAL(error(QLowEnergyController::Error)),
            this, SLOT(errorReceived(QLowEnergyController::Error)));
    connect(controller, SIGNAL(disconnected()),
            this, SLOT(deviceDisconnected()));

    qDebug() << "Trying to connect..." ;
    qDebug() << QT_VERSION_STR;
     controller->connectToDevice();

}

void BLEDevice::deviceConnected()
{
    qDebug() << "Device connected";
}

void BLEDevice::deviceDisconnected()
{
    qDebug() << "Device disconnected";
    controller->disconnectFromDevice();
}

void BLEDevice::errorReceived(QLowEnergyController::Error /*error*/)
{
    qWarning() << "Error: " << controller->errorString();
}

bledevice.h

#ifndef BLEDEVICE_H
#define BLEDEVICE_H

#include <QObject>
#include <QLowEnergyController>

class BLEDevice : public QObject
{
    Q_OBJECT
public:
    explicit BLEDevice(QObject *parent = 0);

protected:
     QLowEnergyController *controller;

signals:

public slots:
    void deviceConnected();
    void deviceDisconnected();
    void errorReceived(QLowEnergyController::Error);
};

#endif // BLEDEVICE_H

以下是来自终端的结果命令:

$ sudo hcitool lescan
[sudo] password for jennifer: 
LE Scan ...
xx:xx:xx:xx:xx:xx f022cpdk
xx:xx:xx:xx:xx:xx (unknown)
yy:yy:yy:yy:yy:yy f022cpGv
yy:yy:yy:yy:yy:yy (unknown)
$sudo gatttool -b xx:xx:xx:xx:xx:xx -I
[xx:xx:xx:xx:xx:xx][LE]> connect
Attempting to connect to xx:xx:xx:xx:xx:xx
Connection successful
[xx:xx:xx:xx:xx:xx][LE]> 
(gatttool:24877): GLib-WARNING **: Invalid file descriptor.
[xx:xx:xx:xx:xx:xx][LE]>exit
$sudo gatttool -b yy:yy:yy:yy:yy:yy -I
[yy:yy:yy:yy:yy:yy][LE]> connect
Attempting to connect to yy:yy:yy:yy:yy:yy
Error: connect error: Connection reset by peer (104)
[yy:yy:yy:yy:yy:yy][LE]> exit

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

洛尔。 QT的蓝牙支持似乎并不是一流的。您应该使用https://github.com/50ButtonsEach/fliclib-linux-hci代替。