您的位置:首页 > 移动开发 > Android开发

获取 android 设备 mac 地址的方法

2014-07-13 11:21 561 查看
1: By wifi manager. When wifi is not enabled after last power off, mac address by this method may be null. So need enable wifi to get it. This way is most reliable.

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.lang.InterruptedException;
...
public static String getMacAddress() {
final WifiManager wifiMan = (WifiManager) YourApplication().getSystemService(Context.WIFI_SERVICE);
WifiInfo wif = wifiMan.getConnectionInfo();
macadd = wif.getMacAddress();

if(macadd==null && !wifiMan.isWifiEnabled()) {
new Thread() {
@Override
public void run() {
wifiMan.setWifiEnabled(true);
for(int i=0;i<10;i++) {
WifiInfo _info = wifiMan.getConnectionInfo();
macadd = _info.getMacAddress();
if(macadd!=null)
break;
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
wifiMan.setWifiEnabled(false);
bMacSearchDone = true;
}
}.start();
}

while (macadd == null && !bMacSearchDone) {}
bMacSearchDone = false;
if ( macadd != null ) {
return macadd;
} else {
return null;
}
}

2. By IP. When connect web by 3G, can't get mac address by this way.

public static String getLocalMacAddressFromIp() {
String mac_s= null;
try {
String ip = getLocalIpAddress();
Log.i("Mac-IP",
"ip = " + ip);
if (ip != null) {
byte[] mac;
NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(ip));
if (ne ==
null) {
Log.i("Mac-IP",
"ne is null");
} else {
mac = ne.getHardwareAddress();
if (mac ==
null) {
Log.i("Mac-IP",
"mac is null");
} else {
Log.i("Mac-IP",
"mac = " + new String(mac));
mac_s = byte2hex(mac);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

return mac_s;
}
public static String getLocalIpAddress() {
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 instanceof Inet4Address
//InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())
) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
}
public static String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer(b.length);
String stmp = "";
int len = b.length;
for (int n =
0; n < len; n++) {
stmp = Integer.toHexString(b
& 0xFF);
if (stmp.length() ==
1)
hs = hs.append("0").append(stmp).append(":");
else {
hs = hs.append(stmp).append(":");
}
}
hs = hs.deleteCharAt(hs.length() - 1);
return String.valueOf(hs);
}

3. By file. Some device save mac info in the file "/sys/class/net/wlan0/address", but some saves in "/sys/class/net/eth0/address". So this method is not reliable.

/*

* Load file content to String
*/
public static String loadFileAsString(String filePath)
throws java.io.IOException{
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new
char[1024];
int numRead=0;
while((numRead=reader.read(buf)) !=
-1){
String readData = String.valueOf(buf,
0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}

/*
* Get the STB MacAddress
*/
public static String getMacAddressByFile() {
try {
return loadFileAsString("/sys/class/net/wlan0/address").substring(0,
17);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

Ref:
http://blog.csdn.net/u010152805/article/details/17535891 http://hunanliutian.blog.163.com/blog/static/950690762013101935054442/ http://xiaopao.iteye.com/blog/1851777
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: