1

Ok so this may be a very stupid question but I honestly can't quite figure out what's going on here, I'm trying to compare the current SSID to another variable like this:

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String SSID = wifiInfo.getSSID().replace("SSID: ","");
if(myWifi.equals(SSID)){

} else {

}

Now, as you know, wifiInfo.getSSID() returns a String like this SSID: wifi1. Let's say myWifi's value is wifi1, if it gets compared to wifiInfo.getSSID() it will say it's false because it is, so I'm trying to replace the SSID: part for nothing, hopefully getting only the SSID name (wifi1), so the strings can be compared and return true. But using the code above makes the SSID variable have the value of "wifi1", and so the app says it's false because "wifi1" is not the same as wifi1.

Why is this happening?

user1676874
  • 875
  • 2
  • 24
  • 45

2 Answers2

5

Simple just use .replaceAll("\"",""); after removing the SSID part. So you will have:

String SSID = wifiInfo.getSSID().replace("SSID: ","").replaceAll("\"","");

This will remove all the " no matter where they are in the String.

Alex Newman
  • 1,369
  • 12
  • 34
  • 1
    One thing to also note is that on some versions of Android, the SSID has extra quotation marks and on some versions it doesn't. See here: http://stackoverflow.com/questions/13563032/jelly-bean-issue-wifimanager-getconnectioninfo-getssid-extra – Stephen Ruda Jan 06 '17 at 19:09
  • Well this definitely works. No idea why replace would add the quotation marks though. – user1676874 Jan 06 '17 at 19:10
  • 1
    The answer linked by @StephenRuda should work as well actually. – Alex Newman Jan 06 '17 at 19:13
-1

I would use a String.Contains to check for the string you want.

bool wifiNameExists = wifiInfo.Contains("wifi1")

You can ofcourse replace "wifi1" with a string containing the wifi name you wish to search for.

JavaadPatel
  • 97
  • 11
  • True. Per OP's code snippet you can use: `if (SSID.contains(myWifi))`. But then if you want to save the SSID you'd better remove the `"` as explained in my answer above. – Alex Newman Jan 06 '17 at 19:17
  • @AlexNewman not just that - with this answer "hellogigglehello" would be accepted even if you're looking for a wifi named "hello" too. Also, I believe it's should be wifiInfo.getSSID().contains – Abraham Philip Apr 09 '17 at 11:33