Connect to a Wi-Fi hotspot in the program. If the WiFi hotspot has been configured, it is very simple. there are all textbooks. however, if it is a new Wi-Fi network, the problem arises. as usual, I searched the internet first, if there is a ready-made one. that's the case. the complete code cannot be found. has no connection been successful?
It seems that only self-reliance. Let's take a look at the description of the class library http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#allowedAuthAlgorithms
1. Connect to WEP-encrypted WiFi
Debugging took a lot of time, but still succeeded. The source code is attached.
View code
1 wifimanager WiFi = (wifimanager) getsystemservice (WiFi. This. wifi_service );
2
3 wificonfiguration WC = new wificonfiguration ();
4
5/* --------------------- WEP connection method ---------------------*/
6
7 WC. SSID = "\" SSID \"";
8 WC. hiddenssid = false;
9
10 WC. Status = wificonfiguration. Status. enabled;
11
12 WC. allowedgroupciphers. Set (wificonfiguration. groupcipher. wep104 );
13 WC. allowedauthalgorithms. Set (wificonfiguration. authalgorithm. Open );
14
15 WC. allowedkeymanagement. Set (wificonfiguration. keymgmt. None );
16 WC. weptxkeyindex = 0;
17
18 WC. wepkeys [0] = "psw ";
19
20 int res = wifi. addnetwork (WC );
21
22 Boolean B = wifi. enablenetwork (Res, true );
23
24/* --------------------- WEP connection method end ---------------------*/
25
26
27
2. Connect WPA encrypted Wi-Fi
View code
1 wificonfiguration WC = new wificonfiguration ();
2
3/* ---------------------- WPA connection method ------------------------*/
4 WC. SSID = "\" SSID \"";
5 WC. hiddenssid = false;
6
7 WC. Status = wificonfiguration. Status. enabled;
8
9
10 WC. allowedauthalgorithms. Set (wificonfiguration. authalgorithm. Open );
11 WC. allowedprotocols. Set (wificonfiguration. Protocol. WPA );
12 WC. allowedkeymanagement. Set (wificonfiguration. keymgmt. wpa_psk );
13 WC. allowedpairwiseciphers. Set (wificonfiguration. pairwisecipher. TKIP );
14
15 WC. presharedkey = "\" psw \"";
16
17 int res = wifi. addnetwork (WC );
18
19 Boolean B = wifi. enablenetwork (Res, true );
20/* ---------------------- WPA connection method end ------------------------*/
Also attached to my wifi configuration. Wireless routing is a TP-LINK, a more common router.
Today, I encountered a password-free situation, but cannot connect. strange. continue the study. if you have been connected to a database without a password, please do not hesitate to enlighten me for one night's debugging. finally connected. encryption-free WiFi focuses on several parameter settings and continues to include code
Connectionunconfigfornopsw
Public Boolean connectionunconfigfornopsw (string SSID)
{
Try
{
Wificonfiguration WC = new wificonfiguration ();
WC. hiddenssid = false;
WC. Status = wificonfiguration. Status. enabled;
WC. allowedkeymanagement. Set (keymgmt. wpa_psk );
WC. allowedkeymanagement. Set (keymgmt. wpa_eap );
WC. allowedprotocols. Set (protocol. WPA );
WC. allowedprotocols. Set (protocol. RSN );
WC. allowedpairwiseciphers. Set (pairwisecipher. TKIP );
WC. allowedpairwiseciphers. Set (pairwisecipher. CCMP );
WC. allowedgroupciphers. Set (groupcipher. wep40 );
WC. allowedgroupciphers. Set (groupcipher. wep104 );
WC. allowedgroupciphers. Set (groupcipher. TKIP );
WC. allowedgroupciphers. Set (groupcipher. CCMP );
WC. SSID = "\" "+ SSID + "\"";
WC. presharedkey = NULL;
Int res = mwifimanager. addnetwork (WC );
Return mwifimanager. enablenetwork (Res, true );
}
Catch (exception ex)
{
Return false;
}
}