获取 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
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
相关文章推荐
- android 获取设备IP和Mac地址
- android 设备唯一码的获取,Cpu号,Mac地址
- Android开发中获取本机Mac地址和IP地址的方法
- android 设备唯一码的获取,Cpu号,Mac地址
- 关于android设备wifi-mac地址的获取
- Android中获取设备IP、MAC地址
- Android环境下稳妥获取mac地址的方法
- android 获取本机的IP地址和mac物理地址的实现方法
- android 设备唯一码的获取,Cpu号,Mac地址
- Android获取本机Mac地址及IP地址方法
- android 获取设备的IP和Mac地址
- android 设备唯一码的获取,Cpu号,Mac地址
- Android 获取本机Mac 地址方法及ip地址
- android 获取设备信息的IP地址和Mac地址—亲测无误!!
- Android手机获取Mac地址的方法
- android设备无线、有线网络下获取MAC地址
- android:获取本机Mac地址及IP地址方法
- 获取android 设备的cpu gpu信息 mac地址
- android 设备唯一码的获取,Cpu号,Mac地址
- Android下获取以太网mac地址方法 非wifi