0

I'm developing an Android application which needs to stay connected to a specific WiFi network (without internet connection) and make all Volley HTTP requests and socket communication via WiFi. The problem is when the device mobile network is enabled the requests go all to the mobile network.

How can I do this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
admqueiroga
  • 95
  • 1
  • 1
  • 6

1 Answers1

0
ConnectivityManager connManager = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}




note: you also need to add

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

should work.

seven_seas
  • 703
  • 4
  • 21
  • How does that force volley not to use the mobile network? – OneCricketeer Sep 04 '16 at 00:41
  • it doesn't but it checks wether youre connected via wifi when connected via wifi normally phones use wifi and turn off mobile network so tadaa the volley will only get triggerd if youre on wifi – seven_seas Sep 04 '16 at 01:06
  • If both connections have internet the phone will use WiFi, but in my case my WiFi network doesn't have internet, that's the problem. – admqueiroga Sep 04 '16 at 01:20
  • referring to this discussion http://stackoverflow.com/questions/5171770/how-to-force-the-http-connection-over-wi-fi-instead-of-cellular-in-android It's not possible to do force the connection BUT you could simply turn cellular off for the request to happen over Wifi then reanable it – seven_seas Sep 04 '16 at 01:42