how to get current wifi connection info in android

前端 未结 3 681
天涯浪人
天涯浪人 2020-12-15 03:04

I\'m trying to find if scanResult is the current connected wifi network.

here is my code

public boolean IsCurrentConnectedWifi(ScanResul         


        
3条回答
  •  攒了一身酷
    2020-12-15 03:44

    My (used to be) non-deprecated, modified approach to the current answer

    https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetworkInfo()

    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;
    }
    

提交回复
热议问题