Search code examples
androiddeviceandroid-version

requestRouteToHost issue under 4.0.3 and 4.0.4 but not other


For a personal project, I have to check if the device LAN ip for MMS is reachable. I've already found the IP with the provided APNs. Now I need to user the requestRouteToHost method to check if the proxy found is valid or not.

The code is perfectly working on devices running android 2.3 to 4.0 and from 4.1 to above. But it throws me an error under 4.0.3 and 4.0.4. I've tested it on multiple devices, that's not a device issue but a version one.

if (!connMgr.requestRouteToHost(2, inetAddr)) {
     Log.e(TAG, "unable to request route to host");
     throw new IOException("Cannot establish route to proxy " + inetAddr);
} else {
     Log.e(TAG, "route to host requested");
}

The first condition is always true, which is a real problem.

The above code is contained into an AsynTask that I made like that:

private class AsyncRoute extends AsyncTask<String, Void, Void>
{
    @Override
    protected Void doInBackground(String... proxyAddr)
    {
        try
        {
            ensureRouteToHost(proxyAddr[0]);
            Log.e(TAG, "OK ACK");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        mInetAck = true;
        return null;
    }
}

The InetAddress found is -938825536 which is equivalent to 192.168.010.200.

To start this AsyncTask, I use the following code:

new AsyncRoute().execute(MMSProxy);
while (!mInetAck);

Basically, it waits acknowledgement before doing a couple of requests.

So, Does someone know if there is a trick to bypass this issue ? I mean is there a way to do the same thing as connMgr.requestRouteToHost(2, inetAddr) that could work on every device ?

Thanks.

Ps: Tested on Desire S 2.3, Desire S 4.0.3, Xperia lt30p 4.0.4, Nexus S 4.0.3, etc..

EDIT: On devices that throw an exception, a ping command on the route IP works:

> adb shell ping 192.168.10.200
PING 192.168.10.200 (192.168.10.200) 56(84) bytes of data.
64 bytes from 192.168.10.200: icmp_seq=1 ttl=251 time=4023 ms
64 bytes from 192.168.10.200: icmp_seq=5 ttl=251 time=81.3 ms

But the route request failed :s


Solution

  • Problem solved. It can't find the route to IP while the wifi is enabled. Simplest way is to disable wifi, do your stuff and then enable wifi.

    Here is the code I used:

    // Disable wifi if it's active
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isWifiEnabled())
    {
          mWasWifiActive = true;
          wifiManager.setWifiEnabled(false);
          Log.e(TAG, "Wifi was enabled, now Off.");
    }
    
    // Do stuff here
    
    // Re-enable wifi if it was active before routing
    if (mWasWifiActive)
    {
           WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
           wifiManager.setWifiEnabled(true);
           Log.e(TAG, "Wifi is back online.");
    }