Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Application crashed when I stop bluetooth scanning
QtWS25 Last Chance

Application crashed when I stop bluetooth scanning

Scheduled Pinned Locked Moved Unsolved General and Desktop
qbluetoothdevic
4 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    helenebro
    wrote on last edited by helenebro
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Where exactly does it crash ?

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        helenebro
        wrote on last edited by
        #3

        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
        ```.
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you get a stack trace for the crash ?

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved