android.net.wifi.WifiManager.getDhcpInfo()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(513)

本文整理了Java中android.net.wifi.WifiManager.getDhcpInfo()方法的一些代码示例,展示了WifiManager.getDhcpInfo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WifiManager.getDhcpInfo()方法的具体详情如下:
包路径:android.net.wifi.WifiManager
类名称:WifiManager
方法名:getDhcpInfo

WifiManager.getDhcpInfo介绍

暂无

代码示例

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

/**
 * 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;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnDhcpInfo() {
 DhcpInfo dhcpInfo = new DhcpInfo();
 shadowOf(wifiManager).setDhcpInfo(dhcpInfo);
 assertThat(wifiManager.getDhcpInfo()).isSameAs(dhcpInfo);
}

代码示例来源:origin: cSploit/android

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

代码示例来源:origin: robolectric/robolectric

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

代码示例来源:origin: mayubao/KuaiChuan

/**
   * 获取当前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;
  }

代码示例来源:origin: mayubao/KuaiChuan

/**
 * 设备连接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;
}

代码示例来源:origin: mayubao/KuaiChuan

/**
 * 开启热点之后,获取自身热点的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;
}

代码示例来源:origin: JonesChi/CastScreen

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

代码示例来源:origin: AlexMofer/ProjectX

/**
 * 判断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;
}

代码示例来源:origin: AlexMofer/ProjectX

/**
 * 获取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);
}

代码示例来源:origin: leavesC/WifiFileTransfer

/**
 * 获取开启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 "";
}

代码示例来源:origin: EthACKdotOrg/orWall

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

代码示例来源:origin: AdleyLong/RecyclerViewDemo

/**
 * 获取网关信息
 */
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;
}

代码示例来源:origin: itsMelo/UDPSocketDemo

public String getServerIPAddress() {
  DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
  return intToIp(mDhcpInfo.gateway);
}

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: stackoverflow.com

WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  WifiInfo  mWifiInfo = mWifiManager.getConnectionInfo();
String subnet = getSubnetAddress(mWifiManager.getDhcpInfo().gateway);

代码示例来源:origin: leavesC/WifiFileTransfer

/**
 * 获取连接的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 "";
}

代码示例来源:origin: voroshkov/Chorus-RF-Laptimer

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

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: eliotstocker/Light-Controller

public String getWifiIP(int type) throws ConnectionException{
  if(isWifiConnection()) {
    WifiManager wifi = (WifiManager) mCtx.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();

相关文章