Android Wifi相关操作

摘要

自己造了一个轮子,包含了Android WIFI 一些常用的方法,例如:
1、开启wifi设置
2、关闭wifi设置
3、获取当前wifi状态
4、判断指定SSID的wifi是否开启
5、连接指定SSID的wifi
6、清除指定SSID的wifi

注意

wifi对应的SSID不需要被 ” 符号包围
WifiConfiguration对应的SSID需要被 ” 符号包围

代码

public class WifiHelper {

    public static final String WIFI_SSID = "CMCC-FREE"; //指定wifi的SSID
    public static final String WIFI_NETWORK_ID = "\"CMCC-FREE\""; //指定WifiConfiguration的SSID
    public static final String WIFI_PASSWORD = "\"1234567890\""; //指定wifi的密码

    public static final int WIFI_CIPHER_NOPASS = 1;
    public static final int WIFI_CIPHER_WEP = 2;
    public static final int WIFI_CIPHER_WAP = 3;

    private WifiManager mWifiManager;

    public WifiHelper(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    /**
     * 打开WIFI
     */
    public void openWifi() {
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
        }
    }

    /**
     * 关闭WIFI
     */
    public void closeWifi() {
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
        }
    }

    /**
     * wifi是否打开
     *
     * @return true已打开,false已关闭
     */
    public boolean isWifiEnable() {
        return mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED;
    }

    /**
     * 指定SSID的wifi是否开启
     *
     * @param ssId ssId
     * @return true成功,false失败
     */
    public boolean isWifiOpen(String ssId) {
        mWifiManager.startScan();
        List mWifiList = mWifiManager.getScanResults();
        if (mWifiList != null) {
            for (ScanResult item : mWifiList) {
                if (item.SSID.equals(ssId)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 特定SSID的wifi是否存在
     *
     * @param ssId ssId
     * @return WifiConfiguration
     */
    private WifiConfiguration isExist(String ssId) {
        List existingConfigs = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration existingConfig : existingConfigs) {
            if (existingConfig.SSID.equals(ssId)) {
                return existingConfig;
            }
        }
        return null;
    }

    /**
     * 连接指定SSID的wifi
     *
     * @param ssId     ssId
     * @param password 密码
     * @param type     热点类型:1-无密码,2-WEP密码验证,3-WAP或WAP2 PSK密码验证
     * @return true连接成功,false连接失败
     */
    public boolean connectWifi(String ssId, String password, int type) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = ssId;

        WifiConfiguration tmp = isExist(ssId); //若配置列表中存在同名的配置,则清除
        if (tmp != null) {
            mWifiManager.removeNetwork(tmp.networkId);
        }

        switch (type) {
            case WIFI_CIPHER_NOPASS: //无密码
                config.hiddenSSID = true;
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                break;
            case WIFI_CIPHER_WEP: //WEP密码验证
                config.hiddenSSID = true;
                config.wepKeys[0] = password;
                config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                config.wepTxKeyIndex = 0;
                break;
            case WIFI_CIPHER_WAP: //WAP或WAP2 PSK密码验证
                config.preSharedKey = password;
                config.hiddenSSID = true;
                config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                config.status = WifiConfiguration.Status.ENABLED;
                break;
            default:
                break;
        }
        int wcgId = mWifiManager.addNetwork(config);
        return mWifiManager.enableNetwork(wcgId, true);
    }

    /**
     * 清除指定ssId的热点配置
     *
     * @param ssId
     */
    public void removeWifi(String ssId) {
        List list = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration configuration : list) {
            if (configuration.SSID.equals(ssId)) {
                mWifiManager.removeNetwork(configuration.networkId);
                break;
            }
        }
    }
}

你可能感兴趣的:(Android)