Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to connect to two LE peripherals
Qt 6.12 LTS is out! See what's new in the release blog

How to connect to two LE peripherals

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 573 Views 1 Watching
  • 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.
  • LubomirBogdanovL Offline
    LubomirBogdanovL Offline
    LubomirBogdanov
    wrote on last edited by
    #1

    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.

    J.HilkJ 1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • LubomirBogdanovL LubomirBogdanov

        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.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @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_controller you 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 do

        I 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

        https://doc.qt.io/qt-6/qobject.html#sender


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1

        • Login

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