Qt:读取MacOS的SSID列表
必须有一种方法可以使用Qt读取可用WiFi SSID列表 尝试:Qt:读取MacOS的SSID列表,macos,qt,networking,Macos,Qt,Networking,必须有一种方法可以使用Qt读取可用WiFi SSID列表 尝试: QNetworkConfigurationManager nwkMgr; // UPDATE: // Need to call nwkMgr.updateConfigurations() here // UPDATE this code needs to run in the slot for QNetworkManager::updateCompleted signal QList<QNetworkConfigurati
QNetworkConfigurationManager nwkMgr;
// UPDATE:
// Need to call nwkMgr.updateConfigurations() here
// UPDATE this code needs to run in the slot for QNetworkManager::updateCompleted signal
QList<QNetworkConfiguration> nwkCnfList = nwkMgr.allConfigurations();
for(const QNetworkConfiguration &ncnf : nwkCnfList)
{
qDebug() << ncnf.name() << ncnf.bearerType();
if (ncnf.bearerType() == QNetworkConfiguration::BearerWLAN)
{
// would like to detect WiFi here
qDebug() << "WiFi:" << ncnf.name();
}
}
更新:
上面使用接口而不是SSID的结果是第一次运行代码示例。我已经为我的跨平台应用程序添加了刷新按钮,并尝试在Mac上进行“刷新”,在应用程序启动几秒钟后的第二次运行显示了所有可用网络的SSID后面的界面。这一定是我打算报告的一些Qt错误(?)
但是上面在Windows和Linux上的相同代码Ubuntu输出真实的SSID并找到WiFi。
可以从命令行获取所需的(但我不仅要“首选”)WiFi网络列表:
bash-3.2$ networksetup -listpreferredwirelessnetworks en0
Preferred networks on en0:
WiFi-N1
WiFi-N2
WiFi-N3
....
WiFi-NN
我还从命令行调用airport程序以获取所有WiFi网络,并且有一条通向框架二进制目录的长路径,我不想硬编码
请建议如何处理它。最坏的情况是在这里处理MacOSAPI(在C++中),这也是可以接受的。目标是让读取SSID的用户打开一个与Qt的WiFi连接,并通过Qt启动一些“交换”。如果需要提供密码,我们甚至可以处理“首选”预设连接,但最好也是通过编程方式处理。很可能错误的范围是:Qt 5.7和OS X El Capitan 10.11.6,因为有很多报告说类似的代码应该可以正常工作 更新: 上面使用接口而不是SSID的结果是第一次运行代码示例。我已经为我的跨平台应用程序添加了刷新按钮,并尝试在Mac上进行“刷新”,应用程序启动几秒钟后的第二次运行显示了所有可用网络的SSID后面的界面。这一定是我打算报告的一些Qt错误(?) 基本上,OSX上的进程或Qt框架在启动后会立即报告SSID,启用该功能需要几秒钟的时间。与其他平台的另一个不一致之处是接口报告为SSID 更新2:
心灵召唤。只有回答这个问题,我们才能读取实际的SSID列表。在Windows中使用命令行参数读取SSID 这将显示当前配置文件是连接的配置文件
Netsh wlan show profiles
其他示例:SSID是当前连接的Wi-Fi
Netsh WLAN show interfaces
在Linux中,使用QProcess
运行命令
QProcess process;
QStringList arguments;
QString stdout;
process.start("ifconfig wlan0 up");
process.waitForFinished(-1);
arguments << "-c" << "iw dev wlan0 scan | grep SSID";
process.start("sh" ,arguments);
process.waitForFinished();
请解释一下你的答案。没有任何解释的代码被认为是一个合适的答案(从评论)。在这里,我们从命令行参数(NETSH WLAN显示接口)的输出中读取了连接的WiFi的SSID。在这里,使用QProcess执行命令。并在信号readyReadStandardOutput()后读取标准输出;这是我们获取已连接wifi的SSID的一种方法。但不确定其有效性。
QProcess process;
QStringList arguments;
QString stdout;
process.start("ifconfig wlan0 up");
process.waitForFinished(-1);
arguments << "-c" << "iw dev wlan0 scan | grep SSID";
process.start("sh" ,arguments);
process.waitForFinished();
process = new QProcess(this); // create on the heap, so it doesn't go out of scope
connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(readData())); // connect process signals with your code
process->start("netsh WLAN show interfaces"); // start the process
void readData()
{
QTextStream StdoutStream(process->readAllStandardOutput());
QString line;
QString profileStr;
do {
line = StdoutStream.readLine();
if ( line.contains("SSID")) // if ( line.contains("Profile"))
{
profileStr = line;
process->close();
break;
}
} while (!line.isNull());
}