is Changed SSID - Android android.net.wifi

Android examples for android.net.wifi:Wifi SSID

Description

is Changed SSID

Demo Code

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

public class Main {

  public static boolean isChangedSSID(final Context context, final String oldSSID) {
    String current = getCurrentSSID(context);
    if (current == null) {
      return (oldSSID != null);
    }/*w  w  w . j a  v a 2  s  .  co  m*/
    return !current.equals(oldSSID);
  }

  public static String getCurrentSSID(final Context context) {

    String ssid = null;

    if (isOnWiFi(context)) {
      WifiManager wifi = (WifiManager) context.getSystemService(android.content.Context.WIFI_SERVICE);
      ssid = wifi.getConnectionInfo().getSSID();

      if (ssid != null) {
        ssid = ssid.replaceAll("\"", "");
      }
    }

    return ssid;
  }
  public static boolean isOnWiFi(final Context context) {

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();

    if (ni == null || !ni.isConnected()
            || (ni.getType() != ConnectivityManager.TYPE_WIFI)) {
        return false;
    }

    return true;
}
}

Related Tutorials