Android应用中的自动Wifi连接

时间:2014-04-20 10:53:59

标签: android wifi android-wifi

我正在尝试一个Android应用程序,它可以在Button Click上自动连接到WiFi设备:

这是我的Wifi设备

 SSID: HI_LINK-DA79
 Encrypt Type: WPA/WPA2 TKIP

在我的应用程序Wifi显示之前:

HI_LINK-DA79
Secured with WPA/WPA2
Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener() {

        public void onClick(View v) {

            /*********************************************/
                        int test=0;
        String networkSSID = "HI-LINK_DA79";
        String networkPass = "12345678";
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";   //ssid must be in quotes
        conf.wepKeys[0] = "\""+ networkPass +"\"";
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
        conf.preSharedKey = "\""+ networkPass +"\"";


        WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); 
         int  r1 = wifiManager.addNetwork(conf);
         Log.d("Client", "add Network returned " + r1 );


        boolean b = wifiManager.enableNetwork(r1, true);        
        Log.d("Client", "enableNetwork returned " + b );

        boolean d= wifiManager.reconnect();
        Log.d("Client", "wifiManager.reconnect() returned " + d );

        WifiInfo info = wifiManager.getConnectionInfo();
        Log.i("Client", "ipaddr: " +info.getIpAddress());

        Log.i("Client", "SSID: " +info.getSSID());
        Log.i("Client", "MAC: " +info.getMacAddress());
        Log.i("Client", "NetId: " +info.getNetworkId());

}

这里的Logcat消息看起来没问题但它没有连接,运行后我可以看到

04-20 18:36:13.790: D/Client(24203): add Network returned 2
04-20 18:36:13.810: D/Client(24203): enableNetwork returned true
04-20 18:36:13.810: D/Client(24203): wifiManager.reconnect() returned true
04-20 18:36:13.810: I/Client(24203): ipaddr: 0
04-20 18:36:13.810: I/Client(24203): SSID: HI-LINK_DA79
04-20 18:36:13.810: I/Client(24203): MAC: 8c:77:16:8c:a7:36
04-20 18:36:13.810: I/Client(24203): NetId: -1

我预计在没有错误之后应该连接它,但Wifi显示"已保存"

  

HI_LINK-DA71保存,使用WPA / WPA2保护

为什么它没有连接?在配置中我还有什么需要做的吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

删除以下内容,因为您要连接到WPA安全网络&不是WEP安全网络:

conf.wepKeys[0] = "\""+ networkPass +"\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 

答案 1 :(得分:0)

HI_LINK-DA79
Secured with WPA/WPA2
Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener() {

        public void onClick(View v) {

            /*********************************************/
                        int test=0;
        String networkSSID = "HI-LINK_DA79";
        String networkPass = "12345678";
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";   //ssid must be in quotes
        conf.wepKeys[0] = "\""+ networkPass +"\"";
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
        conf.preSharedKey = "\""+ networkPass +"\"";


        WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); 
         int  r1 = wifiManager.addNetwork(conf);
         Log.d("Client", "add Network returned " + r1 );


        boolean b = wifiManager.enableNetwork(r1, true);        
        Log.d("Client", "enableNetwork returned " + b );

        boolean d= wifiManager.reconnect();
        Log.d("Client", "wifiManager.reconnect() returned " + d );

        WifiInfo info = wifiManager.getConnectionInfo();
        Log.i("Client", "ipaddr: " +info.getIpAddress());

        Log.i("Client", "SSID: " +info.getSSID());
        Log.i("Client", "MAC: " +info.getMacAddress());
        Log.i("Client", "NetId: " +info.getNetworkId());

}