Android利用反射获取WLAN热点信息

来源:互联网 发布:周立波与郭德纲 知乎 编辑:程序博客网 时间:2024/05/16 05:33

当然使用前需初始化wifiManager

mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);private static final String TAG = "wifiHelper";private WifiManager mWifiManager = null;//判断WLAN状态是否开启public boolean isWifiApOn() {    Method method = null;    int i = 0;    try {        method = mWifiManager.getClass().getMethod("getWifiApState");    } catch (NoSuchMethodException e) {        e.printStackTrace();    }    try {        i = (Integer) method.invoke(mWifiManager);    } catch (IllegalAccessException e) {        e.printStackTrace();    } catch (InvocationTargetException e) {        e.printStackTrace();    }    Log.i(TAG, "wifi sharing state -> " + i);    // 10---正在关闭;11---已关闭;12---正在开启;13---已开启    return i == 13;}//设置WLAN状态public boolean setWifiApEnabled(boolean enabled) {    Method method = null, configMethod = null;    boolean result = false;    if (mWifiManager == null) {        Log.i(TAG, "mWifiManager is null  -> " + result);        return result;    }    try {        configMethod = mWifiManager.getClass().getMethod("getWifiApConfiguration");        method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);    } catch (Exception e) {        e.printStackTrace();    }    try {        WifiConfiguration apConfig = (WifiConfiguration) configMethod.invoke(mWifiManager);        result = (boolean) method.invoke(mWifiManager, new Object[]{apConfig, enabled});    } catch (IllegalAccessException e) {        e.printStackTrace();    } catch (InvocationTargetException e) {        e.printStackTrace();    }    Log.i(TAG, "setWifiApEnabled -> " + result);    return result;}//获取WLAN SSIDpublic String getWifiApSSID() {    Method method = null;    String SSID = null;    try {        method = mWifiManager.getClass().getMethod("getWifiApConfiguration");    } catch (Exception e) {        e.printStackTrace();    }    try {        WifiConfiguration apConfig = (WifiConfiguration) method.invoke(mWifiManager);        SSID = apConfig.SSID;    } catch (IllegalAccessException e) {        e.printStackTrace();    } catch (InvocationTargetException e) {        e.printStackTrace();    } catch (Exception e) {        e.printStackTrace();    }    Log.i(TAG, "getWifiApSSID -> " + SSID);    return SSID;}//获取WLAN 密码public String getWifiApSharedKey() {    Method method = null;    String SharedKey = null;    try {        method = mWifiManager.getClass().getMethod("getWifiApConfiguration");    } catch (Exception e) {        e.printStackTrace();    }    try {        WifiConfiguration apConfig = (WifiConfiguration) method.invoke(mWifiManager);        SharedKey = apConfig.preSharedKey;    } catch (IllegalAccessException e) {        e.printStackTrace();    } catch (InvocationTargetException e) {        e.printStackTrace();    } catch (Exception e) {        e.printStackTrace();    }    return SharedKey;}
0 0
原创粉丝点击