简体   繁体   English

Android - 连接到指定的 wifi 并检查 SSID

[英]Android - Connecting to specified wifi and check SSID

I tried to check that mobile is connecting to required wifi are not?我试图检查手机是否连接到所需的 wifi 不是? So, I use this code.所以,我使用这个代码。

(wifiInfo.getSSID().equals("WiredSSID"))

but it doesn't work.但它不起作用。 When I try to make toast name, it still shows "WiredSSID".当我尝试制作吐司名称时,它仍然显示“WiredSSID”。 So, I don't know what point is wrong.所以,我不知道哪一点是错的。

Moreover, how can I connect to Captive Portal Wifi Automatically.此外,如何自动连接到 Captive Portal Wifi。 I use the code from How do I connect to a specific Wi-Fi network in Android programmatically?我使用如何以编程方式连接到 Android 中的特定 Wi-Fi 网络中的代码?

make it to OPEN type (conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);) and put it in OnCreate function, but it cant work.将其设为 OPEN 类型 (conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);) 并将其放入 OnCreate 函数中,但它无法工作。 I don't know that I should change network type or not?我不知道我是否应该更改网络类型? Please help me, thank you请帮帮我,谢谢

You can try the below code snippet to check for available wifi networks and get connected to a specified wifi network您可以尝试以下代码片段来检查可用的 wifi 网络并连接到指定的 wifi 网络

         List<ScanResult> wifiScanList = wifi.getScanResults();
         wifis = new String[wifiScanList.size()];

         for(int i = 0; i < wifiScanList.size(); i++){
            wifis[i] = ((wifiScanList.get(i)).SSID);                

            if(wifis[i].equals("WiredSSID")) {

                 WifiConfiguration wifiConfig = new WifiConfiguration();
                 wifiConfig.SSID = String.format("\"%s\"", wifis[i]);
                 wifiConfig.preSharedKey = String.format("\"%s\"", "password");

                 WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
                 //remember id
                 int netId = wifiManager.addNetwork(wifiConfig);
                 wifiManager.disconnect();
                 wifiManager.enableNetwork(netId, true);
                 wifiManager.reconnect();
             }
         }
String networkSSID = "WiredSSID";
(wifiInfo.getSSID().equals("\"" + networkSSID + "\""))

Please note the quotes.请注意引号。 String should contain ssid in quotes.字符串应该在引号中包含 ssid。

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

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