qt qdbus 网络管理器

问题描述

我通过 dbus 访问网络管理器。目前我已经设法列出了所有可用的连接 我有一个包含 ssid 和密码的 json。我想依靠我的 json 来激活连接。 例如“blabla”网络可用,其密码包含“qt is good” 并且我的 json 包含登录名“blabla”和密码“qt is good”我希望能够连接到这个网络 我扫描wifi的代码,他返回一个QString:

    QDBusInterface NetworkManager("org.freedesktop.NetworkManager",// get the interface to NetworkManager
                      "/org/freedesktop/NetworkManager","org.freedesktop.NetworkManager",QDBusConnection::systemBus());
    if(!NetworkManager.isValid())
    {
        qDebug()<< "Failed to connect to the system bus" << NetworkManager.lastError() <<endl ;
    }

    QDBusMessage DbusMsg = NetworkManager.call("GetDevices"); //Get  Devices
   // qDebug() << "GetDevices reply: " << DbusMsg << endl;

    QDBusArgument DbusArg = DbusMsg.arguments().at(0).value<QDBusArgument>();
    if(DbusArg.currentType() != QDBusArgument::ArrayType)
    {
        qDebug()<< "Something went wrong with getting the device list" << endl;
    }
    QList<QDBusObjectPath> pathsLst = qdbus_cast<QList<QDBusObjectPath>>(DbusArg);
    foreach(QDBusObjectPath DbusObjectPath,pathsLst)
    {
        QDBusInterface device("org.freedesktop.NetworkManager",// creating an interface used to gather this devices properties
                              DbusObjectPath.path(),"org.freedesktop.NetworkManager.Device",QDBusConnection::systemBus());

        if (device.property("DeviceType").toInt() != 2) // 2 is WiFi dev,see https://people.freedesktop.org/~lkundrak/nm-docs/nm-dbus-types.html#NMDeviceType
        {
            continue;
        }
        QDBusInterface wifi_device(   // Get dbus Interface for WiFi Device
                    "org.freedesktop.NetworkManager",DbusObjectPath.path(),"org.freedesktop.NetworkManager.Device.Wireless",QDBusConnection::systemBus());

        QMap<QString,QVariant> ArgList;
        QDBusMessage msg = wifi_device.call("RequestScan",ArgList);
        QThread::sleep(2); //wait for the scan

        msg = wifi_device.call("GetAllAccessPoints"); //Call devices
        QDBusArgument Dbus_Arg_List = msg.arguments().at(0).value<QDBusArgument>();
        QList<QDBusObjectPath> Dbus_Object_Path = qdbus_cast<QList<QDBusObjectPath> >(Dbus_Arg_List);

        foreach(QDBusObjectPath p,Dbus_Object_Path)
        {
            QDBusInterface ap_interface("org.freedesktop.NetworkManager",p.path(),"org.freedesktop.NetworkManager.AccessPoint",QDBusConnection::systemBus());
            return ap_interface.property("Ssid").toString() ;

        }
    }

以及我在哪里使用它:

if(error.error == QJsonParseError::NoError)
        {
            QJsonObject json(parsed_object.object());

            QString pass = json.value("pass").toString();
            QString ssid = json.value("ssid").toString();
            QString action = json.value("action").toString();

            QJsonObject machine = json.value("machine").toObject(); // get sub-object
            QString login = machine.value("login").toString();
            QString password = machine.value("password").toString();

            qDebug() << "parsed BARCODE JSON..." << parsed_object << endl;

            if(action.contains("network-configuration"))
            {
                qDebug()<< "Network Requirements" << endl;

                if(machine.contains("login")){
                   if(login == ("test") && password == ("test"))
                   {
                      qDebug()<< "The machine's login and password are correct" << endl;
                      qDebug()<< "wifi available" << ScanWiFi() << endl; // scan available networks
                      if (ScanWiFi() == ssid)
                      {
                          qDebug()<< "deleting... " << ssid << endl ;
                          /*delete the connection
                          ...
                          ...
                          */

                             /*AND*/

                          /*recreate and active connection
                          ...
                          ...
                          */
                          qDebug()<< "active connection :" << ssid << endl ;

                      }
                   }
}

json 示例:

BARCODE: {"action": "network-configuration","ssid": "truc","pass": "machin","machine": {"login": "...","password": "..."}}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...