qt搜索附近wifi名称并连接

#include <Windows.h>
#include <wlanapi.h>
#pragma comment(lib, "Wlanapi.lib")

void findWIFiNameAndConnect()
{
    // 初始化 WLAN API
    DWORD dwMaxClient = 2; // 客户端版本为 Windows Vista 或更高版本
    DWORD dwCurVersion = 0;
    HANDLE hClient = NULL;

    DWORD dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
    if (dwResult != ERROR_SUCCESS) {
        return;
    }

    // 获取网络接口列表
    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
    if (dwResult != ERROR_SUCCESS) {
        WlanCloseHandle(hClient, NULL);
        return ;
    }

    // 遍历网络接口列表
    for (DWORD i = 0; i < pIfList->dwNumberOfItems; i++) {
        PWLAN_INTERFACE_INFO pIfInfo = &pIfList->InterfaceInfo[i];
        qDebug() << "Interface #" << i + 1;
        qDebug() << "Interface Name: " << QString::fromWCharArray(pIfInfo->strInterfaceDescription);

        // 获取接口的网络连接属性
        PWLAN_AVAILABLE_NETWORK_LIST pAvailableNetworkList = NULL;
        dwResult = WlanGetAvailableNetworkList(hClient, &pIfInfo->InterfaceGuid, 0, NULL, &pAvailableNetworkList);
        if (dwResult == ERROR_SUCCESS) {
            for (DWORD j = 0; j < pAvailableNetworkList->dwNumberOfItems; j++) {
                PWLAN_AVAILABLE_NETWORK pNetwork = &pAvailableNetworkList->Network[j];
                QString ssid = QString::fromUtf8(reinterpret_cast<const char*>(pNetwork->dot11Ssid.ucSSID));
                QString profile = QString::fromUtf8(reinterpret_cast<const char*>(pNetwork->strProfileName));
                qDebug() << "SSID: " <<  ssid << "   profile:" << profile;

                // 连接指定的wifi名称
                if (ssid == "KO") {
#if 0
                    // 连接一个已经连过的wifi
                    PWLAN_CONNECTION_PARAMETERS pConnectionParams = new WLAN_CONNECTION_PARAMETERS();
                    pConnectionParams->wlanConnectionMode = wlan_connection_mode_profile ;
                    pConnectionParams->strProfile = pNetwork->strProfileName; // 替换为实际的 WiFi 配置文件名称
                    pConnectionParams->pDot11Ssid = &pNetwork->dot11Ssid; // 使用配置文件中的 SSID
                    pConnectionParams->pDesiredBssidList = NULL;
                    pConnectionParams->dot11BssType = dot11_BSS_type_infrastructure;
                    pConnectionParams->dwFlags = 0;

                    dwResult = WlanConnect(hClient, &pIfList->InterfaceInfo[i].InterfaceGuid, pConnectionParams, NULL);
                    if (dwResult != ERROR_SUCCESS) {
                        qDebug() << "WlanConnect failed with error code: " << dwResult;
                    } else {
                        qDebug() << "WlanConnect success";
                        break;
                    }
                    delete pConnectionParams;
#else
                    QString wifiName = "wifi名称";
                    QString wifiPassword = "wifi密码";
                    QString profile = QString("<?xml version=\"1.0\"?>"
                            "<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">"
                                "<name>%1</name>"
                                "<SSIDConfig>"
                                    "<SSID>"
                                        "<name>%2</name>"
                                    "</SSID>"
                                "</SSIDConfig>"
                                "<connectionType>ESS</connectionType>"
                                "<connectionMode>auto</connectionMode>"
                                "<autoSwitch>false</autoSwitch>"
                                "<MSM>"
                                    "<security>"
                                        "<authEncryption>"
                                            "<authentication>WPA2PSK</authentication>"
                                            "<encryption>AES</encryption>"
                                            "<useOneX>false</useOneX>"
                                        "</authEncryption>"
                                        "<sharedKey>"
                                            "<keyType>passPhrase</keyType>"
                                            "<protected>false</protected>"
                                            "<keyMaterial>%3</keyMaterial>"
                                        "</sharedKey>"
                                    "</security>"
                                "</MSM>"
                            "</WLANProfile>").arg(wifiName).arg(wifiName).arg(wifiPassword);

                    // 设置Wlan配置文件
                    WLAN_REASON_CODE reasonCode;
                    std::wstring wstr = profile.toStdWString();
                    dwResult = WlanSetProfile(hClient, &pIfInfo->InterfaceGuid, 0, wstr.c_str(), NULL, TRUE, NULL, &reasonCode);
                    if (dwResult != ERROR_SUCCESS) {
                        qDebug() << "WlanSetProfile failed with error code: " << dwResult;
                        break;
                    }
                    // 连接一个未连过的wifi
                    PWLAN_CONNECTION_PARAMETERS pConnectionParams = new WLAN_CONNECTION_PARAMETERS();
                    pConnectionParams->wlanConnectionMode = wlan_connection_mode_profile ;
                    pConnectionParams->strProfile = wstr.c_str();       // 替换为实际的 WiFi 配置文件名称

                    DOT11_SSID dot11Ssid = { 0 };
                    // 将 QString 转换为宽字符串
                    std::wstring wifiNameW = wifiName.toStdWString();
                    // 拷贝宽字符串到 dot11Ssid
                    memcpy(dot11Ssid.ucSSID, wifiNameW.c_str(), wifiNameW.size() * sizeof(wchar_t));
                    dot11Ssid.uSSIDLength = static_cast<ULONG>(wifiNameW.size());
                    pConnectionParams->pDot11Ssid = &dot11Ssid;               // 使用配置文件中的 SSID
                    pConnectionParams->pDesiredBssidList = NULL;
                    pConnectionParams->dot11BssType = dot11_BSS_type_infrastructure;
                    pConnectionParams->dwFlags = 0;

                    dwResult = WlanConnect(hClient, &pIfList->InterfaceInfo[i].InterfaceGuid, pConnectionParams, NULL);
                    if (dwResult != ERROR_SUCCESS) {
                        qDebug() << "WlanConnect failed with error code: " << dwResult;
                        break;
                    } else {
                        qDebug() << "WlanConnect success";
                        break;
                    }
                    delete pConnectionParams;
#endif
                }
            }
            WlanFreeMemory(pAvailableNetworkList);
        }
        else {
            qDebug() << "WlanGetAvailableNetworkList failed with error code: " << dwResult;
        }
    }

    WlanFreeMemory(pIfList);
    WlanCloseHandle(hClient, NULL);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容