get wifi Current SSID - Android Wifi

Android examples for Wifi:Wifi SSID

Description

get wifi Current SSID

Demo Code

/*//from w w  w  .ja v  a2  s.c om
 WiFiUtil.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
//package com.java2s;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

public class Main {

    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