1

I have a socket that connects to a TCP server using either Wifi or 4G signal that receives a PING every 5 minutes from the server. But if I turn Wifi off on the device, I would like the socket to detect this and reconnect using a 4g signal. I was wondering if there was a way to reconnect the socket automatically to 4G if I go out of the range of the wifi or if I turn wifi off?

Also is there a way to go the other way as well(From 4g to wifi if the wifi is available?

Bnaffy
  • 129
  • 3
  • 14

1 Answers1

1

Register an inclass BroadCastReceiver to listen to WIFI on or off :

  IntentFilter filter = new IntentFilter();
  filter.addAction("android.net.wifi.supplicant.CONNECTION_CHANGE");

  receiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
        boolean isConnected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
        if (isConnected ) {
               //Reconnect using Wifi.
        } else {
               //Reconnect using 4G.
        }
     }
  }
  registerReceiver(receiver, filter);

Dont forget to unregister the receiver:

@Override
protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(receiver);
}

Regarding the EXTRA_SUPPLICANT_CONNECTED extra, the documentation states:

The lookup key for a boolean that indicates whether a connection to the supplicant daemon has been gained or lost. true means a connection now exists. Retrieve it with getBooleanExtra(String, boolean).

Constant Value: "connected"

Storo
  • 988
  • 1
  • 7
  • 31
  • So will this work if Wifi is on but the device isn't connected to it? Like if I walk out of range of the wifi... – Bnaffy Dec 03 '14 at 21:34
  • According to the [documentation](http://developer.android.com/reference/android/net/wifi/WifiManager.html#EXTRA_SUPPLICANT_CONNECTED), it should. – Storo Dec 03 '14 at 21:39
  • @user3322270 Did you try it? – Storo Dec 03 '14 at 23:13
  • Would this be in the service or the activity? – Bnaffy Dec 04 '14 at 15:15
  • It depends on your implementation. How are you doing it? You should add this where you have control of the socket. Either way it should work. – Storo Dec 04 '14 at 17:20
  • I've tried tagging you but it won't let me...Well I'm following fairly close to this [link](http://stackoverflow.com/questions/14985678/how-to-keep-the-android-client-connected-to-the-server-even-on-activity-changes).. When you say connect to 4g or wifi, can those be connected differently? Because right now I just connect and if I have 4g it connects, same for wifi. But if I connect via Wifi, then turn Wifi off on my device I catch an error and what I would like is for the socket to auto reconnect via 4g.. – Bnaffy Dec 04 '14 at 17:24
  • @user3322270 You should do where you are controlling the socket. According to your aforementioned link, you should do it on the service, since there your are making the connection and manipulating the socket. You should disconnect and reconnect your socket according to the state of the wifi supplicant. – Storo Dec 04 '14 at 17:30
  • Ok Ill try that..if it works I wont forget to give you credit – Bnaffy Dec 04 '14 at 17:31
  • @user3322270 catch the error and use it to disconnect the socket. After that you should receive the CONNECTION_CHANGE intent, which you should use to check the state of the wifi and start it again. Also, if no error is thrown (first 3G second WIFI scenario), you may disconnect the socket when receiving the same intent. – Storo Dec 04 '14 at 17:36
  • @user3322270 One more thing. You can use the method [setNetworkPreference()](http://developer.android.com/reference/android/net/ConnectivityManager.html#setNetworkPreference%28int%29) to make the socket use only one network. Take a look at the following [question](http://stackoverflow.com/questions/2513713/how-to-use-3g-connection-in-android-application-instead-of-wi-fi). – Storo Dec 04 '14 at 17:39
  • I think it may be working, if I go out of range of the wifi it reconnects, however I get a message saying the activity crashed even though the process is still running.. – Bnaffy Dec 04 '14 at 21:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66230/discussion-between-storo-and-user3322270). – Storo Dec 04 '14 at 21:14
  • I'm getting isConnected as always false..When I turn wifi on and off its always false..any clue why? – Bnaffy Dec 05 '14 at 16:57
  • @user3322270 check the following [link](http://stackoverflow.com/questions/18947906/checking-if-wifi-is-connected-returning-false-even-though-it-is-connected) and [link](http://stackoverflow.com/questions/20973973/getting-connection-returns-false-even-when-the-device-has-connection) – Storo Dec 05 '14 at 17:50