7

I have a use case where I need to use Wifi network for my requests instead of the mobile network. The mobile device has already connected to the Wifi network. But Android seems to always resolve network requests using the mobile data network instead of the Wifi network. Note that the Wifi network does not have internet access, it is simply a wifi access point exposed by an external device during setup.

I tried using ConnectivityManager's setNetworkPreference(ConnectivityManager.TYPE_WIFI) but that did not work.

rajarshi
  • 131
  • 1
  • 2
  • 5
  • 2
    Possible duplicate of [Android: Send data only by WIFI](http://stackoverflow.com/questions/11742894/android-send-data-only-by-wifi) – Gennadii Saprykin Jun 21 '16 at 23:08
  • 2
    No it does not look like a duplicate. The answer to that question refers to checking the isConnected state which does not solve this problem where the wifi already connected, but not getting used. – rajarshi Jun 21 '16 at 23:40
  • So why it doesn't solve the problem then? isConnected will be `true` when the wifi is connected and `false` when it's not. – Gennadii Saprykin Jun 21 '16 at 23:42
  • 1
    Even when it is connected to Wifi, when we try to make network request from the webview, it resolves those network requests using mobile data instead of Wifi. Thats the problem. – rajarshi Jun 22 '16 at 00:06
  • Might need to see a bit of that code. – usajnf Jun 22 '16 at 00:15
  • You can block some specific urls from loading on 3G/4G by overriding `shouldOverrideUrlLoading` in your `WebViewClient`. You can check whether the connection is over wifi for a specific url. This is just a guess, haven't tried it on practice. – Gennadii Saprykin Jun 22 '16 at 00:21
  • 3
    Possible duplicate of [Android Lollipop defaults to Mobile Data when Wi-Fi has not Internet access?](http://stackoverflow.com/questions/27164298/android-lollipop-defaults-to-mobile-data-when-wi-fi-has-not-internet-access) – Daniel Nugent Jun 22 '16 at 02:14
  • This is how you do it (please upvote the answer if it solves your problem): https://stackoverflow.com/a/70977088/2267817 – Always Learning Jul 11 '22 at 17:13

1 Answers1

1

Disable mobile data while wifi setup is required. It won't resolve any requests with a carrier network if this is enabled.

Enable aeroplane mode, then toggle wifi

tutacat
  • 27
  • 4