android 采集mac,android获取mac地址

获取mac地址分为三段

1.Android 6.0 之前(不包括6.0)

2.Android 6.0(包括) - Android 7.0(不包括)

3.Android7.0(包括)之后

获取方法

/**

* Android  6.0 之前(不包括6.0)

* 必须的权限

* @param context

* @return

*/

private static StringgetMacDefault(Context context) {

String mac ="02:00:00:00:00:00";

if (context ==null) {

return mac;

}

int netMode = getNetMode(context);

if (netMode ==1){

Log.i(Constants.TAG, "有线 ");

return getLocalEthernetMacAddress();

}else if (netMode==2){

Log.i(Constants.TAG, "wifi ");

return getWifiMacAddr(context,mac);

}

return mac;

}

/**

* Android 6.0(包括) - Android 7.0(不包括)

* @return

*/

private static StringgetMacFromFile() {

String str ="";

String macSerial ="";

try {

Process pp = Runtime.getRuntime().exec(

"cat /sys/class/net/wlan0/address");

InputStreamReader ir =new InputStreamReader(pp.getInputStream());

LineNumberReader input =new LineNumberReader(ir);

for (; null != str; ) {

str = input.readLine();

if (str !=null) {

macSerial = str.trim();// 去空格

break;

}

}

}catch (Exception ex) {

ex.printStackTrace();

}

if (macSerial ==null ||"".equals(macSerial)) {

try {

return loadFileAsString("/sys/class/net/eth0/address")

.toUpperCase().substring(0, 17);

}catch (Exception e) {

e.printStackTrace();

}

}

return macSerial;

}

private static StringloadFileAsString(String fileName)throws Exception {

FileReader reader =new FileReader(fileName);

String text =loadReaderAsString(reader);

reader.close();

return text;

}

private static StringloadReaderAsString(Reader reader)throws Exception {

StringBuilder builder =new StringBuilder();

char[] buffer =new char[4096];

int readLength = reader.read(buffer);

while (readLength >=0) {

builder.append(buffer, 0, readLength);

readLength = reader.read(buffer);

}

return builder.toString();

}

/**7.0之后

* 遍历循环所有的网络接口,找到接口是 wlan0

* 必须的权限

* @return

*/

private static StringgetMacFromHardware() {

try {

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

for (NetworkInterface nif : all) {

if (!nif.getName().equalsIgnoreCase("wlan0"))continue;

byte[] macBytes = nif.getHardwareAddress();

if (macBytes ==null) {

return "";

}

StringBuilder res1 =new StringBuilder();

for (byte b : macBytes) {

res1.append(String.format("%02X:", b));

}

if (res1.length() >0) {

res1.deleteCharAt(res1.length() -1);

}

return res1.toString();

}

}catch (Exception e) {

e.printStackTrace();

}

return "02:00:00:00:00:00";

}

调用方法

/**

* 获取mac地址

*

* @return

*/

private static StringgetMacAddress(Context context) {

String mac ="02:00:00:00:00:00";

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {

mac =getMacDefault(context);

}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {

mac =getMacFromFile();

}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

mac =getMacFromHardware();

}

return mac;

}

判断有线和无线

private static int getNetMode(Context context) {

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = connectivityManager.getActiveNetworkInfo();

if (info == null) {

return 0;

}

boolean iscon = info.isAvailable();

if (!iscon) {

return 0;

}

if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {

//有线

return 1;

} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {

return 2;

} else {

return 0;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值