如何获取连接的Wifi名称?

时间:2017-12-28 08:53:57

标签: android

我使用下面的代码在android中获取Wifi名称。以下是我使用过的代码:

WifiManager wifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
       WifiInfo info;
       if (wifiManager != null && isWebViewLoaded) {
           info = wifiManager.getConnectionInfo();
           String wifiName = info.getSSID();
}

该代码适用于其他设备(低于版本8),但我尝试使用Nexus 5X(Oreo v-8.1),它提供了"未知的SSID"反对info.getSSID()。

1 个答案:

答案 0 :(得分:0)

试试此代码,原始回答here

public String getWifiName(Context context) {
   WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (manager.isWifiEnabled()) {
       WifiInfo wifiInfo = manager.getConnectionInfo();
       if (wifiInfo != null) {
          DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
          if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {
              return wifiInfo.getSSID();
          }
       }
    }
    return null;
}