Question

I need to check if the route to host exists for my Android app. Here is my code:

private void ensureRouteToHost(String proxyAddr) throws IOException
{
    ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

    int inetAddr;
    inetAddr = lookupHost(proxyAddr); // Return -938825536 for IP 192.168.10.200
    Log.d(TAG, "host for proxy is " + inetAddr);
    if (inetAddr == -1)
    {
        Log.e(TAG, "cannot establish route for " + proxyAddr + ": unkown host");
        throw new IOException("Cannot establish route for " + proxyAddr + ": Unknown host");
    }
    else
    {
        int[] apnTypes = new int[] {ConnectivityManager.TYPE_MOBILE, ConnectivityManager.TYPE_MOBILE_MMS, ConnectivityManager.TYPE_MOBILE_DUN, ConnectivityManager.TYPE_MOBILE_HIPRI, ConnectivityManager.TYPE_MOBILE_SUPL};
        for (int i=0; i<apnTypes.length; i++)
        {
            if (connMgr.requestRouteToHost(apnTypes[i], inetAddr))
            {
                Log.d(TAG, "route to host requested");
                return;
            }
        }

        Log.e(TAG, "unable to request route to host");
        throw new IOException("Cannot establish route to proxy " + inetAddr);
    }
}

public static int lookupHost(String hostname)
{
    hostname = hostname.substring(0, hostname.indexOf(":") > 0 ? hostname.indexOf(":") : hostname.length());
    String result = "";
    String[] array = hostname.split("\\.");
    if (array.length != 4) return -1;

    int[] hexArray = new int[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
    hexArray[0] = Integer.parseInt(array[0]) / 16;
    hexArray[1] = Integer.parseInt(array[0]) % 16;
    hexArray[2] = Integer.parseInt(array[1]) / 16;
    hexArray[3] = Integer.parseInt(array[1]) % 16;
    hexArray[4] = Integer.parseInt(array[2]) / 16;
    hexArray[5] = Integer.parseInt(array[2]) % 16;
    hexArray[6] = Integer.parseInt(array[3]) / 16;
    hexArray[7] = Integer.parseInt(array[3]) % 16;

    for (int i=0; i<8; i++)
    {
        result += Integer.toHexString( hexArray[i] );
    }

    return Long.valueOf(Long.parseLong(result, 16)).intValue();
}

It works perfectly on most devices but, and that's really weird, it doesn't work on Nexus S Europe. I've tried a couple of Nexus and I always got that problem. The problem is located in the ensureRouteToHost method when I call connMgr.requestRouteToHost(apnTypes[i], inetAddr). It always returns false, whatever I put in. My plan is to check if the IP 192.168.10.200 is reachable or not for my MMS App. This doesn't work neither for public IP such as stackoverflow.com (69.59.197.21 or 1161544981 as int).

So, do you have an idea why this isn't working on some devices ? Thanks for reading my thread.

Was it helpful?

Solution 2

Problem solved. It can't find the route to IP with the wifi on. 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.");
}

OTHER TIPS

You need to bring up those interfaces, e.g. HIPRI, first. The way to do that is described here.

However I find that whilst this makes both interfaces come up, and requestRouteToHost return true (at least, once the network is actually up), the routing still appears to have no effect.

This is as tested on a number of different phones.

Please let me know if you succeed with this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top