MTK平台APP层 通过INvram获取SN号
生活随笔
收集整理的這篇文章主要介紹了
MTK平台APP层 通过INvram获取SN号
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?拷貝成 SNParamUtils.java 即可享用。
import com.blankj.utilcode.util.LogUtils;import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays;import vendor.mediatek.hardware.nvram.V1_0.INvram;public class SNParamUtils {public static final String PRODUCT_INFO_FILENAME = "/vendor/nvdata/APCFG/APRDEB/PRODUCT_INFO";private static final int SN_LENGTH =32;public static void writeSn(String sn) {char sn_chars[] = sn.toCharArray();byte sn_bytes[] = getBytes(sn_chars);try {INvram agent = INvram.getService();if (agent != null) {ArrayList<Byte> dataArray = new ArrayList<>();for (byte b : sn_bytes) {dataArray.add(new Byte(b));}int ret_1 = agent.writeFileByNamevec(PRODUCT_INFO_FILENAME, SN_LENGTH, dataArray);if (ret_1 == 0) {LogUtils.d("writeSn success" + ret_1);} else {LogUtils.d("writeSn failed" + ret_1);}} else {LogUtils.d( "writeSn: agent null");}} catch (Exception e) {LogUtils.d( "writeSn exception:" + e.getLocalizedMessage());e.printStackTrace();}}public static String readSn() {int targets = 0;try {String buff = null;INvram agent = INvram.getService();if (agent != null) {buff = agent.readFileByName(PRODUCT_INFO_FILENAME, SN_LENGTH);}byte[] buffArr = HexDump.hexStringToByteArray(buff.substring(0, buff.length() - 1));targets = (buffArr[0] & 0xff) | ((buffArr[1] << 8) & 0xff00) | ((buffArr[2] << 24) >>> 8) | (buffArr[3] << 24);LogUtils.d( "readSn: chars=>" + Arrays.toString(getChars(buffArr)) + ", targets == " + targets);LogUtils.d( "readSn: bytes=>" + Arrays.toString((buffArr)) + ", targets == " + targets);return String.valueOf(getChars(buffArr));} catch (Exception e) {LogUtils.d( "readSn exception:" + e.getLocalizedMessage());e.printStackTrace();}return "123456789";}private static char[] getChars(byte[] bytes) {Charset cs = Charset.forName("UTF-8");ByteBuffer bb = ByteBuffer.allocate(bytes.length);bb.put(bytes);bb.flip();CharBuffer cb = cs.decode(bb);return cb.array();}private static byte[] getBytes(char[] chars) {Charset cs = Charset.forName("UTF-8");CharBuffer cb = CharBuffer.allocate(chars.length);cb.put(chars);cb.flip();ByteBuffer bb = cs.encode(cb);return bb.array();} }?
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的MTK平台APP层 通过INvram获取SN号的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android App层 单独使用Sys
- 下一篇: Android APP层 ShellUt