Search code examples
androidandroid-wifiwifimanagerwifihotspot

Connecting to a Hotspot


I am doing an where i have to connect 2 phones via hotspot.

There will be 2 buttons (SEND and RECEIVE). If a person presses the RECEIVE button, their hotspot will be activated. If a person presses the SEND button, they should connect to that hotspot.

I already did a lot of research in here, and it helped me a lot to build the code, but it is still not working. The addNetwork method returns a valid id, the enableNetwork returns true, but it still doesn't connect to the hotspot network. It disables the other ones, but it doesn't connect to the hotspot.

The hotspot is well created since i can join it if i go through the settings, wifi, etc.

Here is my code :

Here are the listeners for the buttons:

      protected void setUpButtonListeners(){
       Log.i("SETUP BUTTON LISTENERS", "SETTING UP THE BUTTON LISTENERS");

    wificonfiguration=new WifiConfiguration();
    setUpTheWifiConfiguration(wificonfiguration);
    wifimanager = (WifiManager) HighwayActivity.this.getSystemService(HighwayActivity.this.WIFI_SERVICE);
    wifimanager.setWifiEnabled(true);

    Button sendButton= (Button) findViewById(R.id.send);
    Button receiveButton = (Button) findViewById(R.id.receive);
    sendButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0)
        {
                netID = wifimanager.addNetwork(wificonfiguration);
                Log.e("NET ID ::: ", "O net id is : "+ netID);

                boolean asad=wifimanager.disconnect();
                Log.e("wifi.manager disconnect"," bool is "+asad );
                boolean aux =  wifimanager.enableNetwork(netID, true);
                Log.e("enableNetwork", "bool aux : " + aux);
                boolean reconnect=wifimanager.reconnect();
                Log.e("Recconect ::: ", " recnect is : "+ reconnect);

            wifireceiver=new WifiReceiver();
            registerReceiver(wifireceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));

        }
    });

    receiveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0)
        {

            try
            {
                wifimanager.setWifiEnabled(false);
                //USING REFLECTION TO GET METHOD "SetWifiAPEnabled"
                Method method=wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                method.invoke(wifimanager, wificonfiguration, true);

                Toast.makeText(HighwayActivity.this, "Craeted a hotspot with the SSID : " + wificonfiguration.SSID, Toast.LENGTH_SHORT).show();

            }
            catch (NoSuchMethodException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IllegalArgumentException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}

Here is my wifi configuration: (i tried both of them)

    protected void setUpTheWifiConfiguration(WifiConfiguration wificonfiguration){
    wificonfiguration.SSID = "\"" + mySSID + "\"";

    wificonfiguration.preSharedKey = "\"" + "pwteste123A"+ "\"";
    /*
    wificonfiguration.status = WifiConfiguration.Status.ENABLED;
    wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
   // wificonfiguration.hiddenSSID = true;
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wificonfiguration.priority = 40;
    */
    // No security
    wificonfiguration.status = WifiConfiguration.Status.DISABLED;
    wificonfiguration.priority = 40;

              wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wificonfiguration.allowedAuthAlgorithms.clear();
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
}

Solution

  • i already solved the problem.

    1st of all i had to put 2 more quotes in the SSID on the client side. Then i had to remove all of the GroupCiphers off from the WifiConfiguration,change the status to enabled and i didn't need to put any priority .

                WifiConfiguration conf = new WifiConfiguration();
                conf.SSID = "\"" + "TinyBox" + "\"";
    
                conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    
                conf.preSharedKey="\"password\"";
                conf.hiddenSSID = true;
                conf.status = WifiConfiguration.Status.ENABLED;
    
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    
                int netid=wifi.addNetwork(conf);
    
                List<WifiConfiguration> list = wifi.getConfiguredNetworks();
                for( WifiConfiguration i : list ) {
    
                    if(i.SSID != null && i.SSID.equals("\"" + "TinyBox" + "\"")) {
                        Log.d("cONFIG nETOWKRS", "Found List of COnfigured Networks Tinybox");
                        try {
                            boolean b=wifi.disconnect();
    
                            boolean enab=wifi.enableNetwork(i.networkId, true);
    
                            wifi.reconnect();
                            break;
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
    
                    }
                }
    

    Thank you, I hope this helps other persons in the future