YiiWang

Android打开WiFi、关闭WiFi、打开WiFi热点、关闭WiFi热点以及通过SSID和密码来连接到指定WiFi的实现代码
Android打开WiFi、关闭WiFi、打开WiFi热点、关闭WiFi热点以及通过SSID和密码来连接到指定Wi...
扫描右侧二维码阅读全文
05
2018/07

Android打开WiFi、关闭WiFi、打开WiFi热点、关闭WiFi热点以及通过SSID和密码来连接到指定WiFi的实现代码

Android打开WiFi、关闭WiFi、打开WiFi热点、关闭WiFi热点以及通过SSID和密码来连接到指定WiFi的实现代码

需要权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

public class WiFi {
private WifiManager wifiManager;
private final String TAG = getClass().getSimpleName();

public WiFi(Context context) {
    wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
}

// 打开WiFi
public void openWiFi() {
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }
}

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

// 创建WiFi热点
public void openHotspot(String SSID, String password) {
    // 关闭WiFi
    closeWifi();
    WifiConfiguration config = new WifiConfiguration();
    config.SSID = SSID;
    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;
    try {
        // 通过反射来打开热点
        Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
        boolean enable = (Boolean) method.invoke(wifiManager, config, true);
        if (enable) Log.i(TAG, "热点已开启 SSID:" + SSID + " password:" + password);
        else Log.e(TAG, "创建热点失败");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "创建热点失败:" + e.getMessage());
    }
}

// 关闭WiFi热点
public void closeHotspot() {
    try {
        Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
        method.setAccessible(true);
        WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
        Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method2.invoke(wifiManager, config, false);
    } catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
}

/**
 * 连接到特定WiFi
 *
 * @param SSID     SSID
 * @param password 密码
 * @param type     加密类型 1.没有密码 2.用wep加密 3.用wpa加密
 */
public boolean connectWiFi(String SSID, String password, int type) {
    int wcgID = wifiManager.addNetwork(createConfig(SSID, password, type));
    return wifiManager.enableNetwork(wcgID, true);
}
// 创建一个WiFi配置
private WifiConfiguration createConfig(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 + "\"";
    // 如果已经连接过WiFi,移除掉
    WifiConfiguration tempConfig = isExsits(SSID);
    if (tempConfig != null) {
        wifiManager.removeNetwork(tempConfig.networkId);
    }
    if (type == 1) {
        config.wepKeys[0] = "";
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;
    }
    if (type == 2) {
        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;
    }
    if (type == 3) {
        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;
    }
    return config;
}

// 判断当前SSID是否已经连接过
private WifiConfiguration isExsits(String SSID) {
    List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
    if (existingConfigs == null) return null;
    for (WifiConfiguration existingConfig : existingConfigs) {
        if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
            return existingConfig;
        }
    }
    return null;
}
}

下载地址:
WiFi.java

Last modification:July 5th, 2018 at 09:27 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment