如何以编程方式获取所有配置的 WiFi 网络的 SSID?

How to get the SSID of all configured WiFi networks programmatically?

我想获取所有已添加密码的 WiFi 详细信息。

代码:

WifiManager wifiManager = (WifiManager) getApplicationContext()
    .getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredList = wifiManager.getConfiguredNetworks();

编辑:WifiConfiguration 自 API 级别 29 起被弃用,来自其 docs:

This class was deprecated in API level 29. Use WifiNetworkSpecifier.Builder to create NetworkSpecifier and WifiNetworkSuggestion.Builder to create WifiNetworkSuggestion. This will become a system use only object in the future.

原始(过时)答案

您可以简单地从 WifiConfiguration 的 public SSID 字段中获取 SSID:

List<String> ssidList = new ArrayList<>();

for(WifiConfiguration config : configuredList) {
    ssidList.add(config.SSID);
}

不要忘记将 ACCESS_WIFI_STATE 权限添加到您的 AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>