在android中连接到wifi网络,如果密码不正确则返回

Wil*_*son 5 android connect wifi

我想创建一个允许用户连接到wifi网络的应用程序,但是我无法连接到网络

我目前的代码是:

    WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE);
    wifi.setWifiEnabled(true);
    WifiConfiguration wc = new WifiConfiguration();
    wifi.startScan();
    List<ScanResult> l=wifi.getScanResults();
    wc.SSID = l.get(NUMBER).SSID;
    post(wc.SSID);
    /*This is the bit that I think is failing, my network does not have these properties.. but I can't see how to get them from the Scan Result*/
    wc.preSharedKey  = "\"passw0rd123\"";
    wc.hiddenSSID = false;
    wc.status = WifiConfiguration.Status.ENABLED;        
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    int res = wifi.addNetwork(wc);
    post("add Network returned " + res);
    boolean b = wifi.enableNetwork(res, true);        
    post("enableNetwork returned " + b);
Run Code Online (Sandbox Code Playgroud)

我认为这与设置(在我的评论之后)有关,这与我的网络配置不同,但我不知道如何从ScanResult获取这些设置.

编辑:我也想知道它是否已正确连接.

Szy*_*ski -1

确保在 AndroidManifest.xml 文件中设置正确的权限