Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unable to reach "ServiceDiscovered" when try to get BLE characteristics



  • Hi!
    I'm having some problems trying to get the BLE characteristics of a NRF52 DK board. I'm able to connect to the device and to discover all its services using a push button on the GUI. However, once I'm trying to get the characteristics (mService->discoverDetails(), where mService is a QLowEnergyService object), the service first gets the state DiscoveringServices for then getting stuck on DiscoveryRequired; it never reaches the ServiceDiscovered state unless I click the connect button again, which shouldn't happen (clicking the button only once should be enough to connect to the device and use it). Also, between the transition from DiscoveringServices to DiscoveryRequired, I get four times the following issue I don't understand at all:
    "Debugger encountered an exception: Exception at 0x7ffbab653b29, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance)"
    I'm using Qt 5.15.2 (MSVC 2019, 64 bit) on Windows 10. Any help will be very appreciated!

    Here's the code:

    // To connect to the device
    void MainWindow::on_connectBtn_clicked()
    {
        mGoodUuidService = false;
    
        QBluetoothDeviceInfo dev = ui->listWidget->currentItem()->data(Qt::UserRole).value<QBluetoothDeviceInfo>();
        mController = QLowEnergyController::createCentral(dev, this);
        mController->setRemoteAddressType(QLowEnergyController::RandomAddress);
    
        connect(mController, &QLowEnergyController::serviceDiscovered, this, &MainWindow::verifyGoodService);
        connect(mController, &QLowEnergyController::discoveryFinished, this, &MainWindow::bleScanDone);
        connect(mController, SIGNAL(error(QLowEnergyController::Error)),
                    this, SLOT(controllerError(QLowEnergyController::Error)));
        connect(mController, &QLowEnergyController::connected, this, &MainWindow::deviceConnected);
        connect(mController, &QLowEnergyController::disconnected, this, &MainWindow::deviceDisconnected);
    
        mController->connectToDevice();
    }
    
    //To verify we receive the good service
    void MainWindow::verifyGoodService(const QBluetoothUuid &newService)
    {
        if(newService == QBluetoothUuid(QUuid(myUuidService))){
            qDebug() << "Good Service found!";
            mGoodUuidService = true;
        }
    }
    
    // Create service and discover service details
    void MainWindow::bleScanDone()
    {
       if(mService){
           mService->deleteLater();
           mService = nullptr;
       }
    
       if(mGoodUuidService){
           qDebug() << "Connecting to BLE UART service";
           mService = mController->createServiceObject(QBluetoothUuid(QUuid(myUuidService)),this);
    
           if(mService){
               connect(mService, &QLowEnergyService::stateChanged, this, &MainWindow::bleServiceState);
               connect(mService, &QLowEnergyService::characteristicChanged, this, &MainWindow::updateButtonState);
               connect(mService, SIGNAL(descriptorWritten(QLowEnergyDescriptor,QByteArray)),
                           this, SLOT(confirmedDescriptorWrite(QLowEnergyDescriptor,QByteArray)));
               mService->discoverDetails();
            }
       }
    }
    
    // PROBLEM!! To try to get the characteristics service. Unable to get "ServiceDiscovered" unless the connect button is clicked again...
    void MainWindow::bleServiceState(QLowEnergyService::ServiceState newState)
    {
        switch(newState){
            case QLowEnergyService::ServiceDiscovered:{
               // some code....
             }
    
        default:
            break;
        }
    }
    


  • Hey!
    I've faced with the same problem.
    I have ble server device with 2 standard services(0x1800, 0x1801) and 4 custom services. There aren't problems with standard services - I get "ServiceDiscovered" state. But custom services can't reach the same statement. I'm really confused.
    I use PyQt5.15, Win 10.
    There's similar post ( https://forum.qt.io/topic/109630/bluetooth-low-energy-state-servicediscovered-is-never-reached ), but there isn't solving :(



  • Hey @Aleksei_Gusakov !
    I understand your frustration. After several hours of debugging, I found two solutions that are not very efficient (in my humble opinion) but that work. The first one is trying to connect again to the device, that means, create and initialize your client (QLowEnergyController) again after having tried to get the services discovered (when signal QLowEnergyService::stateChanged is triggered). As I said in my post, I get this by clicking twice a push button I programmed to connect to the BLE peripheral, but the same effect can be done by creating and triggering a signal when the service state is "DiscoveryRequired". This custom signal should execute the connecting function and then you should be able to get the "ServiceDiscoverd" state. However, this method causes several warnings in debug mode...The other method, is to use an action (clicking a button for example) to explore the characteristics of your services, as it is showed in this video: https://www.youtube.com/watch?v=drmXdoRu3AQ
    This last method seems to be more stable that the first one (you don't get warnings), but you have to use two buttons: one to connect the device and other to be able to interact with the BLE peripheral service characteristics.

    Hope this solutions could help you in your project.



  • Good morning @JPolo !
    Your solutions helped me! Thanks a lot! I've broken my mind yet
    Although I think it's really strange thing :)


Log in to reply