Connect to wifi network Android programmatically

与世无争的帅哥 提交于 2021-02-08 06:14:19

问题


I am trying to connect to selected wifi network(from UI) but I am unable to connect to it.

Here is my code:

private boolean addNetworkAndActivate(ScanResult scanResult, String password) {

        WifiConfiguration wc = null;

        List<WifiConfiguration> configs = mainWifi.getConfiguredNetworks();

        for (WifiConfiguration wifiConfiguration : configs) {
            try {
                if (wifiConfiguration.SSID.equals("\"" + scanResult.SSID + "\"")) {
                    wc = wifiConfiguration;
                    break;
                }
            } catch (Exception e) {

            }
        }

        // not configured, create new
        if (wc == null) {
            wc = new WifiConfiguration();

            ConfigurationSecuritiesV1 conf = new ConfigurationSecuritiesV1();
            conf.setupSecurity(wc, conf.getScanResultSecurity(scanResult), password);
            wc.SSID = "\"" + scanResult.SSID + "\"";

            int res = mainWifi.addNetwork(wc);

            if (res == -1)
                return false;

            if (!mainWifi.saveConfiguration())
                return false;
        }

        mainWifi.disconnect();
        boolean active = mainWifi.enableNetwork(wc.networkId, true);
//      boolean test = mainWifi.reconnect();

        return active;
    }


        public void setupSecurity(WifiConfiguration config, String security, String password) {
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();

        final int sec = security == null ? SECURITY_NONE : Integer.valueOf(security);
        final int passwordLen = password == null ? 0 : password.length();
        switch (sec) {
        case SECURITY_NONE:
            config.allowedKeyManagement.set(KeyMgmt.NONE);
            break;

        case SECURITY_WEP:
            config.allowedKeyManagement.set(KeyMgmt.NONE);
            config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
            config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
            if (passwordLen != 0) {
                // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
                if ((passwordLen == 10 || passwordLen == 26 || passwordLen == 58) && password.matches("[0-9A-Fa-f]*")) {
                    config.wepKeys[0] = password;
                } else {
                    config.wepKeys[0] = '"' + password + '"';
                }
            }
            break;

        case SECURITY_PSK:
            config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
            if (passwordLen != 0) {
                if (password.matches("[0-9A-Fa-f]{64}")) {
                    config.preSharedKey = password;
                } else {
                    config.preSharedKey = '"' + password + '"';
                }
            }
            break;

        case SECURITY_EAP:
            config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
            config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
            break;

        default:
            Log.d(TAG, "Invalid security type: " + sec);
        }
    }

When I try to connect with above code, The network gets selected but when I see its status in android wifi settings screen, it says "network name" disabled and then it automatically connects to previously connected network.

来源:https://stackoverflow.com/questions/38691846/connect-to-wifi-network-android-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!