Optmize connect portal wifi function

This commit is contained in:
chenlelin 2021-03-27 21:00:40 +08:00
parent 505fe36fdc
commit cbb9bb0717
3 changed files with 57 additions and 13 deletions

View File

@ -4,7 +4,7 @@
# #
#------------------------------------------------- #-------------------------------------------------
QT += core gui x11extras dbus KWindowSystem svg concurrent QT += core gui x11extras dbus KWindowSystem svg concurrent network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

View File

@ -1,21 +1,53 @@
#include "wifi-auth-thread.h" #include "wifi-auth-thread.h"
#include <QProcess> #include <QDebug>
WifiAuthThread::WifiAuthThread()
{
//检查连接状态
naManager = new QNetworkAccessManager(this);
// QString url=QString("http://nmcheck.gnome.org/check_network_status.txt");
QString url=QString("http://connectivity-check.ubuntu.com/");
request.setUrl(QUrl(url));
naManager->get(request);
//qDebug()<<"-------------------Network state check!----------------------";
connect(naManager,&QNetworkAccessManager::finished,this,[=](QNetworkReply* reply){
_isReply=true;
QNetworkReply::NetworkError err = reply->error();
//qDebug()<<"----------------NetworkReply error:----------------------"<<err;
if(err==QNetworkReply::NoError)
{
if(!reply->readAll().isEmpty())
_isConnect=false;
}
else
_isConnect=false;
});
}
WifiAuthThread::~WifiAuthThread()
{
delete naManager;
}
void WifiAuthThread::run() void WifiAuthThread::run()
{ {
sleep(3); sleep(3);
QDBusInterface interface( "org.freedesktop.NetworkManager", QDBusInterface interface( "org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager", "/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager", "org.freedesktop.NetworkManager",
QDBusConnection::systemBus() ); QDBusConnection::systemBus() );
QDBusReply<uint> reply= interface.call("CheckConnectivity"); QDBusReply<uint> reply= interface.call("CheckConnectivity");
if (reply.isValid()) { if (reply.isValid()) {
//qDebug()<<"---------------------wifi type:----------------------"<<reply.value();
if (reply.value() == 2) { if (reply.value() == 2) {
//system("xdg-open http://nmcheck.gnome.org"); while(1)
QProcess::startDetached(QString("xdg-open http://nmcheck.gnome.org")); {
if(!_isConnect)
QProcess::startDetached(QString("xdg-open http://nmcheck.gnome.org"));
if(_isReply)
break;
}
} }
} }
} }

View File

@ -1,6 +1,10 @@
#ifndef WIFI_AUTH_THREAD_H #ifndef WIFI_AUTH_THREAD_H
#define WIFI_AUTH_THREAD_H #define WIFI_AUTH_THREAD_H
#include <QThread> #include <QThread>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QProcess>
#include <QDBusInterface> #include <QDBusInterface>
#include <QDBusReply> #include <QDBusReply>
@ -8,6 +12,14 @@ class WifiAuthThread:public QThread
{ {
protected: protected:
virtual void run(); virtual void run();
QNetworkRequest request;
QNetworkAccessManager* naManager=nullptr;
bool _isConnect=true;//是否已联网
bool _isReply=false;//联网状态检查是否收到回复
public:
WifiAuthThread();
~WifiAuthThread();
}; };
#endif //WIFI_AUTH_THREAD_H #endif //WIFI_AUTH_THREAD_H