I am trying to develop a small app where I can scan and connect to the WiFi hot-spots from scanned list of networks. But for both, Open and Secure networks I have written a password prompt and if the network is open (by knowing it beforehand) I do not enter password and keep the text blank and enter and then it connects. Can anyone tell how to identify programmatically open and secure wifi so that I won't ask for password for Open network and let it connect directly. (I won't be knowing which is Open and Which is secure network in future, so we need to identify open and secure network from SSID or something)
Asked
Active
Viewed 3,947 times
2
-
Possibly duplicate http://stackoverflow.com/questions/6866153/android-determine-security-type-of-wifi-networks-in-range-without-connecting-t – dasar Sep 04 '14 at 10:20
3 Answers
8
Here you have capabilities field which is used to identify the network type
WifiManager wifimanger = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> networkList = wifimanger.getScanResults();
if (networkList != null) {
for (ScanResult network : networkList) {
String capabilities = network.capabilities;
Log.w(TAG, network.SSID + " capabilities : " + capabilities);
if (capabilities.toUpperCase().contains("WEP")) {
// WEP Network
} else if (capabilities.toUpperCase().contains("WPA")
|| capabilities.toUpperCase().contains("WPA2")) {
// WPA or WPA2 Network
} else {
// Open Network
}
}
}

HotIceCream
- 2,271
- 2
- 19
- 27

raji ramamoorthi
- 443
- 5
- 7
-
After `capabilities.toUpperCase().contains("WPA")`, `|| capabilities.toUpperCase().contains("WPA2")` is redundant. – Sandor Mezil Sep 12 '19 at 10:51
1
Use this filter function to differentiate.
private boolean isProtectedNetwork(String capability){
return (capability.contains("WPA") ||
capability.contains("WEP") ||
capability.contains("WPS")
);
}
So when you have hold of all the List. Just iterate through it and add them in two different lists. (One for open and one for secure networks).
Here is the code for that
private void filterScan(List<ScanResult> allScanResults){
List<ScanResult>openScans = new ArrayList<ScanResult>();
List<ScanResult>closeScans = new ArrayList<ScanResult>();
for(ScanResult result : allScanResults)
{
if(!isProtectedNetwork(result.capabilities))
{
openScans.add(result);
}
else {
closeScans.add(result);
}
}
}
Useful Resource:
You can find more related solutions on My Github Repository

Rohit Singh
- 16,950
- 7
- 90
- 88
0
Today I was looking for an answer to this same question, but the solution pointed out here and in other similar questions made me a little insecure. And if a new security mode is created in the future?
For now I prefer to do something like:
public WifiSecurityMode testAgainstSecurityModes(WifiSecurityMode... securities) {
for (WifiSecurityMode security : securities) {
if (this.capabilities.toUpperCase().contains(security.getName().toUpperCase())) {
return security;
}
}
return WifiSecurityMode.UNKNOWN;
}
public enum WifiSecurityMode {
WEP("WEP"), WPA("WPA"), WPA2("WPA2"),//....
UNKNOWN ("UNKNOWN");
private String name;
WifiSecurityMode (String name){
this.name = name;
}
public String getName() {
return name;
}
}
//to use it: (WifiInfo are just a class I create to encapsulate the values in a ScanResult object)
public List<WifiInfo> getOpenWifis() {
List<WifiInfo> open = new ArrayList<>();
for (WifiInfo w : wifiInfoList) {
if (w.testAgainstSecurityModes(WifiSecurityMode.WEP, WifiSecurityMode.WPA, WifiSecurityMode.WPA2).equals(WifiSecurityMode.UNKNOWN)) {
open.add(w);
}
}
return open;
}
After that, you do what you want with UNKNOWN return.
If a new security mode be created in the future, you will not need to change testAgainstSecurityModes method.

alexpfx
- 6,412
- 12
- 52
- 88