Android Wifi相关操作

这篇博客介绍了如何在Android平台上进行WiFi相关操作,包括开启和关闭WiFi设置、查询当前WiFi状态、检查特定SSID的WiFi是否已开启、连接指定SSID的WiFi以及清除特定SSID的WiFi配置。注意在处理SSID时的字符引用差异。
摘要由CSDN通过智能技术生成

摘要

自己造了一个轮子,包含了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<ScanResult> 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<WifiConfiguration> 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<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration configuration : list) {
            if (configuration.SSID.equals(ssId)) {
                mWifiManager.removeNetwork(configuration.networkId);
                break;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值