gpt4 book ai didi

Java - 如果无线网络已经存在,如何连接?

转载 作者:行者123 更新时间:2023-11-30 09:38:12 25 4
gpt4 key购买 nike

我正在使用下面的代码在 Android 手机上创建无线配置文件。出于某种原因,它每次都会创建一个新的配置文件,因此手机将拥有同一无线网络的多个配置文件。如果它已经存在,有没有办法让它不创建新的配置文件?

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.TextView;

public class IVMOBILEActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
TextView status = new TextView(this);


WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"MyNetwork\""; //IMP! This should be in Quotes!!

wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.priority = 10;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.preSharedKey = "\"Password\"";
WifiManager wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE);
boolean res1 = wifiManag.setWifiEnabled(true);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean es = wifi.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es );
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b );

tv.setText("You are now connected! " +
"Version 1.0");

status.setText("The was an error connecting, please try again.");

try {

Thread.sleep(5000);

ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){
//You are connected, do something online.
setContentView(tv);

}else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
//Not connected.
setContentView(status);
}

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}






}


}

最佳答案

在将其添加到WifiManager 之前先检查它是否存在。伪代码例如:

//Check if this SSID is already stored. If it is, return that configuration.
//If not, return the configuration being tested.
public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
List<WifiConfiguration configs = wifi.getConfigurations();
for(WifiConfiguration config : configs) {
if(config.SSID.equals(wc.SSID)) return config;
}
return wc;
}

//......
int res = wifi.addNetwork(checkPreviousConfiguration(wc));

有道理吗?

编辑:显然,除了 SSID 之外,您可能还想检查其他因素(因为 SSID 不一定是唯一标识符),并在构建新的 WifiConfiguration 之前检查那些因素例如,但这是我要开始的想法。

关于Java - 如果无线网络已经存在,如何连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10215399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com