상세 컨텐츠

본문 제목

[C++ QT QML] 리눅스에서 QML WIFI list 불러오게 구현

QT|QML

by donggyu1998 2021. 6. 16. 10:35

본문

반응형

💡 실행 사진

💡 list_ssid.pro

QT -= gui
QT += dbus
SOURCES += list_ssid.cpp

코드 : https://pastebin.com/Mkvbu01c

💡 main.cpp

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>
#include <QDebug>
#include <QThread>
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv); 
    QDBusInterface nm("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
            "org.freedesktop.NetworkManager", QDBusConnection::systemBus());
    if(!nm.isValid())
    {
        qFatal("Failed to connect to the system bus");
    }
    QDBusMessage msg = nm.call("GetDevices");
    qDebug() << "GetDevices reply: " << msg << endl;
    QDBusArgument arg = msg.arguments().at(0).value<QDBusArgument>();
    if(arg.currentType() != QDBusArgument::ArrayType)
    {
        qFatal("Something went wrong with getting the device list");
    }
    QList<QDBusObjectPath> pathsLst = qdbus_cast<QList<QDBusObjectPath> >(arg);
    foreach(QDBusObjectPath p, pathsLst)
    {
        qDebug() << "DEV PATH: " << p.path();
        QDBusInterface device("org.freedesktop.NetworkManager", p.path(),
        "org.freedesktop.NetworkManager.Device", QDBusConnection::systemBus());
        if (device.property("DeviceType").toInt() != 2) 
        {
            continue;
        }
        QDBusInterface wifi_device("org.freedesktop.NetworkManager", p.path(),
        "org.freedesktop.NetworkManager.Device.Wireless", QDBusConnection::systemBus());
        QMap<QString, QVariant> argList;
        QDBusMessage msg = wifi_device.call("RequestScan", argList);
        QThread::sleep(2); 
        msg = wifi_device.call("GetAllAccessPoints");
        qDebug()<< "Answer for GetAllAccessPoints:  " << msg << endl << endl;
        QDBusArgument ap_list_arg = msg.arguments().at(0).value<QDBusArgument>();
        QList<QDBusObjectPath> ap_path_list = qdbus_cast<QList<QDBusObjectPath> >(ap_list_arg);
        foreach(QDBusObjectPath p ,ap_path_list)
        {
            QDBusInterface ap_interface("org.freedesktop.NetworkManager", p.path(),
            "org.freedesktop.NetworkManager.AccessPoint", QDBusConnection::systemBus());
            qDebug() << "SSID: " << ap_interface.property("Ssid").toString();
        }
    }
    return 0;
}

코드 : https://pastebin.com/UQcMTFdw

💡 ssid_list.cpp

#include <arpa/inet.h>
#include <QThread>
#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/Device>
#include <NetworkManagerQt/WirelessDevice>
#include <NetworkManagerQt/AccessPoint>
int main()
{
    NetworkManager::Device::List list = NetworkManager::networkInterfaces();
    Q_FOREACH (NetworkManager::Device::Ptr dev, list)
    {
        if(dev->type() != NM_DEVICE_TYPE_WIFI)
        {
            continue;
        }
        NetworkManager::WirelessDevice wifi_dev(dev->uni());
        wifi_dev.requestScan();
        QThread::sleep(2);
        foreach(QString ap_path, wifi_dev.accessPoints())
        {
            NetworkManager::AccessPoint ap(ap_path);
            qDebug() << "SSID:" << ap.ssid();
        }
    }
}

코드 : https://pastebin.com/q7YmuFWa

💡 CMakeLists.txt

project(ssid_list LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 REQUIRED COMPONENTS
    Core
    Gui
    Network
    DBus
)
find_package(KF5NetworkManagerQt REQUIRED)
add_executable(ssid_list
  ssid_list.cpp
)
target_link_libraries(ssid_list Qt5::Core Qt5::DBus Qt5::Network KF5::NetworkManagerQt)

코드 : https://pastebin.com/rQJEQbMT

반응형

관련글 더보기