首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接到WPA/ WPA2 PSK WiFi网络

连接到WPA/ WPA2 PSK WiFi网络
EN

Stack Overflow用户
提问于 2014-04-10 13:42:39
回答 1查看 3.8K关注 0票数 2

我已经通过了几个关于stackoverflow本身和其他博客和网站的线程,了解如何通过编程连接到WiFi网络。我已经得到了他们的指南,并开发了一个代码片段,发布在下面。我的地方有一个安全的WPA/WPA2PSK安全的WiFi网络,我想知道的是,我能通过这个片段连接到WiFi网络吗?我暂时连接不上。我在这里做错了什么?我能做些什么才能继续呢?任何建议和帮助都是非常感谢的。

谢谢。

这是我的代码片段。

代码语言:javascript
复制
try {
            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID           = "\"" + SSID + "\"";
            conf.hiddenSSID     = false;
            conf.wepKeys[0]     = "\\" + networkPass + "\\";
            conf.wepTxKeyIndex  = 0;

            conf.preSharedKey = "\\" + networkPass + "\\";
                conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

            conf.status = WifiConfiguration.Status.ENABLED;
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);              

            mainWifi.addNetwork(conf);
            mainWifi.saveConfiguration();

            List<WifiConfiguration> list = mainWifi.getConfiguredNetworks();
            for( WifiConfiguration i : list ) {
                if(i.SSID != null && i.SSID.equals("\"" + SSID + "\"")) {
                    mainWifi.disconnect();

                    Log.d("RSSI_VALUE", "NET_ID " + String.valueOf(i.networkId));

                    boolean enable = mainWifi.enableNetwork(i.networkId, true);
                    Log.d("RSSI_VALUE", "ENABLE_WIFI " + String.valueOf(enable));

                    boolean recon = mainWifi.reconnect();         
                    Log.d("RSSI_VALUE", "RECONN_WIFI " + String.valueOf(recon));

                    break;
                }           
             }
        } catch (Exception e) {
            e.printStackTrace();
        }            }
EN

回答 1

Stack Overflow用户

发布于 2014-09-26 17:22:06

您已经混合了WEP和PSK安全模式的代码,您应该尝试以下代码:

代码语言:javascript
复制
    if(scanResultSecurity.equals(Constants.WEP)){
                                            wifiConfiguration.SSID = "\"" + networkSSID + "\""; 
                                            wifiConfiguration.wepKeys[0] = "\"" + password + "\"";
                                            wifiConfiguration.wepTxKeyIndex = 0;
                                            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                                            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                                            //Adds to the list of network and returns the network id which can be used to enable it later.
                                            networkId = wifiManager.addNetwork(wifiConfiguration);
                                            if(networkId != -1){
                                                connectWifi(wifiConfiguration);
                                            }

                                        }
                                        else if(scanResultSecurity.equals(Constants.PSK)){
                                            wifiConfiguration.SSID = "\"" + networkSSID + "\""; 
                                            wifiConfiguration.preSharedKey = "\"" + password + "\"";
                                            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_PSK);
                                            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                                            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                                            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                                            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                                          //Adds to the list of network and returns the network id which can be used to enable it later.
                                            networkId = wifiManager.addNetwork(wifiConfiguration); 
                                            if(networkId != -1){
                                                connectWifi(wifiConfiguration);
                                            }
                                        }


private void connectWifi(WifiConfiguration wifiConfiguration){
        wifiManager.disconnect();
        wifiManager.setWifiEnabled(true);
        boolean enableNetworkBoolean = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
        Log.i(Constants.LOG_TAG, "networkId "+wifiConfiguration.networkId);
        Log.i(Constants.LOG_TAG, "SSID "+wifiConfiguration.SSID);
        Log.i(Constants.LOG_TAG, enableNetworkBoolean+" enableNetworkBoolean");
        boolean reconnectBoolean = wifiManager.reconnect();
        Log.i(Constants.LOG_TAG, reconnectBoolean+" reconnectBoolean");
        boolean changeHappen = wifiManager.saveConfiguration();
        Log.i(Constants.LOG_TAG, changeHappen+" changeHappen");
        if(enableNetworkBoolean && reconnectBoolean && changeHappen){
            Log.i(Constants.LOG_TAG, "Change Happen" );     
            Utility.showToast(MainActivity.this, Constants.CONNECTED);
        }
        else{
            Log.i(Constants.LOG_TAG, "Change not Happen" );
            Utility.showToast(MainActivity.this, Constants.CONNECTING_ERROR);
        }                   
    }




  Hope this works for you :-)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22979505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档