2013-01-24 1 views
1

내 Android 앱에 대한 호스트 경로가 존재하는지 확인해야합니다. 여기 내 코드는 다음과 같습니다.RequestRouteToHost가 작동하지 않습니다.

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

이것은 대부분의 기기에서 완벽하게 작동하지만 정말 이상합니다. Nexus S Europe에서 작동하지 않습니다. 나는 Nexus 몇 가지를 시험해 보았는데 항상 문제가 발생했습니다. connMgr.requestRouteToHost(apnTypes[i], inetAddr)을 호출하면이 문제가 ensureRouteToHost 메서드에 있습니다. 내가 입력 한 내용은 항상 false를 반환합니다. MMS 앱에 IP 192.168.10.200에 연결할 수 있는지 확인해야합니다. 이것은 공용 IP (stackoverflow.com 같은) (69.59.197.21 또는 1161544981 int로)는 작동하지 않습니다.

왜 일부 기기에서는 작동하지 않는 이유가 있습니까? 내 스레드를 읽어 주셔서 감사합니다.

+0

TYPE_MOBILE이있는 Galaxy Nexus GSM (CyanogenMod 10)에서 코드 작업 (모바일 데이터 연결이 설정된 경우에만 가능) – bwt

+0

의견에 감사드립니다. Xperia T 4.0.3, Desire S 4.0.4, Galaxy S3 4.1, Huawei Ascend P1 4.0.3 등에서도 작동합니다. 문제는 Nexus S 4.0u 버전에서 나타납니다. 데이터는 모든 기기에서 사용 설정됩니다. SIM 카드를 변경해도 문제가 해결되지 않습니다. – Manitoba

답변

-1

문제가 해결되었습니다. Wi-Fi가 켜져있는 IP 경로를 찾을 수 없습니다. 가장 간단한 방법은 Wi-Fi를 사용 중지하고 물건을 준비한 다음 Wi-Fi를 사용하도록 설정하는 것입니다. 당신은 이러한 인터페이스, 예를 제기 할 필요가

// 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."); 
} 
0

: 여기

은 내가 사용하는 코드입니다 HIPRI, 첫번째. 그렇게하는 방법은 described here입니다.

그러나 두 인터페이스가 모두 나타나고 requestRouteToHost이 true이면 (적어도 네트워크가 실제로 켜지면) 라우팅이 여전히 효과가없는 것으로 나타납니다.

이것은 다양한 전화에서 테스트되었습니다.

성공하면 알려 주시기 바랍니다.

관련 문제