简体   繁体   English

Android获取当前连接的wifi网络的SSID

[英]Android get SSID of currently connected wifi network

Several people have asked questions regarding getting the SSID, all of them only partionly work. 有几个人提出了有关获取SSID的问题,所有这些都只能部分起作用。 According to the Android API wifiInfo.getSSID() should return a string, but no matter what I do the if statement returns false. 根据Android API,wifiInfo.getSSID()应该返回一个字符串,但是无论我做什么,if语句都将返回false。 I want to check if my phone is connected to "DieKantankys" 我想检查我的手机是否连接到“ DieKantankys”

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    //Log.d("wifiInfo", wifiInfo.toString());
    //Log.d("NetworkId",wifiInfo.getNetworkId());

    if(wifiInfo.getSSID()=="DieKantankys"){
        setContentView(R.layout.group_choose_activity);
    }else{
        setContentView(R.layout.not_connected_to_scouting_wifi_error);
    }

}

What am I doing wrong? 我究竟做错了什么?

but no matter what I do the if statement returns false. 但是无论我做什么,if语句都将返回false。

At first, when you are comparing Strings, you have to use equals() because you want to compare values and not references: 首先,在比较字符串时,必须使用equals(),因为要比较值而不是引用:

if (wifiInfo.getSSID().equals("DieKantankys")) {
   // do your stuff
}

For that reason it did't work for you. 因此,它对您不起作用。 Your current scenario will always return false because you're comparing String references with == 您当前的方案将始终返回false,因为您正在将String引用与==进行比较

Note: Sometimes is very "handy" to use equalsIgnoreCase() - with an usage of this method, comparison is case-insensitive. 注意:有时使用equalsIgnoreCase()非常“方便”-使用此方法时,比较不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM