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. Connecting to secondary WLAN interface
QtWS25 Last Chance

Connecting to secondary WLAN interface

Scheduled Pinned Locked Moved Solved General and Desktop
networkqnetworksessionwifi connection
10 Posts 5 Posters 2.2k 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.
  • N Offline
    N Offline
    NeuroSamuel
    wrote on 27 May 2019, 11:09 last edited by
    #1

    My computer has 2 WLAN interfaces: One integrated ("Wi-Fi") and an external adapter ("Wi-Fi 2"). How can I make my Qt application establish a connection to a given access point (AP) through the second interface?

    I want to make sure that the first interface is always used to connect to the internet (my ISP router), while the second interface is used to connect to a third-party device (which is a Wi-Fi AP itself).

    The following code connects to an AP (SSID = DeviceRouter). But I don't know how to force it to use my secondary interface (Wi-Fi 2) instead.

    // My target SSID and interface (let's assume this is an open AP)
    QString ssid = "DeviceRouter";
    QString interface = "Wi-Fi 2";  // I want to use my secondary interface
    
    // Get all configurations
    QNetworkConfigurationManager mgr;
    mgr.updateConfigurations();
    waitForSignal( &mgr, SIGNAL( updateCompleted() ), 20000 ); // implemented elsewhere
    QList<QNetworkConfiguration> allConfigs = mgr.allConfigurations();
    
    // Select the configuration matching my target SSID
    bool connected = false;
    foreach( QNetworkConfiguration config, allConfigs ) {
        if( config.name() == ssid && config.bearerType == QNetworkConfiguration::BearerWLAN ) {
            QNetworkSession s( config );
            // How can I set the interface for the session?
            s.open();
            connected = s.waitForOpened( 30000 );
            if( connected ) {
                qDebug() << "You're connected on interface: " << s.interface().humanReadableName();
            }
            break;
        }
    }
    

    The above code always reports: You're connected on interface: Wi-Fi, as expected. But, how can I specify Wi-Fi 2 as my desired interface?

    Any help/hint would be greatly appreciated.

    P A 2 Replies Last reply 27 May 2019, 19:26
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 May 2019, 19:06 last edited by SGaist
      #2

      Hi and welcome to devnet,

      AFAIK, that's done on your system level and is outside of Qt's scope.

      You can select the configuration you want to use but is not meant as a system network configuration manager.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • N NeuroSamuel
        27 May 2019, 11:09

        My computer has 2 WLAN interfaces: One integrated ("Wi-Fi") and an external adapter ("Wi-Fi 2"). How can I make my Qt application establish a connection to a given access point (AP) through the second interface?

        I want to make sure that the first interface is always used to connect to the internet (my ISP router), while the second interface is used to connect to a third-party device (which is a Wi-Fi AP itself).

        The following code connects to an AP (SSID = DeviceRouter). But I don't know how to force it to use my secondary interface (Wi-Fi 2) instead.

        // My target SSID and interface (let's assume this is an open AP)
        QString ssid = "DeviceRouter";
        QString interface = "Wi-Fi 2";  // I want to use my secondary interface
        
        // Get all configurations
        QNetworkConfigurationManager mgr;
        mgr.updateConfigurations();
        waitForSignal( &mgr, SIGNAL( updateCompleted() ), 20000 ); // implemented elsewhere
        QList<QNetworkConfiguration> allConfigs = mgr.allConfigurations();
        
        // Select the configuration matching my target SSID
        bool connected = false;
        foreach( QNetworkConfiguration config, allConfigs ) {
            if( config.name() == ssid && config.bearerType == QNetworkConfiguration::BearerWLAN ) {
                QNetworkSession s( config );
                // How can I set the interface for the session?
                s.open();
                connected = s.waitForOpened( 30000 );
                if( connected ) {
                    qDebug() << "You're connected on interface: " << s.interface().humanReadableName();
                }
                break;
            }
        }
        

        The above code always reports: You're connected on interface: Wi-Fi, as expected. But, how can I specify Wi-Fi 2 as my desired interface?

        Any help/hint would be greatly appreciated.

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 27 May 2019, 19:26 last edited by
        #3

        @NeuroSamuel from the code snippet you pasted, it looks to me that you're always only checking the first QNetworkConfiguration available, because of the break statement that exits the foreach() loop... Am I missing something?

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • N NeuroSamuel
          27 May 2019, 11:09

          My computer has 2 WLAN interfaces: One integrated ("Wi-Fi") and an external adapter ("Wi-Fi 2"). How can I make my Qt application establish a connection to a given access point (AP) through the second interface?

          I want to make sure that the first interface is always used to connect to the internet (my ISP router), while the second interface is used to connect to a third-party device (which is a Wi-Fi AP itself).

          The following code connects to an AP (SSID = DeviceRouter). But I don't know how to force it to use my secondary interface (Wi-Fi 2) instead.

          // My target SSID and interface (let's assume this is an open AP)
          QString ssid = "DeviceRouter";
          QString interface = "Wi-Fi 2";  // I want to use my secondary interface
          
          // Get all configurations
          QNetworkConfigurationManager mgr;
          mgr.updateConfigurations();
          waitForSignal( &mgr, SIGNAL( updateCompleted() ), 20000 ); // implemented elsewhere
          QList<QNetworkConfiguration> allConfigs = mgr.allConfigurations();
          
          // Select the configuration matching my target SSID
          bool connected = false;
          foreach( QNetworkConfiguration config, allConfigs ) {
              if( config.name() == ssid && config.bearerType == QNetworkConfiguration::BearerWLAN ) {
                  QNetworkSession s( config );
                  // How can I set the interface for the session?
                  s.open();
                  connected = s.waitForOpened( 30000 );
                  if( connected ) {
                      qDebug() << "You're connected on interface: " << s.interface().humanReadableName();
                  }
                  break;
              }
          }
          

          The above code always reports: You're connected on interface: Wi-Fi, as expected. But, how can I specify Wi-Fi 2 as my desired interface?

          Any help/hint would be greatly appreciated.

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 28 May 2019, 03:41 last edited by
          #4

          @NeuroSamuel said in Connecting to secondary WLAN interface:

          QString interface = "Wi-Fi 2";

          You define this string at the beginning of your code, but don't use it to actually check the interface. Together with the break @Pablo-J-Rogina already mentioned, you never get to interface 2.

          By the way:

          waitForSignal( &mgr, SIGNAL( updateCompleted() ), 20000 );

          connected = s.waitForOpened( 30000 );

          Unless you run this code in a thread, this already screams for problems. Don't use waitFor... in the main thread, please.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          4
          • N Offline
            N Offline
            NeuroSamuel
            wrote on 29 May 2019, 08:04 last edited by
            #5

            Thank you guys, for your comments and hints. Really appreciated!

            @aha_1980 I'm aware of your point regarding the use of waitFor... in the main thread. This was just a sample minimal code to help explain what I want to achieve.

            @Pablo-J-Rogina and @aha_1980 You're right, the break in the foreach(...) loop will make me connect to the first interface. But, again, this is just sample code (not a very good one, though), not the real implementation. If I remove the break, nothing changes: I only get connected to one network, through the first WLAN interface. The origin of the problem here is that I would expect QNetworkConfigurationManager::allConfigurations() to return two configurations for each SSID: one for each interface. Then, I could open a QNetworkSession with the one I want. But instead, this function returns one configuration for each SSID. No way to specify the interface I want to use.

            I think @SGaist is right. Such fine manipulation of network connections is probably out of Qt's scope. So, I will go for a system-specific implementation. I will try with Native Wifi in Windows and probably Core WLAN for MacOS.

            J 1 Reply Last reply 29 May 2019, 08:08
            0
            • N NeuroSamuel
              29 May 2019, 08:04

              Thank you guys, for your comments and hints. Really appreciated!

              @aha_1980 I'm aware of your point regarding the use of waitFor... in the main thread. This was just a sample minimal code to help explain what I want to achieve.

              @Pablo-J-Rogina and @aha_1980 You're right, the break in the foreach(...) loop will make me connect to the first interface. But, again, this is just sample code (not a very good one, though), not the real implementation. If I remove the break, nothing changes: I only get connected to one network, through the first WLAN interface. The origin of the problem here is that I would expect QNetworkConfigurationManager::allConfigurations() to return two configurations for each SSID: one for each interface. Then, I could open a QNetworkSession with the one I want. But instead, this function returns one configuration for each SSID. No way to specify the interface I want to use.

              I think @SGaist is right. Such fine manipulation of network connections is probably out of Qt's scope. So, I will go for a system-specific implementation. I will try with Native Wifi in Windows and probably Core WLAN for MacOS.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 29 May 2019, 08:08 last edited by jsulm
              #6

              @NeuroSamuel You don't use "interface" variable anywhere! And your if() statement simply matches the first interface and connects to that one.

              if( config.name() == ssid && config.bearerType == QNetworkConfiguration::BearerWLAN )
              

              this matches already first one I guess...

              Did you check how many elements allConfigs contains?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              N 1 Reply Last reply 4 Jun 2019, 10:37
              1
              • J jsulm
                29 May 2019, 08:08

                @NeuroSamuel You don't use "interface" variable anywhere! And your if() statement simply matches the first interface and connects to that one.

                if( config.name() == ssid && config.bearerType == QNetworkConfiguration::BearerWLAN )
                

                this matches already first one I guess...

                Did you check how many elements allConfigs contains?

                N Offline
                N Offline
                NeuroSamuel
                wrote on 4 Jun 2019, 10:37 last edited by
                #7

                @jsulm Yes, I did check allConfigs. And it contains only one instance of each available SSID.

                J 1 Reply Last reply 4 Jun 2019, 10:38
                0
                • N NeuroSamuel
                  4 Jun 2019, 10:37

                  @jsulm Yes, I did check allConfigs. And it contains only one instance of each available SSID.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 4 Jun 2019, 10:38 last edited by
                  #8

                  @NeuroSamuel said in Connecting to secondary WLAN interface:

                  And it contains only one instance of each available SSID

                  So, two of them since you have two SSIDs?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  N 1 Reply Last reply 5 Jun 2019, 15:52
                  0
                  • J jsulm
                    4 Jun 2019, 10:38

                    @NeuroSamuel said in Connecting to secondary WLAN interface:

                    And it contains only one instance of each available SSID

                    So, two of them since you have two SSIDs?

                    N Offline
                    N Offline
                    NeuroSamuel
                    wrote on 5 Jun 2019, 15:52 last edited by
                    #9

                    @jsulm Yes, allConfigs contains two QNetworkConfigurations: If I inspect their name(), I see that they correspond to the SSID's of the available access points (say, HomeRouter and DeviceRouter).

                    The following code:

                    QList<QNetworkConfiguration> allConfigs = QNetworkConfigurationManager::allConfigurations();
                    foreach( QNetworkConfiguration config, allConfigs ) {
                        if( config.bearerType == QNetworkConfiguration::BearerWLAN )
                            qDebug() << "*" << config.name();
                    }
                    

                    returns:

                    * HomeRouter
                    * DeviceRouter
                    

                    My problem is that I see no way to open a QNetworkSession from one of those configurations choosing the hardware interface. I wish there was a method like

                    QNetworkSession s( allConfigs[1], "Wifi 2");    //  <-- this constructor doesn't exist
                    

                    But QNetworkSession does not provide that functionality and I can't find any OS-independent workaround.

                    P 1 Reply Last reply 5 Jun 2019, 16:08
                    0
                    • N NeuroSamuel
                      5 Jun 2019, 15:52

                      @jsulm Yes, allConfigs contains two QNetworkConfigurations: If I inspect their name(), I see that they correspond to the SSID's of the available access points (say, HomeRouter and DeviceRouter).

                      The following code:

                      QList<QNetworkConfiguration> allConfigs = QNetworkConfigurationManager::allConfigurations();
                      foreach( QNetworkConfiguration config, allConfigs ) {
                          if( config.bearerType == QNetworkConfiguration::BearerWLAN )
                              qDebug() << "*" << config.name();
                      }
                      

                      returns:

                      * HomeRouter
                      * DeviceRouter
                      

                      My problem is that I see no way to open a QNetworkSession from one of those configurations choosing the hardware interface. I wish there was a method like

                      QNetworkSession s( allConfigs[1], "Wifi 2");    //  <-- this constructor doesn't exist
                      

                      But QNetworkSession does not provide that functionality and I can't find any OS-independent workaround.

                      P Offline
                      P Offline
                      Pablo J. Rogina
                      wrote on 5 Jun 2019, 16:08 last edited by
                      #10

                      @NeuroSamuel said in Connecting to secondary WLAN interface:

                      My problem is that I see no way to open a QNetworkSession from one of those configurations choosing the hardware interface.

                      Is not something you (or your sysadmin) did already when configuring the networking devices for your system?
                      Not that I have that much experience with QNetworkSession, but it looks that behavior to be fine, given that you are setting one WLAN interface to one SSID at OS level. Thinking of wpa_supplicant on Linux for instance:
                      wpa_supplicant.conf

                      network={
                              ssid="home"
                              scan_ssid=1
                              key_mgmt=WPA-PSK
                              psk="very secret passphrase"
                      }
                      

                      and then command line:

                      wpa_supplicant -iwlan0 -c/etc/wpa_supplicant.conf -d
                      

                      Because at the end it'll be a question of OS routing, won't it?

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      2

                      10/10

                      5 Jun 2019, 16:08

                      • Login

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