Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt - Connecting to wireless networks
QtWS25 Last Chance

Qt - Connecting to wireless networks

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 5 Posters 11.0k 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.
  • B Offline
    B Offline
    Bob_Walton
    wrote on 23 Oct 2014, 22:07 last edited by
    #1

    I am using Windows7, Qt Creator 3.2.1 and Qt based on Qt 5.3.2 (MSVC 2010, 32 bit)

    My objective at this stage is simple.
    List available WLan networks >> Connect to one >> open QTcpSocket >> Communicate with my custom hardware.

    I am using QNetworkConfigurationManager to list the Networks a choose one. Works ok.

    If I use Windows manually to ‘connect to network’ then open a QTcpSocket socket. Works ok.

    I cannot find anywhere a Qt solution for ‘connect to network’ so I use wlan native api to process the ‘connect to network’ using the SSID chosen from the listed networks and creating an ad-hoc join only BSS type independent connection.

    After the connection is made using a command prompt to ping the target device works ok.

    However using QTcpSocket to open a socket fails.

    Q. Is there a method to accomplish a ‘connect to network’ within Qt?

    Q. If not, after using the wlanapi is there a way of informing Qt about the new connection so that QTcpSocket opens correctly. NB: using updateConfigurations() on QNetworkConfigurationManager does not make a difference.

    Some assistance would be appreciated.

    @//Scan for Wireless networks
    void PCS_MainWindow::on_ButtonNWScan_clicked()
    {
    QIcon icon(":/Icons/PCS_WirelessIcon_01.png");
    QNetworkConfigurationManager PCSmgr;

    ui->listWidgetWScan->clear();
    PCSmgr.allConfigurations().clear();
    PCSmgr.updateConfigurations();

    //REM: Config does not update if interface is removed!

    for(int i=0 ; i < PCSmgr.allConfigurations().count() ; i++)
    {
    if(PCSmgr.allConfigurations().at(i).bearerType() == QNetworkConfiguration::BearerWLAN)
    {
    if(PCSmgr.allConfigurations().at(i).name() != "")
    {
    QListWidgetItem *item = new QListWidgetItem(icon, PCSmgr.allConfigurations().at(i).name(), ui->listWidgetWScan);
    ui->listWidgetWScan->addItem(item);
    }
    }
    }
    }
    @

    @//Connect a network
    void PCS_MainWindow::on_ButtonNWConnect_clicked()
    {
    QNetworkConfigurationManager PCSmgr;
    QNetworkConfiguration PCScfg;
    int gpInt;

    PWLAN_INTERFACE_INFO_LIST pIfList;
    PWLAN_INTERFACE_INFO pIfInfo;
    WLAN_CONNECTION_PARAMETERS wParam;
    DOT11_SSID wSSID;
    HANDLE hClient = NULL;
    DWORD dwCurVersion = 0;
    DWORD dwResult = 0;
    int wIfCount;

    ui->ButtonNWConnect->setEnabled(false);

    //Find the configuration for this SSID
    for(gpInt = 0 ; gpInt < PCSmgr.allConfigurations().count() ; gpInt++)
    {
    if(PCSmgr.allConfigurations().at(gpInt).name() == ui->listWidgetWScan->currentItem()->text())
    {
    PCScfg = PCSmgr.allConfigurations().at(gpInt);
    ui->textSession->setText("<h3>Connecting to Network</h3>");
    ui->textSession->append("SSID: " + PCScfg.name() + "");
    }
    }

    //Open a WINAPI handle for use by the app
    dwResult = WlanOpenHandle(2, NULL, &dwCurVersion, &hClient);
    if(dwResult != ERROR_SUCCESS)
    {
    ui->textSession->append("Opening Handle: Error");
    return;
    }
    else
    {
    ui->textSession->append("Opening Handle: Opened");
    }

    //Build DOT11 SSID
    wSSID.uSSIDLength = PCScfg.name().length();
    QByteArray tSSID = PCScfg.name().toLocal8Bit();
    memcpy(wSSID.ucSSID, tSSID, PCScfg.name().length());

    //Build wlan parameters
    wParam.wlanConnectionMode = wlan_connection_mode_discovery_unsecure;
    wParam.strProfile = NULL;
    wParam.pDot11Ssid = &wSSID;
    wParam.pDesiredBssidList = NULL;
    wParam.dot11BssType = dot11_BSS_type_independent;
    wParam.dwFlags = WLAN_CONNECTION_ADHOC_JOIN_ONLY;

    //Enumerate the interfaces
    dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
    if(dwResult != ERROR_SUCCESS)
    {
    ui->textSession->append("Enumeration: Error");
    return;
    }
    else
    {
    ui->textSession->append("Enumeration: Successful");
    }

    //Allow for multiple wifi adapters by finding the IFace with the correct SSID and conncecting to it
    gpInt = 0;
    wIfCount = pIfList->dwNumberOfItems;
    dwResult = 1;

    while((dwResult != ERROR_SUCCESS) && (wIfCount != 0))
    {
    pIfInfo = (WLAN_INTERFACE_INFO *) &pIfList->InterfaceInfo[gpInt];
    dwResult = WlanConnect(hClient, &pIfInfo->InterfaceGuid, &wParam, NULL);
    gpInt++;
    wIfCount--;
    }

    if(dwResult != ERROR_SUCCESS)
    {
    ui->textSession->append("Connection Status: Error");
    return;
    }
    else
    {
    ui->textSession->append("Connection Status: Connected");
    }

    //Open a socket
    socket = new QTcpSocket(this);

    bool sockState = socket->bind(QHostAddress("192.168.3.10"), 8888);
    if(sockState)
    {
    ui->textSession->append("Socket Bound");
    }
    else
    {
    ui->textSession->append("Socket Not Bound");
    }

    connect(socket,SIGNAL(connected()), this, SLOT(connected()));
    connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(socket,SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
    connect(socket,SIGNAL(readyRead()), this, SLOT(readyRead()));

    // socket->connectToHost("192.168.3.130", 8888);
    socket->connectToHost("192.168.3.130", 8888);

    if(!socket->waitForConnected(1000))
    {
    ui->textSession->append("Socket Connection: Error" + socket->errorString());
    }
    else
    {
    ui->textSession->append("Socket Connection: Connected");
    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ankitlive
      wrote on 6 Nov 2014, 11:12 last edited by
      #2

      hi friend,
      you can easily use QNetworkConfigurationManager and QNetworkSession

      QNetworkConfiguration cfg;
      QNetworkConfigurationManager ncm;
      auto nc = ncm.allConfigurations();

      for (auto &x : nc)
      {
      if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
      {
      if (x.name() == "YouDesiredNetwork")
      cfg = x;
      }
      }

      auto session = new QNetworkSession(cfg, this);
      session->open();

      It first searches for the network with you desired name and then tries to connect to it

      [url=http://www.mygatenow.com/2014/10/shoe-in-money-system-review.html]shoe in money system review[/url]

      S 1 Reply Last reply 7 Jul 2021, 11:57
      1
      • B Offline
        B Offline
        Bill Fleming
        wrote on 11 Mar 2015, 21:49 last edited by
        #3

        When I use QNetworkConfigurationManager and check allConfigurations(), then filter for BearerWLAN. This works on my development laptop and lists the Wi-fi networks in the area.

        But when I try this on another similar laptop it doesn't list anything.
        Both machines have Win7 x86_64 and Intel Wi-Fi cards and I'm using QT 5.4 with MinGW_32 standard install.
        Since QNetworkConfigurationManager pulls up the standardized Windows list I am surprised it would only work on some PCs.

        Anyone know if there is a specific condition to be met for this to work?
        Does the user need admin privileges when running the application or something?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Mar 2015, 21:51 last edited by
          #4

          Hi and welcome to devnet,

          Do you mean that your application is not working on a machine without Qt installed ? If so did you also deploy the bearer plugins with your application ?

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

          B 1 Reply Last reply 12 Mar 2015, 20:04
          0
          • SGaistS SGaist
            11 Mar 2015, 21:51

            Hi and welcome to devnet,

            Do you mean that your application is not working on a machine without Qt installed ? If so did you also deploy the bearer plugins with your application ?

            B Offline
            B Offline
            Bill Fleming
            wrote on 12 Mar 2015, 20:04 last edited by
            #5

            @SGaist After looking into the deployment more I finally figured it out. I needed qgenericbearer.dll and qnativewifibearer.dll in the bearer sub-directory.
            The deployment wiki was a little confusing at first and I thought these needed to be in a plugins sub-directory or plugins/bearer sub-directory.

            Once I figured out how to use it the new windeployqt.exe worked ok and automatically put the plugins where they needed to go so now the application works.

            Thanks for the tip,

            Bill

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 12 Mar 2015, 20:20 last edited by
              #6

              You're welcome !

              Do you mean this article ?

              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
              0
              • A ankitlive
                6 Nov 2014, 11:12

                hi friend,
                you can easily use QNetworkConfigurationManager and QNetworkSession

                QNetworkConfiguration cfg;
                QNetworkConfigurationManager ncm;
                auto nc = ncm.allConfigurations();

                for (auto &x : nc)
                {
                if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
                {
                if (x.name() == "YouDesiredNetwork")
                cfg = x;
                }
                }

                auto session = new QNetworkSession(cfg, this);
                session->open();

                It first searches for the network with you desired name and then tries to connect to it

                S Offline
                S Offline
                Sanskar
                wrote on 7 Jul 2021, 11:57 last edited by Sanskar 7 Jul 2021, 12:00
                #7

                @ankitlive I'm trying to connect to WiFi network using this code, but it is not working also not giving any error

                QNetworkConfiguration cfg;
                QNetworkConfigurationManager ncm;
                auto nc = ncm.allConfigurations();

                for (auto &x : nc)
                {
                if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
                {
                qDebug() << x.name();
                if (x.name() == "YouDesiredNetwork")
                cfg = x;
                }
                }
                auto session = new QNetworkSession(cfg, this);
                session->open();

                also in ubuntu it gives me list of wifi network but when I run this in windows it is showing me below output(I have 5 available networks)
                output: -
                "Wi-Fi"
                "Local Area Connection* 9"

                1 Reply Last reply
                0

                • Login

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