How to connect to a WiFi network with an unknown encryption algorithm in Android?

I researched this question in StackOverflow, but all the answers indicate how to connect to a network with a well-known encryption algorithm (mainly WEP). In my application, I retrieve a list of available Wi-Fi networks and display them in a ListView (using WifiManager ). When a user clicks on one of the items in the list, I want to connect to the network.

My current implementation is trying to get WifiConfiguration data from the ScanResult String capabilities. For example, these are all real feature strings that were received:

 [WPA2-PSK-CCMP][ESS] [WPA2-PSK-CCMP+TKIP][ESS] [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] 

I suggested, based on some research, that these are functions separated by brackets, and the first element for each of them is a highlighted line showing:

 [Authentication Algorithm - Key Management Algorithm - Pairwise Cipher] 

I will analyze this data and then create a WifiConfiguration object, then try to connect to it, but it always fails ( addNetwork returns -1 ). What am I doing wrong? Here is my code:

 @Override public void onItemClick(AdapterView<?> adapter, View parent, int position, long id) { ScanResult result = (ScanResult) adapter.getItem(position); WifiConfiguration config = new WifiConfiguration(); String currentNetwork = mWifiManager.getConnectionInfo().getSSID(); if (currentNetwork != null && currentNetwork.equals(result.SSID)) { Toast.makeText(this, "Already connected", Toast.LENGTH_SHORT).show(); return; } config.BSSID = result.BSSID; config.SSID = result.SSID; String firstCapabilities = result.capabilities.substring(1, result.capabilities.indexOf("]")-1); String[] capabilities = firstCapabilities.split("-"); String auth = capabilities[0]; String keyMgmt = capabilities[1]; String pairwiseCipher = capabilities[2]; int a = 0; if (auth.contains("EAP")) a |= WifiConfiguration.AuthAlgorithm.LEAP; else if (auth.contains("WPA")) a |= WifiConfiguration.AuthAlgorithm.OPEN; else if (auth.contains("WEP")) a |= WifiConfiguration.AuthAlgorithm.SHARED; config.allowedAuthAlgorithms.set(a); int k = WifiConfiguration.KeyMgmt.NONE; if (keyMgmt.contains("IEEE802.1X")) k |= WifiConfiguration.KeyMgmt.IEEE8021X; else if (auth.contains("WPA") && keyMgmt.contains("EAP")) k |= WifiConfiguration.KeyMgmt.WPA_EAP; else if (auth.contains("WPA") && keyMgmt.contains("PSK")) k |= WifiConfiguration.KeyMgmt.WPA_PSK; config.allowedKeyManagement.set(k); int c = WifiConfiguration.PairwiseCipher.NONE; if (pairwiseCipher.contains("CCMP")) c |= WifiConfiguration.PairwiseCipher.CCMP; if (pairwiseCipher.contains("TKIP")) c |= WifiConfiguration.PairwiseCipher.TKIP; config.allowedPairwiseCiphers.set(c); int networkId = mWifiManager.addNetwork(config); if (networkId == -1) { //always hits this line! Toast.makeText(this, "Failed to create network configuration", Toast.LENGTH_SHORT).show(); } else { //Never reaches here! mWifiManager.disconnect(); mWifiManager.enableNetwork(networkId, true); mWifiManager.reconnect(); } } 
+1
source share
1 answer

For WPA *, unless you set preSharedKey to 8 or more characters, this will not be done with -1. I don’t see you configure it at all.

+1
source

All Articles