Korydween
Korydween

Reputation: 13

Qt Bluetooth LE - resource temporarily unavailable

I am starting play around with bluetooth programming to connect a Flic button to my Ubuntu 16.04. For that I am using Qt and its BLE library. However when I try to connect to my button, I get this error:

qt.bluetooth.bluez: void QBluetoothSocketPrivate::_q_readNotify() 7 error: -1 "Resource temporarily unavailable"

I also tried to connect directly with hcitool and gatttool on the same button, it works for some seconds and then I get the error

(gatttool:24877): GLib-WARNING **: Invalid file descriptor.

If I try with the other button I have, I get the same error in Qt but

Error: connect error: Connection reset by peer (104)

in the command line.

Can anyone explain to me what's happening? I have Ubuntu 16.04, blueZ 5.37 and Qt 5.7. Here is a minimal example of my program:

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

And here are the command the the results from the terminal:

$ 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

Thanks in advance for your help.

Upvotes: 1

Views: 1207

Answers (1)

Emil
Emil

Reputation: 18482

Lol. QT's bluetooth support doesn't seem to be top-notch. You should use https://github.com/50ButtonsEach/fliclib-linux-hci instead.

Upvotes: 0

Related Questions