2012-01-30 58 views
0

我無法連接Android上建立的SSID,我嘗試WifiConfiguration但未完成,我的ssid已打開(無密碼)。如何將Android連接至無SSID public並且無密碼要求

在Android UI中,我可以連接,但在代碼上我不能。

有人幫我嗎?

Tankyou。

編輯:

我的代碼:

String networkSSID = "DL63.0.0.0.1"; 
     String networkPass = ""; 

     WifiConfiguration conf = new WifiConfiguration(); 
     conf.SSID = "\"" + networkSSID + "\""; 
//  conf.wepKeys[0] = "\"" + networkPass + "\""; 
//  conf.wepTxKeyIndex = 0; 
     conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
//  conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

     WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
     wifiManager.addNetwork(conf); 

     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(); 

       Toast.makeText(this, "SSID: "+wifiManager.getConnectionInfo().getSSID(), Toast.LENGTH_SHORT).show(); 
       break; 
      } 
     } 

     status.setText("SSID: "+wifiManager.getConnectionInfo().getSSID()); 
+0

請顯示您的代碼...你究竟試過了什麼? – Yahia 2012-01-30 19:30:17

+0

我嘗試這兩個代碼(答案2和3):http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically – 2012-01-30 19:41:55

+0

究竟發生了什麼?任何異常/錯誤消息? – Yahia 2012-01-30 19:43:45

回答

0

大概是因爲wifiManager.reconnect();啓動異步進程進行連接。當你的代碼到達最後一行時,還沒有連接(需要時間,甚至可能是一分鐘左右),所以null是當時的正確答案。您需要一個偵聽器才能對連接事件做出反應。看到這個問題的細節:https://stackoverflow.com/a/5890553/1503237

相關問題