2013-04-15 193 views
1

我能夠使用以下code添加網絡,但無法獲得狀態,如果其添加或authentication失敗後EnableNetworkreconnect。請幫忙!!!網絡狀態丟失

mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
      removeNetwork(); 

     Boolean isWifiConnected = false; 

     WifiConfiguration mWifiConf = new WifiConfiguration();   
     mWifiConf.SSID = "\"" + networkList.get(item_pos).SSID + "\""; 
     mWifiConf.hiddenSSID = true; 
     mWifiConf.status = WifiConfiguration.Status.ENABLED; 
     mWifiConf.priority = 1;   
     if(addWPA2Network(mWifiConf, etPassword.getText().toString())<0) 
     { 

     } 
     else 
     { 
      List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks(); 

      for (WifiConfiguration i : list) 
      { 
       if (i.SSID != null && i.SSID.equals("\"" + networkList.get(item_pos).SSID + "\"")) 
       { 
        mWifiManager.disconnect(); 
        isWifiConnected = mWifiManager.enableNetwork(i.networkId, true); 
        isWifiConnected = mWifiManager.reconnect();     

        break; 
       } 
      } 
     } 

回答

0

將網絡的android權限添加到清單文件中。

0

得到了這個鏈接我的答案:https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/wifi/WifiStatusTest.java

if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { 
       handleSupplicantStateChanged(
         (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE), 
         intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR), 
         intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0)); 
      } 

使用此處理程序,以檢查其是否成功或無法接通。