2017-06-22 82 views
0

我需要這個代碼programaticly連接到新的wifi網絡到設備: `新網絡添加到Android設備programsticly

try 
{ 
    String ssid = "\"" + SSID + "\""; 
    String pass = "\"" + Pass + "\""; 

    for (ScanResult result : results) 
    { 
     if (result.SSID.equals(SSID)) 
     { 
      String security = getScanResultSecurity(result); 
      if (security.equals("PSK")) { 

        WifiConfiguration con = new WifiConfiguration(); 
        con.SSID = ssid; 
        AlertDialog a = new AlertDialog.Builder(MainActivity.this).create(); 
        a.setMessage("in"); 
        a.show(); 
        con.preSharedKey = pass; 
        con.hiddenSSID = true; 
        con.status = WifiConfiguration.Status.ENABLED; 
        con.allowedGroupCiphers.set(WifiConfiguration.Grou pCipher.TKIP); 
        con.allowedGroupCiphers.set(WifiConfiguration.Grou pCipher.CCMP); 
        con.allowedKeyManagement.set(WifiConfiguration.Key Mgmt.WPA_PSK); 
        con.allowedPairwiseCiphers.set(WifiConfiguration.P airwiseCipher.TKIP); 
        con.allowedPairwiseCiphers.set(WifiConfiguration.P airwiseCipher.CCMP); 
        con.allowedKeyManagement.set(WifiConfiguration.Key Mgmt.NONE); 
        con.allowedProtocols.set(WifiConfiguration.Protoco l.RSN); 
        con.allowedProtocols.set(WifiConfiguration.Protoco l.WPA); 

        int ntid = wifimanager.addNetwork(con); 

        wifimanager.disconnect(); 
        wifimanager.enableNetwork(ntid,true); 
        wifimanager.reconnect(); 
        boolean b = wifimanager.saveConfiguration(); 
        if (ntid != -1 && b) { 

         AlertDialog a2 = new AlertDialog.Builder(MainActivity.this).create(); 
         a2.setMessage("saved"); 
         a2.show(); 
        } 
       } 

     } 
    } 
    catch (Exception ex) { 
     AlertDialog a = new AlertDialog.Builder(MainActivity.this).create(); 
     a.setMessage(ex.getMessage()); 
     a.show(); 
    } 

'
但我不能添加網絡到我的設備 問題與網絡配置不能添加到網絡 我無法理解,爲什麼不工作沒有erorr但沒有結果 幫我請

+0

你的日誌說什麼? – Aenadon

+0

我無法理解很多消息,最終失敗 – Ronix3

+0

這是日誌mStopped = false mHashWindowFocus = true sendUserActionEvent()mView = null並且輸入事件接收器已被處置 – Ronix3

回答

0

您應該添加的權限進入清單(和運行權限)

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
+0

我知道關於不是問題的權限 – Ronix3