我在Nexus One上的Android 4.0.3上运行此代码:
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。怎么了?
答案 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);