我已经通过stackoverflow本身以及其他关于以编程方式连接到WiFi网络的博客和网站上的几个主题。我有他们的指南,并开发了一个代码片段,发布在下面。我在我的地方有一个安全的WPA / WPA2 PSK安全WiFi网络,我想知道的是,我能通过这个片段连接到WiFi网络吗?我暂时无法联系。我在这做错了什么?我该怎么做才能继续?任何建议和帮助都非常感谢。
谢谢。
这是我的代码片段。
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();
} }
答案 0 :(得分:2)
您已经混合了WEP和PSK安全模式的代码,您应该尝试以下代码:
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 :-)