Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);
- // get the interface to nm
- QDBusInterface nm("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
- "org.freedesktop.NetworkManager", QDBusConnection::systemBus());
- if(!nm.isValid())
- {
- qFatal("Failed to connect to the system bus");
- }
- // get all devices
- 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();
- // creating an interface used to gather this devices properties
- QDBusInterface device("org.freedesktop.NetworkManager", p.path(),
- "org.freedesktop.NetworkManager.Device", QDBusConnection::systemBus());
- // 2 is WiFi dev, see https://people.freedesktop.org/~lkundrak/nm-docs/nm-dbus-types.html#NMDeviceType
- if (device.property("DeviceType").toInt() != 2)
- {
- continue;
- }
- // we got a wifi device, let's get an according dbus interface
- QDBusInterface wifi_device("org.freedesktop.NetworkManager", p.path(),
- "org.freedesktop.NetworkManager.Device.Wireless", QDBusConnection::systemBus());
- // we need to call scan on the inteface prior to request the list of interfaces
- QMap<QString, QVariant> argList;
- QDBusMessage msg = wifi_device.call("RequestScan", argList);
- QThread::sleep(2); // not the best solution, but here we just wait for the scan
- // doing the actual call
- msg = wifi_device.call("GetAllAccessPoints");
- qDebug()<< "Answer for GetAllAccessPoints: " << msg << endl << endl;
- // dig out the paths of the Access Point objects:
- QDBusArgument ap_list_arg = msg.arguments().at(0).value<QDBusArgument>();
- QList<QDBusObjectPath> ap_path_list = qdbus_cast<QList<QDBusObjectPath> >(ap_list_arg);
- // and iterate through the list
- foreach(QDBusObjectPath p ,ap_path_list)
- {
- // for each Access Point we create an interface
- QDBusInterface ap_interface("org.freedesktop.NetworkManager", p.path(),
- "org.freedesktop.NetworkManager.AccessPoint", QDBusConnection::systemBus());
- // and getting the name of the SSID
- qDebug() << "SSID: " << ap_interface.property("Ssid").toString();
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment