AdeleGoldberg
AdeleGoldberg

Reputation: 1339

Qt API to check if wifi is enabled/disabled

Is there a way in Qt application framework to check if wifi is enabled? And does the way work for Android, iOS, macOS and Windows?

Note that I want to check if wifi is enabled or not. i don't bother if I have internet connectivity.

Environment:
Qt 5.12.x commercial version

Upvotes: 1

Views: 1111

Answers (1)

Former contributor
Former contributor

Reputation: 2576

You can iterate your QNetworkInterface instances (QT+=network in your .pro), and check the type() for a wireless interface, and also check the flags() to see if it is up and running. Example:

#include <QCoreApplication>
#include <QNetworkInterface>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    for(const QNetworkInterface& iface : QNetworkInterface::allInterfaces()) {
        if (iface.type() == QNetworkInterface::Wifi) {
            qDebug() << iface.humanReadableName() << "(" << iface.name() << ")"
                     << "is up:" << iface.flags().testFlag(QNetworkInterface::IsUp)
                     << "is running:" << iface.flags().testFlag(QNetworkInterface::IsRunning);
        }
    }
}

It should be cross-platform, but the documentation says that:

Not all operating systems support reporting all features. Only the IPv4 addresses are guaranteed to be listed by this class in all platforms. In particular, IPv6 address listing is only supported on Windows, Linux, macOS and the BSDs.

There is no word about other attributes.

Upvotes: 4

Related Questions