get Ap SSID from WifiManager - Android android.net.wifi

Android examples for android.net.wifi:Wifi SSID

Description

get Ap SSID from WifiManager

Demo Code

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class Main {

  public static String getApSSID(WifiManager wifiManager) {
    try {//  www  .j  a  va  2s .  c  om
      Method localMethod = wifiManager.getClass().getDeclaredMethod("getWifiApConfiguration", new Class[0]);
      if (localMethod == null)
        return null;
      Object localObject1 = localMethod.invoke(wifiManager, new Object[0]);
      if (localObject1 == null)
        return null;
      WifiConfiguration localWifiConfiguration = (WifiConfiguration) localObject1;
      if (localWifiConfiguration.SSID != null)
        return localWifiConfiguration.SSID;
      Field localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
      if (localField1 == null)
        return null;
      localField1.setAccessible(true);
      Object localObject2 = localField1.get(localWifiConfiguration);
      localField1.setAccessible(false);
      if (localObject2 == null)
        return null;
      Field localField2 = localObject2.getClass().getDeclaredField("SSID");
      localField2.setAccessible(true);
      Object localObject3 = localField2.get(localObject2);
      if (localObject3 == null)
        return null;
      localField2.setAccessible(false);
      String str = (String) localObject3;
      return str;
    } catch (Exception localException) {
    }
    return null;
  }

}

Related Tutorials