get Wifi Ssid with Exception handler - Android android.net.wifi

Android examples for android.net.wifi:Wifi SSID

Description

get Wifi Ssid with Exception handler

Demo Code

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

public class Main{

    public static String getWifiSsid(Context context) {
        String wifi_name = "";
        try {//  w  w w .ja v  a 2 s  .c o  m
            WifiManager wifiMgr = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            // int wifiState = wifiMgr.getWifiState();
            WifiInfo info = wifiMgr.getConnectionInfo();
            wifi_name = info != null ? info.getSSID() : "";
            wifi_name = wifi_name.replace("\"", "");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            wifi_name = "";
        }

        return wifi_name;
    }

}

Related Tutorials