如何以编程方式连接 Android 9 (api 28) 中的特定 Wi-Fi 网络

问题描述

我想以编程方式连接 Android 9 (api 28) 中的特定 Wi-Fi 网络。 我尝试使用以下代码进行连接。

 public boolean ConnectToNetworkWPA(String networkSSID,String password) {
                    try {
                        WifiManager wifiManager = (WifiManager) 
                        this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                        boolean b = wifiManager.isWifiEnabled();
                        if (!b) {
                            wifiManager.setWifiEnabled(true);
                        } 
                        WifiConfiguration conf = new WifiConfiguration();
                        conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain SSID in quotes
            
                        conf.preSharedKey = "\"" + password + "\"";
                        conf.status = WifiConfiguration.Status.ENABLED;
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                        conf.priority = 40;
            
                        Log.d("connecting",conf.SSID + " " + conf.preSharedKey);
            
                        int netid=wifiManager.addNetwork(conf);
            
                        Log.d("after connecting",conf.SSID + " " + conf.preSharedKey);
            
                        List<WifiConfiguration> list = wifiManager.getConfigurednetworks();
                        for( WifiConfiguration i : list ) {
                              if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                                wifiManager.disconnect();
                                wifiManager.enableNetwork(i.networkId,true);
                                wifiManager.reconnect();
                                Log.d("re connecting",i.SSID + " " + conf.preSharedKey);
            
                                break;
                            }
                        }
                        //WiFi Connection success,return true
                        return true;
                    } catch (Exception ex) {
                        System.out.println(Arrays.toString(ex.getStackTrace()));
                        return false;
                    }
                }

我在 android 7 中尝试了这段代码,它在 android 9 中工作但在 android 9 中不起作用。根据 android 文档,这些方法仍然适用于 API 级别 28。

感谢您的任何想法和帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)