Qt - Connecting to wireless networks
-
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");
}
@ -
hi friend,
you can easily use QNetworkConfigurationManager and QNetworkSessionQNetworkConfiguration 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
-
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? -
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 ?
-
@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
-
@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"