编程方式使用WPA安全性的Andr​​oid平板电脑安装无线网络

问题描述:

我在Android平板电脑试图设置无线网络编程。如果没有WiFi连接,然后从一个文本文件中添加网络SSID和密钥读取。加入这个网络的网络列表,并保存密钥。它加入网络并保存密钥也,但是当我尝试连接,它不连接。让我知道...

I am trying setup WiFi programatically on an Android tablet. If there is no WiFi connection then add the network with SSID and passkey read from a text file. Adding this network to network list and saving the passkey. It's adding the network and saving the passkey also but when I try to connect, it's not connecting. let me know...

public static void setupWifi(Context _context)
{
     if(deviceConfig.wireless_ssid.length()==0) return;

     WifiManager wifi = (WifiManager)_context.getSystemService(_context.WIFI_SERVICE);
     WifiConfiguration wc = new WifiConfiguration(); 
     wc.SSID = "\"" + deviceConfig.wireless_ssid + "\""; //IMP! This should be in Quotes!!
     wc.hiddenSSID = true;
     wc.status = WifiConfiguration.Status.ENABLED;     
     wc.priority = 40;
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
     wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

     wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

     wc.preSharedKey = "\"" + deviceConfig.wireless_passkey + "\"";// 

     Log.d("ssid : ", wc.SSID );

     List<WifiConfiguration> netWorkList =  wifi.getConfiguredNetworks();
     WifiConfiguration wifiCong = null;

     if (netWorkList != null) {
         for(WifiConfiguration item:netWorkList) {
             if (item.SSID.equalsIgnoreCase("\"" + deviceConfig.wireless_ssid + "\"")) {
                 wifiCong = item;
             }
         }
     }

     if (wifiCong == null) {
         boolean res1 = wifi.setWifiEnabled(true);
         int res = wifi.addNetwork(wc);
         Log.d("WifiPreference", "add Network returned " + res );
         boolean b = wifi.enableNetwork(res, true);   
         Log.d("WifiPreference", "enableNetwork returned " + b );  
         boolean es = wifi.saveConfiguration();
         Log.d("WifiPreference", "saveConfiguration returned " + es );
     }
 }

谢谢 基兰

我有这个问题为好。我的方式解决它手动添加WiFi网络的设备。然后,我列举了配置和复制的设备添加的价值观成功地连接到该网络。

I had this problem as well. The way I solved it was manually adding the wifi network to the device. I then enumerated over the configuration and copied the values that the device added to successfully connect to that network.

要澄清:

手动添加网络,调用网络管理员来获得配置的网络,找到你的网络,然后调用干将出的一切你想设置的 - 你会发现你所需要的确切配置

Add the network by hand, call the network manager to get the configured networks, find your network, and then call the getters out of everything you are trying to set - you will find the exact configuration you need.

void getWifiConfigs()
{ 
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    List<WifiConfiguration> networks = wifi.getConfiguredNetworks();
    for (WifiConfiguration current : networks){
        //check getters here
    }
}