80,351
社区成员
发帖
与我相关
我的任务
分享
InetAddress inetAddress = InetAddress.getLocalHost();
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
int iDhcp = dhcpInfo.gateway;
/**
杨铭 Created by kys_8 on 16/6/5,0005. <p>Email:771365380@qq.com</p> <p>Mobile phone:15133350726</p>
*/
public class OurIP
{
/** 得到本机所有IP地址 */
public void getLocalHostIP()
{
Enumeration<NetworkInterface> netInterfaces = null;
try
{
netInterfaces = NetworkInterface.getNetworkInterfaces();
}
catch (SocketException e)
{
e.printStackTrace();
}
while (netInterfaces.hasMoreElements())
{
NetworkInterface nif = netInterfaces.nextElement();
Enumeration<InetAddress> iparray = nif.getInetAddresses();
while (iparray.hasMoreElements())
{
InetAddress ip = iparray.nextElement();
System.out.println(ip.getHostAddress());
}
}
}
// 得到本机Mac地址
private String getLocalMac()
{
String mac = "";
// 获取wifi管理器
WifiManager wifiMng = (WifiManager) Global.connectActivity
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfor = wifiMng.getConnectionInfo();
mac = "本机的mac地址是:" + wifiInfor.getMacAddress();
return mac;
}
/** 下面的方法就是从这个方法改来的 */
private void getApIp_source()
{
StringBuilder IFCONFIG = new StringBuilder();
try
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements(); )
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements(); )
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress
.isLinkLocalAddress() && inetAddress.isSiteLocalAddress())
{
IFCONFIG.append(inetAddress.getHostAddress().toString() + "\n");
}
}
}
}
catch (SocketException ex)
{
}
System.out.println(IFCONFIG);
}
/** 获取自定义AP的IP */
public InetAddress getApIp()
{
try
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements(); )
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements(); )
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (inetAddress.isSiteLocalAddress() && inetAddress.getAddress()[0] < 0)
{//对于byte 如果想找到一个在0-255之间的大于byte.max 的那就要设置条件 byte < 0
return inetAddress;
}
}
}
}
catch (SocketException ex)
{
ex.printStackTrace();
}
_Log.e(_Log.msg() + "暂时没有得到AP地址··稍候");
return null;
}