Question

I'm trying an android app which can connect to a WiFi device automatically on Button Click:

Here my Wifi Device

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

Before my app Wifi Showing:

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

}

Here is Logcat messages looks okay But Its not connected, after running this I can see

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

I'm expecting after no errors it should be connected, But Wifi showing "Saved"

HI_LINK-DA71 Saved,secured with WPA/WPA2

why is it not CONNECTED ? Is there anything else I have to do in Configuration ?

Thanks in advance

Was it helpful?

Solution

Remove the following as you want to connect to WPA secured network & not WEP secured network:

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

OTHER TIPS

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top