Problem with establishing classic bluetooth connection
-
Hello everyone.
I need to write an app using Bluetooth to data flow. My experience with QT is not very big and I have never used Bluetooth classes before, so I wanted to learn this from some simple example. I chose my BT speaker and wrote some lines of code which should connect my RaspberryPi to the speaker. Unfortunately I get errors I can't deal with.Console output:
pi@raspberrypi:~/Desktop/build-MyApp-Desktop-Debug $ sudo ./MyApp libEGL warning: DRI2: failed to authenticate QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root' Bluetooth works! New device found: "40-1A-94-0C-4D-18" ( "40:1A:94:0C:4D:18" ) New device found: "4B-BB-97-31-66-6A" ( "4B:BB:97:31:66:6A" ) New device found: "4F-C3-B4-14-74-64" ( "4F:C3:B4:14:74:64" ) New device found: "59-42-47-87-38-EA" ( "59:42:47:87:38:EA" ) New device found: "A10" ( "70:78:1A:FF:4C:02" ) qt.bluetooth.bluez: SDP scan failure QProcess::ExitStatus(CrashExit) 9 qt.bluetooth.bluez: SDP search failed for "<Unknown>"
This error is troublesome for me because I don't understand why it appears and how to fix it.
Also, when I add this code line after calling connectToService():qDebug() << socket.error() << endl;
I gets something like that:
QBluetoothSocket::SocketError( -2 )
Which means: QBluetoothSocket::NoSocketError as far as I know, but I can't interpret it properly so it isn't a helping hand.
My Class source file:
#include "btconnection.h" #include <QDebug> #include <QObject> #include <QtBluetooth/QBluetoothDeviceInfo> #include <QtBluetooth/QBluetoothDeviceDiscoveryAgent> #include <QtBluetooth/QBluetoothUuid> #include <QtBluetooth/QBluetoothSocket> BTConnection::BTConnection(QObject *parent) : QObject(parent) { } void BTConnection::init() { localDevice = new QBluetoothLocalDevice(this); if(localDevice->isValid()) { qInfo() << "Bluetooth works!" <<endl; localDevice->powerOn(); localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable); this->startDeviceDiscovery(); } else { qInfo() << "Bluetooth does't works!" <<endl; } } void BTConnection::startDeviceDiscovery() { deviceAgent = new QBluetoothDeviceDiscoveryAgent(this); QObject::connect(deviceAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo))); deviceAgent->start(); } void BTConnection::deviceDiscovered(const QBluetoothDeviceInfo &device) { qInfo()<<"New device found: "<<device.name()<<" ( "<<device.address().toString()<<" )"; if(device.name()=="A10") { deviceAgent->stop(); localDevice->requestPairing(device.address(), QBluetoothLocalDevice::Paired); QBluetoothSocket socket(QBluetoothServiceInfo::RfcommProtocol); QString uuidString = "{0000110e-0000-1000-8000-00805f9b34fb}"; // UUID of the 'A/V Remote Control' service QBluetoothUuid uuid(uuidString); socket.connectToService(device.address(), uuid, QIODevice::ReadWrite); } } BTConnection::~BTConnection() { if(!deviceAgent) delete deviceAgent; if(localDevice!=nullptr) delete localDevice; }
.PRO file
#------------------------------------------------- # # Project created by QtCreator 2019-11-10T22:42:54 # #------------------------------------------------- QT += core gui bluetooth greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = MyApp TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp\ mainwindow.cpp \ btconnection.cpp HEADERS += mainwindow.h \ btconnection.h FORMS += mainwindow.ui
If it will be helpful I can add that when I tried to use another form of "ConnectToService()" method:
connectToService(const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode = ReadWrite)
I also couldn't establish a connection, and trying to read UUID of any service gave it:
00000000-0000-0000-0000-000000000000I would appreciate some tips or any help because I have been looking for a solution for a long time and found nothing.
Version of my RPi is 3B+ Rev 1.3 , Raspbian version is 10 (buster), QT version is 5.11.3