<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to connect to two LE peripherals]]></title><description><![CDATA[<p dir="auto">Hi guys,<br />
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.</p>
<p dir="auto">However my task is to connect to several peripherals.</p>
<p dir="auto">My question is: do I have to make separate slots for each peripheral or is there another way to do it?</p>
<p dir="auto">Currently I'm trying to avoid this:</p>
<pre><code>void bluetooth_controller::scan_for_characteristics(qsizetype device_index, qsizetype service_index){
    
    service_object[0] = ble_central-&gt;createServiceObject(ble_service_uuids.at(0), this);

    connect(service_object[0], &amp;QLowEnergyService::stateChanged, this, &amp;bluetooth_controller::on_central_state_changed);
    connect(service_object[0], &amp;QLowEnergyService::characteristicChanged, this, &amp;bluetooth_controller::on_central_characteristic_changed);
    connect(service_object[0], &amp;QLowEnergyService::characteristicWritten, this, &amp;bluetooth_controller::on_central_characteristic_written);
    connect(service_object[0], &amp;QLowEnergyService::errorOccurred, this, &amp;bluetooth_controller::on_central_chracteristic_error);
    connect(service_object[0], &amp;QLowEnergyService::characteristicRead, this, &amp;bluetooth_controller::on_central_chracteristic_read);
    connect(service_object[0], &amp;QLowEnergyService::descriptorRead, this, &amp;bluetooth_controller::on_central_chracteristic_descriptor_read);
    connect(service_object[0], &amp;QLowEnergyService::descriptorWritten, this, &amp;bluetooth_controller::on_central_chracteristic_descriptor_written);

    service_object[0]-&gt;discoverDetails();

    service_object[1] = ble_central-&gt;createServiceObject(ble_service_uuids.at(1), this);

    connect(service_object[1], &amp;QLowEnergyService::stateChanged, this, &amp;bluetooth_controller::on_central_state_changed);
    connect(service_object[1], &amp;QLowEnergyService::characteristicChanged, this, &amp;bluetooth_controller::on_central_characteristic_changed);
    connect(service_object[1], &amp;QLowEnergyService::characteristicWritten, this, &amp;bluetooth_controller::on_central_characteristic_written);
    connect(service_object[1], &amp;QLowEnergyService::errorOccurred, this, &amp;bluetooth_controller::on_central_chracteristic_error);
    connect(service_object[1], &amp;QLowEnergyService::characteristicRead, this, &amp;bluetooth_controller::on_central_chracteristic_read);
    connect(service_object[1], &amp;QLowEnergyService::descriptorRead, this, &amp;bluetooth_controller::on_central_chracteristic_descriptor_read);
    connect(service_object[1], &amp;QLowEnergyService::descriptorWritten, this, &amp;bluetooth_controller::on_central_chracteristic_descriptor_written);

    service_object[1]-&gt;discoverDetails();
}
</code></pre>
<p dir="auto">given that the objects are:</p>
<pre><code>    QLowEnergyController *ble_central = nullptr;
    QList&lt;QBluetoothDeviceInfo&gt; ble_devices;
    QList&lt;QBluetoothUuid&gt; ble_service_uuids;
    QLowEnergyService *service_object[16];
</code></pre>
<p dir="auto">Thanks in advance!<br />
L. B.</p>
]]></description><link>https://forum.qt.io/topic/139523/how-to-connect-to-two-le-peripherals</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 08:42:38 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139523.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 23 Sep 2022 12:18:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to connect to two LE peripherals on Fri, 23 Sep 2022 12:33:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lubomirbogdanov">@<bdi>LubomirBogdanov</bdi></a> depends,</p>
<p dir="auto">what I would do is, make a new class, that accepts a QLowEnergyService as an object.</p>
<p dir="auto">than you can do your connects and operations all in that one. No need for other slots.</p>
<p dir="auto">If you insist on doing everything inside <code>bluetooth_controller </code> you should consider using a Lambda, as 4th parameter of QObject::connect.<br />
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</p>
<p dir="auto">I would however discourage the use of <code>sender()</code> 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</p>
<p dir="auto"><a href="https://doc.qt.io/qt-6/qobject.html#sender" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qobject.html#sender</a></p>
]]></description><link>https://forum.qt.io/post/729940</link><guid isPermaLink="true">https://forum.qt.io/post/729940</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Fri, 23 Sep 2022 12:33:30 GMT</pubDate></item><item><title><![CDATA[Reply to How to connect to two LE peripherals on Fri, 23 Sep 2022 12:27:45 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/lubomirbogdanov">@<bdi>LubomirBogdanov</bdi></a>,</p>
<p dir="auto">You can, if you wish, use the same slots.  For example, as you already have:</p>
<pre><code>connect(service_object[0], &amp;QLowEnergyService::stateChanged, this, &amp;bluetooth_controller::on_central_state_changed);
...
connect(service_object[1], &amp;QLowEnergyService::stateChanged, this, &amp;bluetooth_controller::on_central_state_changed);
</code></pre>
<p dir="auto">Then, in <code>bluetooth_controller::on_central_state_changed()</code>, you can do something like:</p>
<pre><code>void bluetooth_controller::on_central_state_changed(...)
{
    QLowEnergyService * service = qobject_cast&lt;QLowEnergyService *&gt;(sender());
    if (service != nullptr) {
        ...
    }
}
</code></pre>
<p dir="auto">See <a href="https://doc.qt.io/qt-6/qobject.html#sender" target="_blank" rel="noopener noreferrer nofollow ugc">QObject::sender()</a> and <a href="https://doc.qt.io/qt-6/qobject.html#qobject_cast" target="_blank" rel="noopener noreferrer nofollow ugc">qobject_cast</a> for details.</p>
<p dir="auto">Cheers.</p>
]]></description><link>https://forum.qt.io/post/729938</link><guid isPermaLink="true">https://forum.qt.io/post/729938</guid><dc:creator><![CDATA[Paul Colby]]></dc:creator><pubDate>Fri, 23 Sep 2022 12:27:45 GMT</pubDate></item></channel></rss>