首当其冲的就是烧录MAC,SN
有些厂家在SDK中没有提供相应的接口,对于不熟悉Linux的小白来说就是一潭深井,在不断与阿里大神谦卑的请教下.总算知道了原理
下面的方法适合所有安卓系统
首先在串口或者adb shell之后可以执行
//烧录有线MAC
echo mac > /sys/class/unifykeys/name
echo "00:00:00:00:00:00" >/sys/class/unifykeys/write
//读取MAC
echo mac>/sys/class/unifykeys/name
cat /sys/class/unifykeys/read
//烧录SN
echo sn> /sys/class/unifykeys/name
echo "55555555555555555" >/sys/class/unifykeys/write
//读取SN
echo sn>/sys/class/unifykeys/name
cat /sys/class/unifykeys/read
在软件开发中,聪(tian)明(zhen)的我百度了Andorid执行Linux命令
然后在代码中执行了
//exec()方法为Runtime.getRuntime().exec(command)
MyUtil.exec("echo mac > /sys/class/unifykeys/name");
MyUtil.exec("echo "00:00:00:00:00:00" >/sys/class/unifykeys/write");
EXO ME
......
其实原理就是读写文件
public static String readFile(String filePath) {
try {
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();
} catch (IOException e) {
//e.printStackTrace();
Log.e(TAG, e.toString());
return null;
}
}
public static void writeFile(String path,String value){
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(path), 32);
out.write(value);
out.flush();
} catch (IOException e) {
Log.e(TAG, "IOException when write " + path);
} finally{
try{
if(out != null){
out.close();
out = null;
}
}catch(IOException e){
}
}
}
而烧录和读取MAC也就成了
public void setNetMac(String mac) {
MyUtil.writeFile("/sys/class/unifykeys/name", "mac");
MyUtil.writeFile("/sys/class/unifykeys/write", mac);
}
public String getNetSn() {
String string="";
MyUtil.writeFile("/sys/class/unifykeys/name", "sn");
string=MyUtil.readFile("/sys/class/unifykeys/read");
return string;
}
另外附带设置和获取系统Property属性
public static int setProperty(String paramString1, String paramString2)
{
try
{
Class localClass = Class.forName("android.os.SystemProperties");
localClass.getDeclaredMethod("set", new Class[] { String.class, String.class }).invoke(localClass, new Object[] { paramString1, paramString2 });
Log.e("设置telnet","Util-setProperty [" + paramString1 + "]=" + paramString2);
return 0;
}
catch (Exception localException)
{
Log.e("设置telnet","Util-setProperty--Exception=" + localException.getMessage());
}
return -1;
}
public static String getProperty(String key,String defValue) {
String value = "fail";
try {
Class<?> classProp = Class.forName("android.os.SystemProperties");
Method method = classProp.getDeclaredMethod("get", String.class);
value = (String) method.invoke(classProp, key);
} catch (Exception e) {
value = "fail";
}
return value;
}