Bluetooth Low Energy: State 'ServiceDiscovered' is never reached
-
@Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
Please, consider creating bug report with a minimal but complete reproducer?
Well, as I said, with the Blueooth Low Energy Scanner example from Qt it works - the state "ServiceDiscovered" is reached and I cann retrieve the characteristics of a chosen service from my BLE device. Therefore I think that this might not be a bug but something wrong with my code or something like this..
-
@SpaceToon Well, but scanner example is not a console application, right? As an example on macOS, some parts of QtBluetooth (mostly Bluetooth Classic) will not work in a console application (and for Bluetooth LE you would need a special event dispatcher).
-
@Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
will not work in a console application
Why should it not? You need to make sure Qt event loop is running.
-
@jsulm said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
@Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
will not work in a console application
Why should it not? You need to make sure Qt event loop is running.
So how to make sure Qt event loop is running? Cuase I am new to Qt.
Because The first output of my application with the above code is "\n Current ServiceState: Dsicovering services" and that is right.
When I turn off my Bluetooth then "Default" is printed to the console because the state must then be "InvalidService". So do you mean this by saying "Qt event loop is running"? -
int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); return app->exec(); // This starts the event loop }
-
@SpaceToon Could you, then, post the code here, to check for some simple errors/mistakes?
-
@jsulm said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); return app->exec(); // This starts the event loop }
Yes, I have exactly the same code in my main method.
-
@SpaceToon Could you also connect to the server's 'error' signal to check if any error happens?
-
@Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
@SpaceToon Could you also connect to the server's 'error' signal to check if any error happens?
Hello, could you give me a piece of code for doing this? I do not really know how to do so thank you :)
@Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
@SpaceToon Could you, then, post the code here, to check for some simple errors/mistakes?
Here is the code. It is just for understanding how BLE works in Qt it is not my final code therefore everything is in one file (there are also header files in which the slots, signals etc. are declared):
/*Uuid of the service from which I want to have the characteristics printed*/ const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e")); /*Discovering Bluetooth devices*/ void Scanner::startDeviceDiscovery(){ discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); discoveryAgent->setLowEnergyDiscoveryTimeout(5000); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect); discoveryAgent->start(); } /*Prints the name of the device that is discovered*/ void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device) { qDebug() << "Found new device:" << device.name(); } void Scanner::doConnect(){ /*All devices are listed in a list. When I see my BLE device, I enter the list element and then */ /*create a connection to this certain ble device*/ cout << "\nPlease enter the number of your device: "; int listNumber; cin >> listNumber; deviceList = discoveryAgent->discoveredDevices(); adafruit = &deviceList[listNumber]; qDebug() << "\nThe element that you chose is: " + adafruit->name(); controller = QLowEnergyController::createCentral(*adafruit); connect(controller, &QLowEnergyController::connected, this, &Scanner::startServiceDiscovery); controller->connectToDevice(); } void Scanner::startServiceDiscovery(){ connect(controller, &QLowEnergyController::discoveryFinished, this, &Scanner::discoveryFinished); controller->discoverServices(); } void Scanner::discoveryFinished(){ qDebug() << "\n Service Discovery finished."; QList<QBluetoothUuid> serviceList = controller->services(); QLowEnergyService *uartService = controller->createServiceObject(adafruitServiceUuid, this); connect(uartService, &QLowEnergyService::stateChanged, this, &Scanner::printChars); uartService->discoverDetails(); } void Scanner::printChars(QLowEnergyService::ServiceState newState){ auto service = qobject_cast<QLowEnergyService *>(sender()); switch(newState){ case QLowEnergyService::DiscoveringServices: qDebug() << "\n Current ServiceState: Dsicovering services"; break; case QLowEnergyService::ServiceDiscovered: { qDebug() << "Current ServiceState: ServiceDiscovered"; const QList<QLowEnergyCharacteristic> chars = service->characteristics(); qDebug()<< "Size in printChars Slot" << chars.size(); for (const QLowEnergyCharacteristic &ch : chars) { qDebug() << &ch; } break; } default: qDebug()<<"Default"; break; } }
This is the output:
The connection is successfully created (a blue LED turns on on my BLE device which indicates a succerssfull connection) .
-
@SpaceToon I suggest you to create a bug report (please, attach a complete minimal project you are using). In JIRA we can assign it to Denis (who's the author of this back-end), so maybe at least he has an immediate idea of what's wrong. Thanks.
-
@Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
@SpaceToon I suggest you to create a bug report (please, attach a complete minimal project you are using). In JIRA we can assign it to Denis (who's the author of this back-end), so maybe at least he has an immediate idea of what's wrong. Thanks.
Okay, thanks. I created the Bug report here, but I could not assign it to someone.
-
@SpaceToon Is this issue resolved? We are also facing the same issue in Windows 10. As you mentioned, the BLE scanner example is working perfectly fine.
-
@harip said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:
@SpaceToon Is this issue resolved? We are also facing the same issue in Windows 10. As you mentioned, the BLE scanner example is working perfectly fine.
Hey,
not it is not fixed yet, but a developer is investigating the issue. You can track the current status of the bug here in Jira. -
The QTBluetooth Central API is not supported on any platform.
see the first table in Qt Bluetooth