Je veux concevoir une application qui affiche une liste des réseaux wifi disponibles et connectez-vous au réseau lorsqu’il est sélectionné. J’ai mis en place la partie montrant les résultats de l’analyse. Maintenant, je veux me connecter à un réseau particulier sélectionné par l’utilisateur dans la liste des résultats de l’analyse. Quelqu'un peut-il me dire s’il vous plaît comment faire cela ?
Réponses
Trop de publicités?Vous devez créer WifiConfiguration exemple comme ceci:
String networkSSID = "test";
String networkPass = "pass";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
Ensuite, pour le WEP réseau, il faut faire ceci:
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
Pour le WPA du réseau, vous devrez ajouter la phrase comme ceci:
conf.preSharedKey = "\""+ networkPass +"\"";
Pour un réseau Ouvert, vous devez faire ceci:
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Ensuite, vous devez l'ajouter à la wifi Android gestionnaire de paramètres:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
Et enfin, vous pourriez avoir besoin pour l'activer, donc Android conntects:
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
UPD: Dans le cas du WEP, si votre mot de passe est en hexadécimal, vous n'avez pas besoin de l'entourer de guillemets.
La réponse précédente fonctionne, mais la solution peut effectivement être plus simple. Effectuer une boucle sur la liste des réseaux configurés n’est pas nécessaire que vous obtenez l’id de réseau lorsque vous ajoutez le réseau par le biais de la WifiManager.
Donc la solution complète et simplifiée, ressemblerait à quelque chose comme ceci :
**Before connecting WIFI netowork you need to check security type of the WIFI network
ScanResult class has a capabilities. This field gives you type of network
Refer: https://developer.android.com/reference/android/net/wifi/ScanResult.html#capabilities
**There are three types of WIFI networks.**
First, instantiate a WifiConfiguration object and fill in the network's SSID (note that it has to be enclosed in double quotes), set the initial state to disabled, and specify the network's priority (numbers around 40 seem to work well).**
WifiConfiguration wfc = new WifiConfiguration();
wfc.SSID = "\"".concat(ssid).concat("\"");
wfc.status = WifiConfiguration.Status.DISABLED;
wfc.priority = 40;
**Now for the more complicated part: we need to fill several members of WifiConfiguration to specify the network's security mode.
For open networks**
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.clear();
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
**For networks using WEP; note that the WEP key is also enclosed in double quotes.**
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
if (isHexString(password)) wfc.wepKeys[0] = password;
else wfc.wepKeys[0] = "\"".concat(password).concat("\"");
wfc.wepTxKeyIndex = 0;
**For networks using WPA and WPA2, we can set the same values for either**
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wfc.preSharedKey = "\"".concat(password).concat("\"");
**Finally, we can add the network to the WifiManager's known list**
WifiManager wfMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int networkId = wfMgr.addNetwork(wfc);
if (networkId != -1) {
// success, can call wfMgr.enableNetwork(networkId, true) to connect
}