如何以编程方式连接到android API级别29中的特定wifi网络?

时间:2019-12-31 06:34:54

标签: android-wifi androidx wifimanager android-api-levels

我目前正在我的应用活动中显示可用wifi的列表,但我无法连接到特定网络。 'android.net.wifi.WifiConfiguration'已弃用,这是我遇到的错误,并且由于我正在androidx api级别29上工作,因此我没有找到任何合适的方法来克服此问题。下面是代码我正在使用连接。

String networkPass = "ChangeMe";

                WifiConfiguration wc = new WifiConfiguration();
                // setup a wifi configuration
                wc.SSID = "\"" + items.getDeviceName() + "\"";
                wc.preSharedKey = "\""+networkPass+"\"";
                //wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                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);
                // connect to and enable the connection
                WifiManager wifiManager;
                wifiManager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);

                int netId = wifiManager.addNetwork(wc);
                wifiManager.enableNetwork(netId, true);
                wifiManager.setWifiEnabled(true);

0 个答案:

没有答案