1

I am creating an application in which I am doing scan wifi, connect to selected wifi, and other wifi related tasks programmatically.Now I want to implement auto connect feature.
Requirement:If auto connect is ON, the info of wifi is cached and allow next time auto connects to network; If multiple networks are set to Auto Connect, then it should connect to recently connected one.Any advise or guidance would be greatly appreciated.

rbrayb
  • 46,440
  • 34
  • 114
  • 174
MostlyJava
  • 345
  • 3
  • 21
  • I suggest you look at this answer: https://stackoverflow.com/questions/27559837/how-to-trigger-broadcastreceiver-when-i-turn-on-off-mobile-cellular-datamobile hope it helps you :) – kätzchen Mar 06 '19 at 14:19

1 Answers1

2

Connecting app to wifi whenever wifi is enable. do steps for auto connect:

Register BroadcastReceiver().

 <receiver
            android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
   </receiver>

Add permission in Menifest.

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

Create MyReceiver :

public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                Log.d("MyReceiver", "MyReceiver invoked...");

                WifiManager wifiManager = (WifiManager) 
                context.getSystemService(context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ssid = wifiInfo.getSSID();

                boolean noConnectivity = intent.getBooleanExtra(
                        ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                if (!noConnectivity) {
                    Log.d("MyReceiver", "connected");
                    WifiConfiguration wifiConfig = new WifiConfiguration();
                    wifiConfig.SSID = String.format("\"%s\"", ssid );
                    wifiConfig.preSharedKey = String.format("\"%s\"", networkKey);
                    WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                    int netId = wifiManager.addNetwork(wifiConfig);
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(netId, true);
                    wifiManager.reconnect();
                }
                else{
                    Log.d("NetworkCheckReceiver", "disconnected");
                }
            }
        }
 }

So whenever app is on foreground and wifi is enable to connect automatically recent wifi.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • wifiConfig.SSID = String.format("\"%s\"", networkSSID); wifiConfig.preSharedKey = String.format("\"%s\"", networkKey); I am not getting this how it get SSID and Password. – MostlyJava Jan 02 '18 at 11:34
  • can I store somewhere that which wifi is configured for auto connect and then I take that SSID and password. I am confused here. – MostlyJava Jan 02 '18 at 11:40
  • This is not possible as this would be a major security risk. You may be able to if the phone is rooted but I do not know, I would imagine, and hope, even if you could get to where it is stored on the phone, it would be encrypted. – Hemant Parmar Jan 02 '18 at 11:46