Jelly Bean Issue - wifiManager.getConnectionInfo().getSSID() - extra “”

有些话、适合烂在心里 提交于 2019-12-02 22:06: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);
}

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.

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;

 }

Two very simple variants:

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

and

string = string.substring(1, string.length() - 1);

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!