From de8e3d4ca9cdb0e218560b1665cd16bf572c33aa Mon Sep 17 00:00:00 2001 From: andrew-wuhan-huawei-990-pc Date: Thu, 25 Mar 2021 21:49:36 +0800 Subject: [PATCH] Add function to reconnect the best wifi after disconnect a wifi --- src/mainwindow.cpp | 130 +++++++++++++++++++++++++++++++++++++------- src/mainwindow.h | 8 ++- src/oneconnform.cpp | 12 ++++ 3 files changed, 128 insertions(+), 22 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 496413f5..14c6768e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1699,9 +1699,20 @@ void MainWindow::getWifiListDone(QStringList slist) } } - if (current_wifi_list_state == RECONNECT_WIFI) { - wifiListOptimize(slist); + if (isHuaWeiPC) { + if (slist.size() >= 2) { + wifiListOptimize(slist); + getFinalWifiList(slist); + } + } + qDebug()<<"------------"; + foreach (QString str, slist) { + qDebug()< freq) { + if ((signal-conSignal) > 50) { //低频的信号格数比高频大于两格,选低频 + deleteWifiStr.append(wifiInfo); + } else { + deleteWifiStr.append(wifiStr); + } + } else { + if ((conSignal-signal) > 50) { //低频的信号格数比高频大于两格,选低频 + deleteWifiStr.append(wifiStr); + } else { + deleteWifiStr.append(wifiInfo); + } + } + break; + } + } + } + + foreach (QString deleteStr, deleteWifiStr) { + slist.removeOne(deleteStr); + } + + return; +} + //从有配置文件的wifi选出最优wifi进行连接 QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){ QStringList target; @@ -1847,18 +1925,18 @@ QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){ } int indexPath = headLine.indexOf("DBUS-PATH"); QStringList tmp = slist; - for(int i=1;i= 5000 && ocf->isWifiConfExist(name) && signal > 55 && name != DisconnectedWifiSsidManualy){ //两格以上有配置的5Gwifi中选择信号最佳的 + if (ocf->isWifiConfExist(wifiname) && wifiname != DisconnectedWifiSsidManualy) { //两格以上有配置的5Gwifi中选择信号最佳的 target << wifiObjectPath <deleteLater(); return target; } @@ -1985,7 +2063,8 @@ void MainWindow::loadWifiListDone(QStringList slist) if (actWifiBssidList.contains(wbssid)) { actWifiName = wname; } - if ("*" == line.mid(0,indexSignal).trimmed()){ + if ("*" == line.mid(0,indexSignal).trimmed()) { + //在华为的电脑中,因为前面的优选工作,及时有已经连接的wifi,也可能会被筛选出去 actWifiBssid = wbssid; } } @@ -2002,13 +2081,23 @@ void MainWindow::loadWifiListDone(QStringList slist) QString wname = line.mid(indexName, indexPath - indexName).trimmed(); QString wfreq = line.mid(indexFreq, 4).trimmed(); - if (actWifiName != "--" && actWifiName == wname) { - if (!actWifiBssidList.contains(wbssid)) { - continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同,但bssid不同,则跳过 + if (!isHuaWeiPC) { + //如果不是华为的电脑,选择wifi在这里执行 + if (actWifiName != "--" && actWifiName == wname) { + if (!actWifiBssidList.contains(wbssid)) { + continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同,但bssid不同,则跳过 + } + } + if ((wnames.contains(wname) && wbssid != actWifiBssid)) { + continue; //过滤相同名称的wifi + } + } else { + //qDebug() << "--------------------> actWifiName= " << actWifiName << " wname= " << wname; + if ((actWifiName == wname) && actWifiBssidList.size()>=1) { + //防止列表中没有已经连接的那个wifi + wbssid = actWifiBssidList.at(0); + actWifiBssid = actWifiBssidList.at(0); } - } - if ((wnames.contains(wname) && wbssid != actWifiBssid)) { - continue; //过滤相同名称的wifi } int max_freq = wfreq.toInt(); @@ -3312,10 +3401,13 @@ void MainWindow::connWifiDone(int connFlag) { // Wifi连接结果,0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 if (connFlag == 0) { - WifiAuthThread *wifi_auth_thread=new WifiAuthThread(); - wifi_auth_thread->start(); + WifiAuthThread *wifi_auth_thread=new WifiAuthThread(); + wifi_auth_thread->start(); syslog(LOG_DEBUG, "Wi-Fi already connected by clicking button"); - this->ksnm->execGetWifiList(this->wcardname); + if (!isHuaWeiPC) { + //如果是华为电脑,使用获取连接信号的方式更新列表 + this->ksnm->execGetWifiList(this->wcardname); + } QString txt(tr("Conn Wifi Success")); objKyDBus->showDesktopNotify(txt); diff --git a/src/mainwindow.h b/src/mainwindow.h index 219ee989..659497b6 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -184,7 +184,7 @@ public: bool isWifiBeConnUp = false; //wifi是否是连接上 bool isToSetLanValue = true; //本次执行是否进行赋值 bool isToSetWifiValue = true; //本次执行是否进行赋值 - bool isWifiReconnecting; + bool isWifiReconnecting = false; //是否正在执行wifi的回连 int addNumberForWifi = 0; int m_priX; @@ -192,6 +192,8 @@ public: int m_priWid; int m_priHei; + int isHuaWeiPC; + public slots: void onPhysicalCarrierChanged(bool flag); void onCarrierUpHandle(); @@ -301,7 +303,8 @@ private: QString lastAddedConn = ""; QString oldActLanName = ""; //上一次获取的已连接有线网名称 int oldDbusActLanDNS = 0; //上一次获取的已连接有线网的DNS代号 - void wifiListOptimize(QStringList& slist); //只保留同名同频信号最强AP + void wifiListOptimize(QStringList& slist); //只保留同名同频信号最强AP + void getFinalWifiList(QStringList& slist); //获取应该显示在wifi列表中的最优列表参数 QStringList connectableWifiPriorityList(const QStringList slist); //可连接wifi优先级列表 //循环检测网络连接状态 QTimer *iconTimer = nullptr; @@ -329,7 +332,6 @@ private: bool hasWifiConnected;//当前是否有wifi连接 QDBusInterface *mDbusXrandInter; - int isHuaWeiPC; private slots: void iconActivated(QSystemTrayIcon::ActivationReason reason); diff --git a/src/oneconnform.cpp b/src/oneconnform.cpp index 1539baba..ad4996c1 100644 --- a/src/oneconnform.cpp +++ b/src/oneconnform.cpp @@ -32,6 +32,7 @@ #include #include #include +#include extern int currentActWifiSignalLv; @@ -886,6 +887,17 @@ void OneConnForm::slotConnWifiResult(int connFlag) connType = ""; if (connFlag == 0) { + if (mw->isHuaWeiPC) { + //network-manager可能回连接到其他bssid对应的网络,改成我们想要连接的那个网络 + QFuture < void > future1 = QtConcurrent::run([=]() { + qDebug() << "实际连接的wifi的bssid是 " << wifiBSsid; + QString modityCmd = "nmcli connection modify \""+ wifiName + "\" " + "802-11-wireless.bssid " + wifiBSsid; + system(modityCmd.toUtf8().data()); + QString reconnectWifiCmd = "nmcli connection up \"" + wifiName + "\""; + system(reconnectWifiCmd.toUtf8().data()); + }); + } + disconnect(this, SIGNAL(selectedOneWifiForm(QString,int)), mw, SLOT(oneWifiFormSelected(QString,int))); }