Android10的WIFI 名称读取为空解决

  其实android9的获取wifi名称的方法可以用,只是android10如果要用到wifi相关的api时要引用ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION权限,然后在android版本判断中加入android10的判断就好了。

public static String getWIFISSID(Activity activity) {
    String ssid = "unknown id";

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O || Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

        WifiManager mWifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        assert mWifiManager != null;
        WifiInfo info = mWifiManager.getConnectionInfo();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return info.getSSID();
        } else {
            return info.getSSID().replace("\"", "");
        }
    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {

        ConnectivityManager connManager = (ConnectivityManager) activity.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        assert connManager != null;
        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo.isConnected()) {
            if (networkInfo.getExtraInfo() != null) {
                return networkInfo.getExtraInfo().replace("\"", "");
            }
        }
    }
    return ssid;
}

 

你可能感兴趣的:(安卓,android)