qt - 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": "..."}}
解决方案
推荐阅读
- android - 如何将文件位置作为函数参数传递给 MediaPlayer 文件的可单击列表视图
- openapi - openapi3:如何指定对象模式应该是模式本身?
- java - 如何在debian虚拟机上安装java
- node.js - 使用 node-svn-ultimate 库按日期范围获取 svn 提交
- jquery - jQuery animate 忽略 z-index 错误
- go - 如何制作 goreleaser 脚本以在本地构建 deb 文件?
- docker - 如何将活动目录集成到 Grafana docker 容器中?
- .net - 如何在 Frame.Navigate 调用后获取导航页面的实际实例?
- php - 如何处理 SOAP-WSDL 响应
- azure - 如何在 Azure ARM 模板中指定子/父关系中的不同资源?