如何在android中获取当前的wifi连接信息

时间:2012-01-10 22:15:33

标签: android

我正在尝试查找scanResult是否是当前连接的无线网络。

这是我的代码

public boolean IsCurrentConnectedWifi(ScanResult scanResult) 
{
    WifiManager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo currentWifi = mainWifi.getConnectionInfo();
    if(currentWifi != null)
    {
        if(currentWifi.getSSID() != null) 
        {
            if(currentWifi.getSSID() == scanResult.SSID)
            return true;
        }
    }  
    return false;
}

我在获取scanresult方面没有任何问题。

总是我得到当前的Wifi是空的。

我错在哪里或有没有其他方法可以做到这一点?

3 个答案:

答案 0 :(得分:56)

很可能您已找到答案:currentWifi.getSSID()在大多数情况下都引用scanResult.SSID而不是{当然您不能在字符串上使用== :)。

尝试这样的操作,它会返回当前SSIDnull

public static String getCurrentSsid(Context context) {
  String ssid = null;
  ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }
  return ssid;
}

还需要权限:

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

StringUtil不是标准的Android类,因此您可以使用TextUtils代替。然后代码如下所示:

public static String getCurrentSsid(Context context) {
  String ssid = null;
  ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }
  return ssid;
}

答案 1 :(得分:16)

我对当前答案的不推荐,修改过的方法

public static String getCurrentSsid(Context context) {
        String ssid = null;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo == null) {
            return null;
        }

        if (networkInfo.isConnected()) {
            final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
                ssid = connectionInfo.getSSID();
            }
        }

    return ssid;
}

答案 2 :(得分:2)

更新Android Oreo。 您还需要在清单中添加ACCESS_FINE_LOCATION权限,否则它将返回未知的SSID

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


 fun getCurrentSsid(context: Context): String {
            var ssid = "NA"
            val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            if (networkInfo.isConnected) {
                val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                val connectionInfo = wifiManager.connectionInfo
                if (connectionInfo != null && !connectionInfo.ssid.isEmpty()) {
                    ssid = connectionInfo.ssid
                }
            }
            return ssid
        }