Application crashed when I stop bluetooth scanning



  • Hi,
    I have an application where I want to communicate with BLE device. At the begin I start scanning. I would like stop scanning as soon as I have found my device. But when I do this, my application crashed.
    I have no problem when I wait end of scanning.
    Have you some ideas ?

    #include "myapp.h"
    #include <QCoreApplication>
    
    MyApp::MyApp()
        : m_deviceDiscoveryAgent(0)
        , m_controller(0)
        , m_service(0)
    {
        m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    
        connect(m_deviceDiscoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
                this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
        connect(m_deviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
                this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
        connect(m_deviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
        connect(m_deviceDiscoveryAgent, SIGNAL(canceled()), this, SLOT(scanCancel()));
    
        qDebug()<<("Scanning for devices...");
        m_deviceDiscoveryAgent->start();
    
    }
    
    MyApp::~MyApp()
    {
        delete m_deviceDiscoveryAgent;
        delete m_controller;
        delete m_service;
    }
    
    void MyApp::addDevice(const QBluetoothDeviceInfo &device)
    {
        if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
            qDebug()<<device.address().toString();
            if(device.address().toString() == "xx:xx:xx:xx:xx:xx")
            {
                qDebug()<<">>> device found !"<<m_deviceDiscoveryAgent;
                m_deviceDiscoveryAgent->stop();
               // connectDevice(device);
    
            }
        }
    }
    
    void MyApp::scanFinished()
    {
        qDebug()<<"scanFinished()";
    }
    
    void MyApp::scanCancel()
    {
        qDebug()<<"scan cancel";
    }
    
    void MyApp::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
    {
        if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
            qDebug()<<("The Bluetooth adaptor is powered off, power it on before doing discovery.");
        else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
            qDebug()<<("Writing or reading from the device resulted in an error.");
        else
            qDebug()<<("An unknown error has occurred.");
    }
    
    void MyApp::deviceConnected()
    {
        qDebug()<<"deviceConnected, request discoverServices";
        m_controller->discoverServices();
    }
    
    void MyApp::deviceDisconnected()
    {
        qWarning() << "Remote device disconnected";
    }
    
    void MyApp::connectDevice(const QBluetoothDeviceInfo& device)
    {
        qDebug()<<"connectDevice";
    
        if (m_controller!=NULL) {
            m_controller->disconnectFromDevice();
            delete m_controller;
            m_controller = 0;
        }
    
        m_controller = new QLowEnergyController(device.address(),
                                                this);
        connect(m_controller, SIGNAL(error(QLowEnergyController::Error)),
                this, SLOT(controllerError(QLowEnergyController::Error)));
        connect(m_controller, SIGNAL(connected()),
                this, SLOT(deviceConnected()));
        connect(m_controller, SIGNAL(disconnected()),
                this, SLOT(deviceDisconnected()));
        connect(m_controller, SIGNAL(serviceDiscovered(QBluetoothUuid)),
                this, SLOT(serviceDiscovered(QBluetoothUuid)));
        connect(m_controller, SIGNAL(discoveryFinished()),
                this, SLOT(serviceScanDone()));
        m_controller->connectToDevice();
    }
    

  • Lifetime Qt Champion

    Hi,

    Where exactly does it crash ?

    Note that you are missing a null pointer check for m_controller in deviceConnected



  • It crash just after slots "scanCancel()".
    This is output (console application):

    Scanning for devices...
    "xx:xx:xx:xx:xx:xa"
    "xx:xx:xx:xx:xx:xb"
    "xx:xx:xx:xx:xx:xx"
    >>> device found ! QBluetoothDeviceDiscoveryAgent(0x23dc7c0)
    scan cancel
    Appuyez sur <ENTRÉE> pour fermer cette fenêtre...
    

    and on console :

    Starting of /path/of/MyAppTest...
    /path/of/MyAppTest crashed
    ```.

  • Lifetime Qt Champion

    Can you get a stack trace for the crash ?

    By the way, what OS and Qt version are you running ?


Log in to reply
 

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