如何解决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 举报,一经查实,本站将立刻删除。