5

My code for before android Q is here but for android Q this function return "unknown ssid". I know that for android Q must use this ConnectivityManager.NetworkCallback and ConnectivityManager#getNetworkCapabilities and ConnectivityManager#getLinkProperties.but i cant find sample code or guid.

private String get_connected_ssid() {

    String s1 = null ,s2 = null;
    try {
        WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        assert wifi != null;
        WifiInfo wifiinfo = wifi.getConnectionInfo();

        List<WifiConfiguration> listOfConfigurations = wifi.getConfiguredNetworks();

        for (int index = 0; index < listOfConfigurations.size(); index++) {

            WifiConfiguration configuration = listOfConfigurations.get(index);

            if (configuration.networkId == wifiinfo.getNetworkId()) {
                s1 = configuration.SSID;
            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }
      /*     NetworkInfo Deprecated in API level 29
             Callers should instead use the ConnectivityManager.NetworkCallback API to learn about connectivity changes,
             or switch to use ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties to
             get information synchronously. Keep in mind that while callbacks are guaranteed to be called for every
             event in order, synchronous calls have no such constraints, and as such it is unadvisable to use the
             synchronous methods inside the callbacks as they will often not offer a view of networking that is
             consistent (that is: they may return a past or a future state with respect to the event being processed
             by the callback). Instead, callers are advised to only use the arguments of the callbacks, possibly
             memorizing the specific bits of information they need to keep from one callback to another.
             */
    //getActiveNetworkInfo Deprecated in API level 29 -->useUse NetworkCallbacks instead for apps that target Android 10 (API level 29) and higher-->refrence : developer.android
    //getExtraInfo() Deprecated in API level 29
    try {
        ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        assert cm != null;
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return s1 + s2;
}

0 Answers0