1

Androidアプリにホストへのルートが存在するかどうかを確認する必要があります。これが私のコードです:

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();
}

ほとんどのデバイスで完全に機能しますが、それは本当に奇妙なことです。NexusSEuropeでは機能しません。私はいくつかのNexusを試しましたが、いつもその問題が発生しました。問題はensureRouteToHost、を呼び出すときのメソッドにありますconnMgr.requestRouteToHost(apnTypes[i], inetAddr)。私が何を入れても、常にfalseを返します。私の計画は192.168.10.200、MMSアプリのIPが到達可能かどうかを確認することです。これは、stackoverflow.com(69.59.197.21またはintとして1161544981)などのパブリックIPでも機能しません。

それで、これが一部のデバイスで機能しない理由がわかりますか?私のスレッドを読んでくれてありがとう。

4

2 に答える 2

0

最初に、HIPRI などのこれらのインターフェイスを起動する必要があります。その方法については、こちらで説明しています

ただし、これにより両方のインターフェイスが起動し、requestRouteToHosttrue が返されますが (少なくとも、ネットワークが実際に起動すると)、ルーティングはまだ効果がないように見えます。

これは、さまざまな電話でテストされたとおりです。

これで成功したら教えてください。

于 2013-01-30T12:31:27.963 に答える
-1

問題が解決しました。wifiがオンの状態でIPへのルートを見つけることができません。最も簡単な方法は、wifi を無効にしてから、wifi を有効にすることです。

使用したコードは次のとおりです。

// 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.");
}
于 2013-01-24T14:18:05.103 に答える