connectToDevice in QLowEnergyController not working



  • Re: QLowEnergyController not working

    Hi everyone I am trying to make my custom application for connecting to bluetooth low energy however the connectToDevice() function doesn't work fine in my custom application
    ( works from scanner example application everytime) I don't know what could be going wrong, its practically the same as in examples.

    Here is my code I am using smart pointers though I don't think that is the problem:

    #include "lowenergyhandle.h"
    #include <QDebug>
    
    LowEnergyHandle::LowEnergyHandle() :
        QObject(),
        address_toFind("00:1E:C0:41:F6:4A"),
        deviceAgent(new QBluetoothDeviceDiscoveryAgent(this)),
        LEdevice(new QBluetoothDeviceInfo())
    {
        qDebug()<<"Creating Low Energy Hanle ";
    
        //SIGNALS AND SLOTS BEGIN
        connect(deviceAgent.get(), &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,this, &LowEnergyHandle::addDevice);
        connect(deviceAgent.get(), &QBluetoothDeviceDiscoveryAgent::finished, this,[](){
            qDebug()<<"Scan was finished";
        });
        connect(deviceAgent.get(), &QBluetoothDeviceDiscoveryAgent::canceled, this,[](){
            qDebug()<<"Scan was canceled";
        });
        connect(this, SIGNAL(foundDevice()), this, SLOT(connectToDevice()));
        //SIGNALS AND SLOTS END
    }
    
    void LowEnergyHandle::startScan()
    {
        qDebug()<<"LEbluetooth Device scan started";
        qDebug()<<"Searching for LEbluetooth Device with address "<<address_toFind;
        deviceAgent->setLowEnergyDiscoveryTimeout(5000);
        deviceAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    }
    
    QString LowEnergyHandle::getAddress_toFind() const
    {
        return address_toFind;
    }
    
    void LowEnergyHandle::setAddress_toFind(const QString &value)
    {
        address_toFind = value;
    }
    
    
    void LowEnergyHandle::addDevice(const QBluetoothDeviceInfo &device)
    {
        if(device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration){
            if(device.address().toString() == address_toFind){
                qDebug()<<"LEbluetooth Device found with address"<<device.address().toString()
                               <<" and name"<<device.name()<<endl;
                LEdevice->operator =(device);
                deviceAgent->stop();
                emit foundDevice();
            }
        }
    }
    
    void LowEnergyHandle::connectToDevice()
    {
        qDebug()<<"attempting to connect to Device";
        std::unique_ptr<QLowEnergyController>
                leController(new QLowEnergyController(*LEdevice.get(),this));
    
    
        //SIGNALS AND SLOTS BEGIN
        connect(leController.get(), &QLowEnergyController::connected, this, [this, &leController](){
           qDebug()<<"Connected to LEbluetooth device";
           leController->discoverServices();
        });
    
        connect(leController.get(), &QLowEnergyController::disconnected, this, [this]() {
            qDebug()<<"Disconnected from LEbluetooth Device";
        });
    
        connect(leController.get(), static_cast<void (QLowEnergyController::*)
                (QLowEnergyController::Error)>(&QLowEnergyController::error),
                    this, [this]() {
                qDebug()<<"Cannot connect to LEbluetooth Device";
        });
    
        connect(leController.get(), &QLowEnergyController::serviceDiscovered, this, &LowEnergyHandle::serviceDiscovered);
        //SIGNALS AND SLOTS END
    
        leController->setRemoteAddressType(QLowEnergyController::PublicAddress);
        leController->connectToDevice();
    }
    

    Here you can see the code in github: https://github.com/PaulMaxAvalosAguilar/Encoder/tree/master/QtCode/Tests/LowEnergyTest

    Please someone help me this is quite important!
    Thanks in advance


  • Moderators

    So in the past I have tried doing bluetooth work with Qt. It's bluetooth support is awful. Things wouldn't work properly, but examples would. So when I dug into the code I found entire parts that were incomplete/uncoded ifs(), etc.

    This was around 4 years ago.

    I have since learned that Qt is a great GUI library and a horrible hardware one. You may need to go to raw bluetooth like I had to using bluez or something (this was for linux).

    Another thing, some devices just don't work without special help. I had a credit card terminal that just refused to connect to bluetooth properly. I can't remember what I ended up having to do to get it connected but it definitely never worked with Qt.

    Moving on to the actual problem:

    1. Can the scanner example actually connect to your specific device, but your code can not?
    2. Is anything output on the terminal when running? Qt likes to log debug info to the terminal.
    3. Have you stepped through the code while running (into Qt) and seen what it was doing differently between the scanner example and your code?
    4. Smart pointers are fine as long as you implemented them correctly and they aren't going out of scope or something.


  • HI @ambershark Thanks for replying and taking the time! Yes the scanner can actually connect, look at services and characteristics, actually it worked like once or twice with my code but after recompiling and deploying it didn't work despite that I even did the same with the example scanner and it always worked.
    The output message in my code is the same than with example: using bluez d bus or something and that CAP_NET was not set, the code is identical and worked once after some time which also makes me question qt reliability in this matter but I don't know I am in raspberry pi 3 and using a console application so I am planning on completely adapt the scanner example to console and look if it works


  • Qt Champions 2017

    Hi @PaulMax,

    actually it worked like once or twice with my code but after recompiling and deploying it didn't work despite that

    Can you explain that a bit more? When did it work? When running from QtCreator? Where did you run? On the Pi or on your computer?

    In the worst case it is a simple deployment problem and some library or plugin is missing on the Pi.

    Your statement

    I even did the same with the example scanner and it always worked.

    is exclusive or to

    which also makes me question qt reliability in this matter

    If the example always works then something is wrong with your program.

    Regards


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.