1

In my app I programmatically connect to an Access Point that has no access to the internet via WifiManager and WifiConfiguration. Later on I have to access a device connected to it to read data – but I didn't get that far.

After connecting to the Wifi I'm getting my DHCP setup just fine, but if I try to ping the Gateway address by typing:

$ adb shell
OnePlus6:/ $ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
^C
--- 192.168.1.1 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1015ms

The gateway is unreacheable...

I suspect this has to do with the fact that Android (9?) re-routes all traffic in case of a "dead" Wifi over to mobile data – for example you can still browse the internet just fine.

You can easily replicate the issue like this:

  1. Connect to an Access Point w/o internet access
  2. Check if your internet still works (e.g. go to google.com)
  3. Connect you phone via adb and ping the address of your AP

Any ideas on how to force the traffic trough Wifi, even if it's "offline"?

Edit: Output of ip route show:

OnePlus6:/ $ ip route show
10.16.52.64/29 dev rmnet_data0 proto kernel scope link src 10.16.52.67 
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.116 
6
  • Does your Android device have a data connection too, above the wifi ones? Commented Oct 11, 2018 at 13:20
  • Yes, mobile data is always on Commented Oct 11, 2018 at 13:22
  • can you please add the result of the following command? ip route show (run it in the adb shell) Commented Oct 11, 2018 at 13:37
  • Yes, see my edit please Commented Oct 11, 2018 at 13:46
  • it's strange, the routing table seems to be ok. Have you already tried to trace the hops of the ping packet? Try a traceroute to 192.168.1.1, so we can see how the traffic is directed Commented Oct 11, 2018 at 13:47

1 Answer 1

1

Ok, I've found a solution. One has to bind the current process to the correct network, which can be done like this:

connectionManager = (ConnectivityManager) 
context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connectionManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {
    public void onAvailable(Network network) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            connectionManager.bindProcessToNetwork(network);
        } else {
            ConnectivityManager.setProcessDefaultNetwork(network);
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.