Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. BluetoothLowEnergy: Application for connecting to 2 devices with as little duplicate code as possible

BluetoothLowEnergy: Application for connecting to 2 devices with as little duplicate code as possible

Scheduled Pinned Locked Moved Unsolved General and Desktop
bluetooth low ebest practice
1 Posts 1 Posters 202 Views
  • 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.
  • T Offline
    T Offline
    TUStudi
    wrote on 17 May 2020, 15:09 last edited by
    #1

    Hi,
    I am relatively new to C ++ and Qt, but so far I have been able to create my own application (with QT Widgets GUI), with which I can connect to a Bluetooth Low Energy device and transfer data. I would now like to make it possible to do the whole thing for 2 devices at the same time. I simply copied all the code and added "_secondDevice" to the end of the variables and function names, for example, just to see if it worked and it does. Now I want to keep my code clear and avoid duplicate code.

    I have all the functionality for connecting in a class called "Connector". This class includes DeviceDsicovery, ServicesDiscovery, CharacteristicsDsicovery. To make the whole thing independent of the Ui I call all these functions from my MainWindow class. This means that when a user clicks on the "Scan" button, the "startDiscovery" function is called up in this slot in MainWindow.

    I thought that e.g. instead of two separate functions "startDiscovery" for both devices have a single generic function. But this function have i.e. three SLOTS which also have to be generic - so the only idea I have is to give the scanDevce function a parameter "device (first or second) and in the function I evaluate if(firstDevcie) then call Slot1,2,3 else call Sot 4,5,6 but this is I think the same bad coding as having 2 separate functions for both. (The following is from this example)

    void Connector::startDeviceDiscovery()
    {
    m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000);
    
    connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &DeviceFinder::addDevice);
    connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &DeviceFinder::scanFinished);
    connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &DeviceFinder::scanFinished);
    
    m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    }
    

    Or for example this function:

    void Connector::connect()
    {
    m_control = QLowEnergyController::createCentral(m_currentDevice->getDevice(), this);
    
    connect(m_control, &QLowEnergyController::serviceDiscovered,
            this, &DeviceHandler::serviceDiscovered);
    connect(m_control, &QLowEnergyController::discoveryFinished,
            this, &DeviceHandler::serviceScanDone);
    
    connect(m_control, &QLowEnergyController::connected, this, [this]() {
        setInfo("Controller connected. Search services...");
        m_control->discoverServices();
    });
    connect(m_control, &QLowEnergyController::disconnected, this, [this]() {
        setError("LowEnergy controller disconnected");
    });
    
    // Connect
    m_control->connectToDevice();
    }
    

    So does anyone have tips on how I can best design the whole thing without copying everything?

    Thank you very much!

    1 Reply Last reply
    0

    1/1

    17 May 2020, 15:09

    • Login

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