Android系统下获取本地IP和mac地址的方法 获取本地ip

Android系统下获取本地IP和mac地址的方法 获取本地ip

第一种方法:

(1)设备开通Wifi连接,获取到网卡的MAC地址和IP地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法)

//根据Wifi信息获取本地Mac

public static voidgetLocalMacAddressFromWifiInfo(Context context)

{

WifiManagerwifi =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);

WifiInfoinfo = wifi.getConnectionInfo();

StringmacAdress = info.getMacAddress(); //获取mac地址

intipAddress=info.getIpAddress();//获取ip地址

Stringip=intToIp(ipAddress);

}

publicStringintToIp(inti)

{

return((i>>24)&0xFF)+"."+((i>>16)&0xFF)+"."

+((i>>8)&0xFF)+"."+(i&0xFF);

}

这里需要加入permission

<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>

第二种方法:

调用android 的API:NetworkInterface. getHardwareAddress ()
首先是getLocalIpAddress,获取本地IP地址.

//获取本地IP

public static StringgetLocalIpAddress()

{

try {

for (Enumeration en =NetworkInterface

.getNetworkInterfaces(); en.hasMoreElements();)

{

NetworkInterface intf =en.nextElement();

for(Enumeration enumIpAddr = intf

.getInetAddresses();enumIpAddr.hasMoreElements();)

{

InetAddress inetAddress =enumIpAddr.nextElement();

if (!inetAddress.isLoopbackAddress()

&&!inetAddress.isLinkLocalAddress())

{

return inetAddress.getHostAddress().toString();

}

}

}

}catch (SocketException ex)

{

Log.e("WifiPreference IpAddress",ex.toString());

}

return null;

}

但如果没有红色部分代码。该方法在android2.3, 2.2...较老版本有效,在android较新版本

(例如4.0等)获取的数据不正确(我们这里增加了红色部分后,可以支持4.0以上版本)

然后,再根据IP地址获取到MAC地址:

public static StringgetLocalMacAddressFromIp(Context context)

{

String mac_s="";

try {

byte[]mac;

NetworkInterface ne =

NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));

mac = ne.getHardwareAddress();

mac_s =byte2hex(mac);

}catch (Exception e) {

e.printStackTrace();

}

return mac_s;

}

public staticString byte2hex(byte[]b)

{

StringBuffer hs =newStringBuffer(b.length);

String stmp ="";

int len =b.length;

for (int n = 0; n< len; n++)

{

stmp= Integer.toHexString(b[n] &0xFF);

if(stmp.length() == 1){

hs= hs.append("0").append(stmp);

}else {

hs= hs.append(stmp);

}

}

return String.valueOf(hs);

}

针对获取IP地址的方法,还有一种改进方法,这是是网上运用比较多的,测试表明,该方法获取到了类似fe80::b607:f9ff:fee5:487e..这样的IP地址。注意看这里的IP地址是IPV6的地址形式。所以,一种解决方法是,在上面代码中的最内层的for循环的if语句中对inetAddress进行格式判断,只有其是IPV4格式地址时,才返回值。修改后代码如下:

public String getLocalIpAddress() {

try {

String ipv4;

List nilist =Collections.list(NetworkInterface.getNetworkInterfaces());

for (NetworkInterface ni:nilist)

{

List ialist =Collections.list(ni.getInetAddresses());

for (InetAddress address: ialist)

{

if (!address.isLoopbackAddress()&&

InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))

{

return ipv4;

}

}

}

} catch (SocketException ex) {

Log.e(LOG_TAG, ex.toString());

}

return null;

}

  

爱华网本文地址 » http://www.413yy.cn/a/25101012/123371.html

更多阅读

低水平管理条件下高羊茅和早熟禾的表现比较 早熟禾草坪

为什么关注低水平养护条件下草坪的管理?大家当然都希望在合理的养护条件下发挥草种的最大优势特性,取得最良好的景观效果,但是这种理想的状况因为种种限制在实际应用当中并不能存在,因此探讨各个种类在不同情况下的具体表现,掌握不同条件

修改电脑IP地址的方法 电脑ip地址修改器

修改电脑IP地址的方法——工具/原料一台电脑,联网修改电脑IP地址的方法——步骤/方法修改电脑IP地址的方法 1、鼠标右键点“网上邻居”图标,选“属性”;修改电脑I

ubuntu下arp攻击防御和反击 ubuntu arp攻击

现在网络执法官、P2P终结者常常出现在局域网中胡搞瞎搞,使得网速变慢了,糟糕的话还断网。在windows中只要有“反p2p终结者”或arp防火墙“antiarp”,就可以轻松解决问题,在linux下呢,以下方法操作是在ubuntu下进行。用静态arp.这种方法

声明:《Android系统下获取本地IP和mac地址的方法 获取本地ip》为网友失恋公主分享!如侵犯到您的合法权益请联系我们删除