How to connect to two LE peripherals
-
Hi guys,
I've successfully connected my Qt application with a LE embedded device. I can read from it and I can write to it. I'm using Qt Creator 6.3.1 and Windows 11.However my task is to connect to several peripherals.
My question is: do I have to make separate slots for each peripheral or is there another way to do it?
Currently I'm trying to avoid this:
void bluetooth_controller::scan_for_characteristics(qsizetype device_index, qsizetype service_index){ service_object[0] = ble_central->createServiceObject(ble_service_uuids.at(0), this); connect(service_object[0], &QLowEnergyService::stateChanged, this, &bluetooth_controller::on_central_state_changed); connect(service_object[0], &QLowEnergyService::characteristicChanged, this, &bluetooth_controller::on_central_characteristic_changed); connect(service_object[0], &QLowEnergyService::characteristicWritten, this, &bluetooth_controller::on_central_characteristic_written); connect(service_object[0], &QLowEnergyService::errorOccurred, this, &bluetooth_controller::on_central_chracteristic_error); connect(service_object[0], &QLowEnergyService::characteristicRead, this, &bluetooth_controller::on_central_chracteristic_read); connect(service_object[0], &QLowEnergyService::descriptorRead, this, &bluetooth_controller::on_central_chracteristic_descriptor_read); connect(service_object[0], &QLowEnergyService::descriptorWritten, this, &bluetooth_controller::on_central_chracteristic_descriptor_written); service_object[0]->discoverDetails(); service_object[1] = ble_central->createServiceObject(ble_service_uuids.at(1), this); connect(service_object[1], &QLowEnergyService::stateChanged, this, &bluetooth_controller::on_central_state_changed); connect(service_object[1], &QLowEnergyService::characteristicChanged, this, &bluetooth_controller::on_central_characteristic_changed); connect(service_object[1], &QLowEnergyService::characteristicWritten, this, &bluetooth_controller::on_central_characteristic_written); connect(service_object[1], &QLowEnergyService::errorOccurred, this, &bluetooth_controller::on_central_chracteristic_error); connect(service_object[1], &QLowEnergyService::characteristicRead, this, &bluetooth_controller::on_central_chracteristic_read); connect(service_object[1], &QLowEnergyService::descriptorRead, this, &bluetooth_controller::on_central_chracteristic_descriptor_read); connect(service_object[1], &QLowEnergyService::descriptorWritten, this, &bluetooth_controller::on_central_chracteristic_descriptor_written); service_object[1]->discoverDetails(); }given that the objects are:
QLowEnergyController *ble_central = nullptr; QList<QBluetoothDeviceInfo> ble_devices; QList<QBluetoothUuid> ble_service_uuids; QLowEnergyService *service_object[16];Thanks in advance!
L. B. -
Hi @LubomirBogdanov,
You can, if you wish, use the same slots. For example, as you already have:
connect(service_object[0], &QLowEnergyService::stateChanged, this, &bluetooth_controller::on_central_state_changed); ... connect(service_object[1], &QLowEnergyService::stateChanged, this, &bluetooth_controller::on_central_state_changed);Then, in
bluetooth_controller::on_central_state_changed(), you can do something like:void bluetooth_controller::on_central_state_changed(...) { QLowEnergyService * service = qobject_cast<QLowEnergyService *>(sender()); if (service != nullptr) { ... } }See QObject::sender() and qobject_cast for details.
Cheers.
-
Hi guys,
I've successfully connected my Qt application with a LE embedded device. I can read from it and I can write to it. I'm using Qt Creator 6.3.1 and Windows 11.However my task is to connect to several peripherals.
My question is: do I have to make separate slots for each peripheral or is there another way to do it?
Currently I'm trying to avoid this:
void bluetooth_controller::scan_for_characteristics(qsizetype device_index, qsizetype service_index){ service_object[0] = ble_central->createServiceObject(ble_service_uuids.at(0), this); connect(service_object[0], &QLowEnergyService::stateChanged, this, &bluetooth_controller::on_central_state_changed); connect(service_object[0], &QLowEnergyService::characteristicChanged, this, &bluetooth_controller::on_central_characteristic_changed); connect(service_object[0], &QLowEnergyService::characteristicWritten, this, &bluetooth_controller::on_central_characteristic_written); connect(service_object[0], &QLowEnergyService::errorOccurred, this, &bluetooth_controller::on_central_chracteristic_error); connect(service_object[0], &QLowEnergyService::characteristicRead, this, &bluetooth_controller::on_central_chracteristic_read); connect(service_object[0], &QLowEnergyService::descriptorRead, this, &bluetooth_controller::on_central_chracteristic_descriptor_read); connect(service_object[0], &QLowEnergyService::descriptorWritten, this, &bluetooth_controller::on_central_chracteristic_descriptor_written); service_object[0]->discoverDetails(); service_object[1] = ble_central->createServiceObject(ble_service_uuids.at(1), this); connect(service_object[1], &QLowEnergyService::stateChanged, this, &bluetooth_controller::on_central_state_changed); connect(service_object[1], &QLowEnergyService::characteristicChanged, this, &bluetooth_controller::on_central_characteristic_changed); connect(service_object[1], &QLowEnergyService::characteristicWritten, this, &bluetooth_controller::on_central_characteristic_written); connect(service_object[1], &QLowEnergyService::errorOccurred, this, &bluetooth_controller::on_central_chracteristic_error); connect(service_object[1], &QLowEnergyService::characteristicRead, this, &bluetooth_controller::on_central_chracteristic_read); connect(service_object[1], &QLowEnergyService::descriptorRead, this, &bluetooth_controller::on_central_chracteristic_descriptor_read); connect(service_object[1], &QLowEnergyService::descriptorWritten, this, &bluetooth_controller::on_central_chracteristic_descriptor_written); service_object[1]->discoverDetails(); }given that the objects are:
QLowEnergyController *ble_central = nullptr; QList<QBluetoothDeviceInfo> ble_devices; QList<QBluetoothUuid> ble_service_uuids; QLowEnergyService *service_object[16];Thanks in advance!
L. B.@LubomirBogdanov depends,
what I would do is, make a new class, that accepts a QLowEnergyService as an object.
than you can do your connects and operations all in that one. No need for other slots.
If you insist on doing everything inside
bluetooth_controlleryou should consider using a Lambda, as 4th parameter of QObject::connect.
That way you can call the function with additional parameters. For example a pointer to the object. That way you can decide in the function what to doI would however discourage the use of
sender()because violates the object-oriented principle of modularity and the return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object's thread