我使用下面的代码在android手机上创建一个无线模式。出于某种原因它每次都会创建一个新模式,因此对于相同的无线网络手机将会有多个模式。是否有办法能实现当一个模式已经存在时,就不要再创建一个新的模式?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);
2 回答
收到一只叮咚
TA贡献1821条经验 获得超4个赞
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));
添加回答
举报
0/150
提交
取消