Jelly Bean Issue - wifiManager.getConnectionInfo().getSSID() - extra ""
Asked Answered
G

5

25

Hi all bug reporting for your information. link

Problem details:

The Code - wifiManager.getConnectionInfo().getSSID()

The above code to returns the current SSID, it is returning the current SSID with extra quotations around it.

For eg. the SSID internet is returned as "internet".

This is only seen on Jelly bean 4.2 using device Nexus 7.

This bug is causing errors in our app as we compare the current SSID with the SSID that we are trying to connect too.

The code wifiManager.getScanResults(); however still returns all SSID's without extra quotation marks.


Garotte answered 26/11, 2012 at 10:36 Comment(3)
Yeap, faced this issue few days ago. Not sure if it's issue or designed with purpose. Hope they will not "fix" it, since it will broke a lot of code!Sanches
same problem with android 4.3 update on samsung galaxy note2. There were no quotes in the 4.1.2 version on the note2.Danielladanielle
It also happens with android 4.4.2Samoyedic
F
32

this is not a bug and behavior is correct as per documentation at http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID()

The so-called bug apparently was in pre 4.2 devices, because they didn't return it with "" enclosure.

Aiden's method looks good to me in the current state of confusion left by Android. However, being theoritically correct would just require

if (ssid.startsWith("\"") && ssid.endsWith("\"")){
             ssid = ssid.substring(1, ssid.length()-1);
}
Ferocity answered 21/2, 2013 at 0:53 Comment(0)
M
9

This regular expression is quite neat:

String ssid = wi.getSSID().replaceAll("^\"(.*)\"$", "$1");

Just for the notes

Edit °1 (as per question in the comment):

The issue that OP describes is, that on some devices the SSID returned by getSSID() is enclosed in "" whereas it is not on other devices. E.g. on some devices the SSID is "MY_WIFI" and on others it is MY_WIFI - or spoken in Java code: "\"MY_WIFI\"" and "MY_WIFI".

In order to to unify both results I proposed to remove the " at start and end - only there, because " is a legal character inside the SSID. In the regular expression above

^ means from start
$ means at end
\" means " (escaped)
.* means any number of characters
(...) means a capturing group, that can be referred by $1

So the whole expression means: replace "<something>" by <something> where $1 = <something>. If there is no " at end/start, the regular expression doesn't match and nothing is replaced.

See Java Pattern class for more details.

Microbe answered 21/11, 2013 at 12:56 Comment(1)
It is neat, but, it would help us to understand what it does, if you would add some explanations. The OP has no clear question as well.External
G
6

For the mean time this is how I am getting around it, although its not great it will fix the issue.

 public String removeQuotationsInCurrentSSIDForJellyBean(String ssid){
     int deviceVersion= Build.VERSION.SDK_INT;

     if (deviceVersion >= 17){
         if (ssid.startsWith("\"") && ssid.endsWith("\"")){
             ssid = ssid.substring(1, ssid.length()-1);
         }
     }

     return ssid;

 }
Garotte answered 26/11, 2012 at 10:36 Comment(0)
S
4

Two very simple variants:

string = string.replaceAll("^\" | \"$", "");

and

string = string.substring(1, string.length() - 1);
Sanches answered 27/11, 2012 at 9:9 Comment(3)
be careful with these as it is possible for valid SSID's to have quotes within them ie. "my "freeking" wifi". Also the second varient is great but you need the check mentioned in my answer to make sure you don't mess up anything else.Garotte
@AidenFry I don't think the regex would affect the example you provided, since it only check the starting char and ending char (with ^ and $). The function would only return String with quotes or hex digits according to the doc, so it only eliminates quotes rounded by the function but would keep the quotes of the original String. The problem with the regex is it should not have empty space after/before the quotes. e.g. ("^\"|\"$")Vicar
It is neat, but, it would help us to understand what it does, if you would add some explanations. The OP has no clear question as well.External
O
0

Faced the same problem! Used this technique which is backwards compatible:

if (suppliedSSID.equals(connectionInfo.getSSID()) || ("\"" + suppliedSSID + "\"").equals(connectionInfo.getSSID()) { DO SOMETHING }

Oleic answered 13/1, 2014 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.