使用散列passphase以编程方式连接到wifi

时间:2016-05-31 16:40:51

标签: android

我想连接到具有给定SSID和密码的WiFi网络。使用非哈希密码短语(例如:password =“123456789”)是可以的,但它不适用于散列密码的网络(特别是在三星设备上)。我已经花了好几天时间进行调试,但我找不到任何方法让它在两种情况下都能正常工作。请帮忙! 这是我的代码:

 public void connectTo(String SSID, String password){
        List<ScanResult> scanResultList = wifiManager.getScanResults();
        //assume the first WiFi always available
        String securityMode = getScanResultSecurity(result.firstObject());

        WifiConfiguration wifiConfiguration = createAPConfiguration(SSID, password, securityMode);

        int res = wifiManager.addNetwork(wifiConfiguration);

        wifiManager.enableNetwork(res, true);
        wifiManager.setWifiEnabled(true);
        wifiManager.saveConfiguration();
}

private WifiConfiguration createAPConfiguration(String networkSSID, String networkPasskey, String securityMode) {
        WifiConfiguration wifiConfiguration = new WifiConfiguration();

        wifiConfiguration.SSID = "\"" + networkSSID + "\"";

        if (securityMode.equalsIgnoreCase("OPEN")) {

            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        } else if (securityMode.equalsIgnoreCase("WEP")) {

            wifiConfiguration.wepKeys[0] = "\"" + networkPasskey + "\"";
            wifiConfiguration.wepTxKeyIndex = 0;
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        } else if (securityMode.equalsIgnoreCase("PSK")) {

            wifiConfiguration.preSharedKey = "\"" + networkPasskey + "\"";
            wifiConfiguration.hiddenSSID = true;
            wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

        } else {
            Log.i(TAG, "# Unsupported security mode: " + securityMode);

            return null;
        }

        return wifiConfiguration;

    }
public String getScanResultSecurity(ScanResult scanResult) {
        Log.i("TEST", "* getScanResultSecurity");

        final String cap = scanResult.capabilities;
        final String[] securityModes = {"WEP", "PSK", "EAP"};

        for (int i = securityModes.length - 1; i >= 0; i--) {
            if (cap.contains(securityModes[i])) {
                return securityModes[i];
            }
        }

        return "OPEN";
    }


 public int connectToAP(String networkSSID, String networkPasskey) {
        List<ScanResult> scanResultList = wifiManager.getScanResults();
        for (ScanResult result : scanResultList) {

            if (result.SSID.equals(networkSSID)) {

                String securityMode = getScanResultSecurity(result);

                WifiConfiguration wifiConfiguration = createAPConfiguration(networkSSID, networkPasskey, securityMode);

                int res = wifiManager.addNetwork(wifiConfiguration);
                Log.d(TAG, "# addNetwork returned " + res);

                boolean b = wifiManager.enableNetwork(res, true);
                Log.d(TAG, "# enableNetwork returned " + b);

                wifiManager.setWifiEnabled(true);

                boolean changeHappen = wifiManager.saveConfiguration();

                if (res != -1 && changeHappen) {
                    Log.d(TAG, "# Change happen");
                } else {
                    Log.d(TAG, "# Change NOT happen");
                }

                return res;
            }
        }

        return -1;
    }

0 个答案:

没有答案