最近,需要给设备手动烧写MAC地址。用户插入U盘到盒子,系统自动读取U盘里面的文件,获取到文件内容,即mac地址。再调用底层接口即可完成mac地址的烧写。我所需要做的就是检测U盘,读取文件,根据接口返回值来判断是否烧写成功。
读取文件,调用接口都很简单,唯独检测U盘这个问题烦恼了许久。原来android系统会自动检测,根据U盘的插入与拔出分别发出相对应的广播。我只需要注册一个广播接受者,即可收到系统发出的广播。
系统检测U盘有三种状态:
1. 检测到U盘:android.intent.action.MEDIA_CHECKING
2. 连接U盘:android.intent.action.MEDIA_MOUNTED
3. 断开连接:android.intent.action.MEDIA_UNMOUNTED
连接广播:
public class BurnMACAddressBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
File usbFile = new File("mnt/sda/sda1/kvUpdateMac/usbUpdateMac.txt");
if(usbFile.exists()){
Intent updateintent = new Intent(context,MACActivity.class);
updateintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(updateintent);
}
}
}
为了能在开机后的任何情况下都能够接收到广播,先到mainfest中注册广播:
<!-- 广播注册 -->
<receiver android:name="com.kv.setmac.broadcast.BurnMACAddressBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
之前没有加<data android:scheme="file" />这一句,接受者始终无法接收到系统发出的U盘检测广播。后来去找找原因,是因为系统广播室隐式传递,于是增加了一个过滤条件。通过这个条件即scheme可以找到我的广播接受者。这一句很重要。在处理广播时,先判断指定的文件是否存在,存在才跳转到一个页面进行mac地址烧写。
MACActivity:
/**
* 获取MAC地址
*
* @return
*/
@SuppressLint("NewApi")
public String getEthMac() {
String str1 = "";
byte[] arrayOfByte = null;
int i = 0;
try {
Iterator<NetworkInterface> localIterator = Collections.list(
NetworkInterface.getNetworkInterfaces()).iterator();
while (true) {
if (!localIterator.hasNext())
return null;
NetworkInterface localNetworkInterface = (NetworkInterface) localIterator
.next();
if (!localNetworkInterface.getDisplayName().equals("eth0"))
continue;
arrayOfByte = localNetworkInterface.getHardwareAddress();
if (arrayOfByte == null)
return null;
if (i < arrayOfByte.length)
break;
}
} catch (Exception e) {
e.printStackTrace();
}
StringBuffer localStringBuilder = null;
if (arrayOfByte != null) {
localStringBuilder = new StringBuffer();
for (int j = 0; j < arrayOfByte.length; j++) {
String str = Integer.toHexString(arrayOfByte[j] & 0xFF);
if (str.length() == 1) {
localStringBuilder.append("0");
}
localStringBuilder.append(str);
localStringBuilder.append(":");
}
str1 = localStringBuilder.deleteCharAt(
localStringBuilder.length() - 1).toString();
}
return str1.toUpperCase();
}
获取利用android标准API获取mac地址,注意加权限, <uses-permission android:name="android.permission.INTERNET" />
读取文件内容:
/**
* 读取文件内容
*/
private void readFileMac() {
File usbFile = new File("mnt/sda/sda1/kvUpdateMac/usbUpdateMac.txt");
if (!usbFile.exists()) {
return;
}
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(usbFile);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
String hhh = new String(buffer);
String macthMac = "[0-9A-Fa-f]{12}";
if (hhh.length() == 12 && hhh.matches(macthMac)) {
mac = hhh;
} else {
mac = "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
将获取到的mac字符串传到底层,通过一些列动态库完成mac烧写。