/** * wifi状态下获取网关 */ public static String pingGateWayInWifi(Context context) { String gateWay = null; WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); if (wifiManager == null) { return "wifiManager not found"; } DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); if (dhcpInfo != null) { int tmp = dhcpInfo.gateway; gateWay = String.format("%d.%d.%d.%d", (tmp & 0xff), (tmp >> 8 & 0xff), (tmp >> 16 & 0xff), (tmp >> 24 & 0xff)); } return gateWay; }
@Test public void shouldReturnDhcpInfo() { DhcpInfo dhcpInfo = new DhcpInfo(); shadowOf(wifiManager).setDhcpInfo(dhcpInfo); assertThat(wifiManager.getDhcpInfo()).isSameAs(dhcpInfo); }
public Network(Context context, String iface) throws SocketException, UnknownHostException { mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); mInfo = mWifiManager.getDhcpInfo(); mWifiInfo = mWifiManager.getConnectionInfo(); mLocal = new IP4Address(mInfo.ipAddress); mGateway = new IP4Address(mInfo.gateway); mNetmask = getNetmask(); mBase = new IP4Address(mInfo.netmask & mInfo.gateway); mTetheredIfacesMethod = getTetheredIfacesMethod(mConnectivityManager); if (iface != null) { if (initNetworkInterface(iface)) return; } else { for (String ifname : getAvailableInterfaces()) { if (initNetworkInterface(ifname)) { return; } } } throw new NoRouteToHostException("Not connected to any network."); }
@Test public void reconnect_shouldEnableDhcp() { // GIVEN WifiConfiguration config = new WifiConfiguration(); config.SSID = "SSID"; int netId = wifiManager.addNetwork(config); wifiManager.enableNetwork(netId, false); // WHEN wifiManager.reconnect(); // THEN assertThat(wifiManager.getDhcpInfo()).isNotNull(); }
/** * 获取当前Wifi所分配的Ip地址 * @return */ // when connect the hotspot, is still returning "0.0.0.0". public String getCurrentIpAddress(){ String ipAddress = ""; int address= mWifiManager.getDhcpInfo().ipAddress; ipAddress = ((address & 0xFF) + "." + ((address >> 8) & 0xFF) + "." + ((address >> 16) & 0xFF) + "." + ((address >> 24) & 0xFF)); return ipAddress; }
/** * 设备连接Wifi之后, 设备获取Wifi热点的IP地址 * @return */ public String getIpAddressFromHotspot(){ // WifiAP ip address is hardcoded in Android. /* IP/netmask: 192.168.43.1/255.255.255.0 */ String ipAddress = "192.168.43.1"; DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo(); int address = dhcpInfo.gateway; ipAddress = ((address & 0xFF) + "." + ((address >> 8) & 0xFF) + "." + ((address >> 16) & 0xFF) + "." + ((address >> 24) & 0xFF)); return ipAddress; }
/** * 开启热点之后,获取自身热点的IP地址 * @return */ public String getHotspotLocalIpAddress(){ // WifiAP ip address is hardcoded in Android. /* IP/netmask: 192.168.43.1/255.255.255.0 */ String ipAddress = "192.168.43.1"; DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo(); int address = dhcpInfo.serverAddress; ipAddress = ((address & 0xFF) + "." + ((address >> 8) & 0xFF) + "." + ((address >> 16) & 0xFF) + "." + ((address >> 24) & 0xFF)); return ipAddress; }
static public InetAddress getBroadcastAddress(Context context) throws IOException { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); if (dhcp == null) { return null; } int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) { quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); } return InetAddress.getByAddress(quads); }
/** * 判断WIFI是否连接 * * @param context Context * @return true:连接, false:未连接 */ @SuppressWarnings("BooleanMethodIsAlwaysInverted") public static boolean isWifiConnected(Context context) { final WifiManager manager = (WifiManager) context.getApplicationContext() .getSystemService(Context.WIFI_SERVICE); if (manager == null) return false; final DhcpInfo info = manager.getDhcpInfo(); return info != null && info.ipAddress != 0; }
/** * 获取WIFI IP地址 * * @param context Context * @return IP地址 */ public static String getWifiIp(Context context) { final WifiManager manager = (WifiManager) context.getApplicationContext() .getSystemService(Context.WIFI_SERVICE); if (manager == null) return "0.0.0.0"; final DhcpInfo info = manager.getDhcpInfo(); if (info == null) return "0.0.0.0"; final int ip = info.ipAddress; return (0xFF & ip) + "." + (0xFF & ip >> 8) + "." + (0xFF & ip >> 16) + "." + (0xFF & ip >> 24); }
/** * 获取开启Ap热点后设备本身的IP地址 * * @param context 上下文 * @return IP地址 */ public static String getHotspotIpAddress(Context context) { WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE); DhcpInfo dhcpInfo = wifimanager == null ? null : wifimanager.getDhcpInfo(); if (dhcpInfo != null) { int address = dhcpInfo.serverAddress; return ((address & 0xFF) + "." + ((address >> 8) & 0xFF) + "." + ((address >> 16) & 0xFF) + "." + ((address >> 24) & 0xFF)); } return ""; }
/** * Provide a simple way to get subnet * * @param context Context in order to get WifiManager * @return subnet as a String */ public static String getSubnet(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); return getNetwork(wifiManager.getDhcpInfo()); }
/** * 获取网关信息 */ public static String getWifiGateWay(Context context) { String result = null; WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifi_service.getDhcpInfo(); result = long2Ip((long) dhcpInfo.gateway); return result; }
public String getServerIPAddress() { DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo(); return intToIp(mDhcpInfo.gateway); }
protected static boolean isWifiAvailable( WifiManager wifi ) { WifiInfo info = wifi.getConnectionInfo() ; if( info == null ) return false ; if( info.getNetworkId() == -1 ) return false ; if( info.getSupplicantState() != SupplicantState.COMPLETED ) return false ; // Here are the crucial additions: DhcpInfo dhcp = wifi.getDhcpInfo() ; if( dhcp == null ) return false ; if( dhcp.ipAddress != info.getIpAddress() ) return false ; return true ; }
WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo mWifiInfo = mWifiManager.getConnectionInfo(); String subnet = getSubnetAddress(mWifiManager.getDhcpInfo().gateway);
/** * 获取连接的Wifi热点的IP地址 * * @param context 上下文 * @return IP地址 */ public static String getHotspotIpAddress(Context context) { WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiinfo = wifiManager == null ? null : wifiManager.getConnectionInfo(); if (wifiinfo != null) { DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); if (dhcpInfo != null) { int address = dhcpInfo.gateway; return ((address & 0xFF) + "." + ((address >> 8) & 0xFF) + "." + ((address >> 16) & 0xFF) + "." + ((address >> 24) & 0xFF)); } } return ""; }
private String getGatewayIP() { if (!checkIsWifiOnAndConnected()) return "0.0.0.0"; WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); int ip = dhcp.gateway; return String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff) ); }
private static String intToIP(int ipAddress) { String ret = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); return ret; } public static String GetSubnetMask_WIFI() { WifiManager wifiManager = (WifiManager) Global.getMainActivity() .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); DhcpInfo dhcp = wifiManager.getDhcpInfo(); String mask = intToIP(dhcp.netmask); return mask; }
public String getWifiIP(int type) throws ConnectionException{ if(isWifiConnection()) { WifiManager wifi = (WifiManager) mCtx.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo();