以编程方式连接到wifi网络android

时间:2016-08-01 05:55:41

标签: java android wifi android-wifi

我正在尝试连接到选定的wifi网络(来自UI),但我无法连接到它。

这是我的代码:

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);
        }

    }

当我尝试连接上面的代码时,网络被选中但是当我在android wifi设置屏幕中看到它的状态时,它会显示&#34;网络名称&#34;禁用,然后它会自动连接到以前连接的网络。

0 个答案:

没有答案