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

当然使用前需初始化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 SSID
public 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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值