我開始玩藍牙編程,將Flic按鈕連接到我的Ubuntu 16.04。爲此我使用Qt和它的BLE庫。然而,當我嘗試連接到我的按鈕,我得到這個錯誤:Qt藍牙LE - 資源暫時不可用
qt.bluetooth.bluez: void QBluetoothSocketPrivate::_q_readNotify() 7 error: -1 "Resource temporarily unavailable"
我也試圖直接與hcitool和gatttool在同一個按鈕連接,它爲幾秒鐘,然後我得到的錯誤
(gatttool:24877): GLib-WARNING **: Invalid file descriptor.
如果我嘗試用其他的按鈕我有,我得到了同樣的錯誤在Qt的,但
在命令行Error: connect error: Connection reset by peer (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
在此先感謝您的幫助。
Bluez 3. *是一個非常舊的版本。您應該升級到Bluez 5.46 – abhiarora
我已經從BLUEZ開發了自己的BLE堆棧。但出於原因,我不得不使用Qt BLE庫。你有沒有解決你的問題,因爲我現在面對它 – abhiarora