2012-02-06 56 views
0

DHCP信息,我有這樣的代碼對Nexus One的Android上4.0.3工作:廣東話檢索在Android 2.2

WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE); 
     DhcpInfo info = wifi.getDhcpInfo(); 
     String s_gateway = String.valueOf(info.gateway); 
     String gatewayString = intToIp(Integer.parseInt(s_gateway)); 
     Log.d("DHCP Info: ", s_gateway+""); 
     Log.d("DHCP Info: ", gatewayString+""); 

這回我網關(PC + Connectify的)正確。

但Android 2.2上的相同代碼返回0.0.0.0。哪裏不對?

回答

1

我固定它的方式:

private String intToIp(int i) { 
    return (i & 0xFF) + "." + 
     ((i >> 8) & 0xFF) + "." + 
     ((i >> 16) & 0xFF) + ".1";// + 
     //((i >> 24) & 0xFF); 
} 

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    int ipAddress = wifiInfo.getIpAddress(); 
    gatewayString = intToIp(ipAddress);