Skip to content

Commit

Permalink
fix(connection-error): Minor fixes for connection errors
Browse files Browse the repository at this point in the history
see #57 and #81
  • Loading branch information
hypery2k committed Jan 31, 2018
1 parent a15b012 commit 891e546
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/android/com/mady/wifi/api/WifiHotSpots.java
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2013-2014 www.Andbrain.com
/*
* Copyright (C) 2013-2014 www.Andbrain.com
* Faster and more easily to create android apps
*
*
* */
package com.mady.wifi.api;

Expand All @@ -22,6 +22,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class WifiHotSpots {
/**
Expand Down Expand Up @@ -98,19 +99,22 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti
isConnectToHotSpotRunning = true;
WifiConfiguration wifiConf = new WifiConfiguration();
if (authentication != null && encryptions != null && encryptions.length > 0) {
removeWifiNetwork(netSSID);
if (WifiConfiguration.AuthAlgorithm.LEAP == authentication.intValue()) {

wifiConf.SSID = "\"" + netSSID + "\"";
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
for (int cipher : encryptions) {
wifiConf.allowedGroupCiphers.set(cipher);
}
mWifiManager.setWifiEnabled(true);
int res = mWifiManager.addNetwork(wifiConf);
mWifiManager.saveConfiguration();
if (res == -1) {
res = getExistingNetworkId(netSSID);
}
mWifiManager.disconnect();
mWifiManager.enableNetwork(res, true);
mWifiManager.reconnect();
mWifiManager.setWifiEnabled(true);
isConnectToHotSpotRunning = false;
return true;
} else if (WifiConfiguration.AuthAlgorithm.SHARED == authentication.intValue()) {
Expand All @@ -122,11 +126,15 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti
for (int cipher : encryptions) {
wifiConf.allowedGroupCiphers.set(cipher);
}
mWifiManager.setWifiEnabled(true);
int res = mWifiManager.addNetwork(wifiConf);
mWifiManager.saveConfiguration();
if (res == -1) {
res = getExistingNetworkId(netSSID);
}
mWifiManager.disconnect();
mWifiManager.enableNetwork(res, true);
mWifiManager.reconnect();
mWifiManager.setWifiEnabled(true);
isConnectToHotSpotRunning = false;
return true;

Expand All @@ -144,12 +152,15 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
mWifiManager.setWifiEnabled(true);
int res = mWifiManager.addNetwork(wifiConf);
mWifiManager.saveConfiguration();
if (res == -1) {
res = getExistingNetworkId(netSSID);
}
mWifiManager.disconnect();
mWifiManager.enableNetwork(res, true);
mWifiManager.reconnect();
mWifiManager.saveConfiguration();
mWifiManager.setWifiEnabled(true);
isConnectToHotSpotRunning = false;
return true;

Expand All @@ -161,20 +172,23 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti

for (ScanResult result : scanResultList) {

if (result.SSID.equals(netSSID)) {
if (result.SSID.contains(netSSID)) {

removeWifiNetwork(result.SSID);
String mode = getSecurityMode(result);

if (mode.equalsIgnoreCase("OPEN")) {
Log.i(LOG_TAG, "Connecting to hotspot with security: OPEN");
wifiConf.SSID = "\"" + netSSID + "\"";
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
mWifiManager.setWifiEnabled(true);
int res = mWifiManager.addNetwork(wifiConf);
mWifiManager.saveConfiguration();
if (res == -1) {
res = getExistingNetworkId(netSSID);
}
mWifiManager.disconnect();
mWifiManager.enableNetwork(res, true);
mWifiManager.reconnect();
mWifiManager.setWifiEnabled(true);
isConnectToHotSpotRunning = false;
return true;

Expand All @@ -186,11 +200,15 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti
wifiConf.wepTxKeyIndex = 0;
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
mWifiManager.setWifiEnabled(true);
int res = mWifiManager.addNetwork(wifiConf);
mWifiManager.saveConfiguration();
if (res == -1) {
res = getExistingNetworkId(netSSID);
}
mWifiManager.disconnect();
mWifiManager.enableNetwork(res, true);
mWifiManager.reconnect();
mWifiManager.setWifiEnabled(true);
isConnectToHotSpotRunning = false;
return true;

Expand All @@ -208,12 +226,15 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
mWifiManager.setWifiEnabled(true);
int res = mWifiManager.addNetwork(wifiConf);
mWifiManager.saveConfiguration();
if (res == -1) {
res = getExistingNetworkId(netSSID);
}
mWifiManager.disconnect();
mWifiManager.enableNetwork(res, true);
mWifiManager.reconnect();
mWifiManager.saveConfiguration();
mWifiManager.setWifiEnabled(true);
isConnectToHotSpotRunning = false;
return true;

Expand All @@ -226,6 +247,19 @@ public boolean connectToHotspot(String netSSID, String netPass, Integer authenti
return false;
}

private int getExistingNetworkId(String SSID) {
List<WifiConfiguration> configuredNetworks = mWifiManager.getConfiguredNetworks();
if (configuredNetworks != null) {
for (WifiConfiguration existingConfig : configuredNetworks) {
if (Pattern.matches("\"" + SSID + "\"", existingConfig.SSID) ||
Pattern.matches(SSID, existingConfig.SSID)) {
return existingConfig.networkId;
}
}
}
return -1;
}

/**
* Check if The Device Is Connected to Hotspot using wifi
*
Expand Down

0 comments on commit 891e546

Please sign in to comment.