我是靠谱客的博主 无心枫叶,最近开发中收集的这篇文章主要介绍android10通过nvram读写wifi蓝牙MAC前言一、怎么使用NVRAM二、使用方式总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

android10通过nvram读写wifi蓝牙MAC

  • 前言
  • 一、怎么使用NVRAM
  • 二、使用方式
    • 1.se修改
    • 2.上读写代码
  • 总结


前言

一般在出厂的时候就会写好MAC地址,不能更改,但是有一些特殊需求,或者测试的时候,需要改到这一块,这里我们还是用NvRam来在上层做一个读写


一、怎么使用NVRAM

参考这个在上层app应用使用Nvram
找到Nvram的jar包,implementation files(‘libsnvram.jar’)这样引用, 不然会报找不到类

二、使用方式

1.se修改

这个部分,有一些修改忘记了,可能没有diff上去, 如果碰到了权限问题,自己增加一下se就行

diff --git a/alps/device/mediatek/mt8168/sepolicy/basic/system_app.te b/alps/device/mediatek/mtxxxx/sepolicy/basic/system_app.te
@@ -8,3 +8,9 @@ allow system_app alarm_device:chr_file read;
 allow system_app alarm_device:chr_file open;
 allow system_app alarm_device:chr_file ioctl;
 allow system_app storage_stub_file:dir getattr;
+allow system_app system_data_file:file write;
+allow system_app system_data_file:file read;
+allow system_app system_data_file:file create;
+allow system_app system_data_file:dir write;
+allow system_app system_data_file:dir read;
+allow system_app system_data_file:dir add_name;

diff --git a/alps/system/sepolicy/prebuilts/api/29.0/public/app.te b/alps/system/sepolicy/prebuilts/api/29.0/public/app.te
@@ -461,7 +461,7 @@ 
-neverallow appdomain system_data_file:dir_file_class_set
+neverallow {appdomain -system_app} system_data_file:dir_file_class_set
     { create write setattr relabelfrom relabelto append unlink link rename };
 
 # Write to various other parts of /data.
diff --git a/alps/system/sepolicy/public/app.te b/alps/system/sepolicy/public/app.te
@@ -461,7 +461,7 @@ 
-neverallow appdomain system_data_file:dir_file_class_set
+neverallow {appdomain -system_app} system_data_file:dir_file_class_set
     { create write setattr relabelfrom relabelto append unlink link rename };


2.上读写代码

这里就直接贴一个Activity的代码了,要打包成系统app,其他没要求


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.internal.util.HexDump;


import java.util.ArrayList;
import java.util.StringTokenizer;

import vendor.mediatek.hardware.nvram.V1_0.INvram;

public class MacActivity extends Activity {

    final String MAC_WIFI_ADDRESS_FILENAME = "/mnt/vendor/nvdata/APCFG/APRDEB/WIFI";
    final String MAC_BT_ADDRESS_FILENAME = "/mnt/vendor/nvdata/APCFG/APRDEB/BT_Addr";
    final int MAC_WIFI_ADDRESS_OFFSET = 4;
    final int MAC_BT_ADDRESS_OFFSET = 0;
    final int MAC_ADDRESS_DIGITS = 6;

    private TextView tv_wifi_mac,tv_bluetooth_mac;
    private EditText et_wifi_mac,et_bluetooth_mac;
    private Button bt_get,bt_set_wifi_mac,bt_set_bluetooth_mac;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mac);

        //tv_wifi_mac.setText(getMacAddr(MAC_WIFI_ADDRESS_FILENAME,MAC_WIFI_ADDRESS_OFFSET,MAC_ADDRESS_DIGITS));
        //tv_wifi_mac.setText(getMacAddr(MAC_BT_ADDRESS_FILENAME,MAC_BT_ADDRESS_OFFSET,MAC_ADDRESS_DIGITS));
        tv_wifi_mac=findViewById(R.id.tv_wifi_mac);
        tv_bluetooth_mac=findViewById(R.id.tv_bluetooth_mac);
        et_wifi_mac=findViewById(R.id.et_wifi_mac);
        et_bluetooth_mac=findViewById(R.id.et_bluetooth_mac);
        bt_get=findViewById(R.id.bt_get);
        bt_set_wifi_mac=findViewById(R.id.bt_set_wifi_mac);
        bt_set_bluetooth_mac=findViewById(R.id.bt_set_bluetooth_mac);

        bt_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_wifi_mac.setText(getMacAddr(MAC_WIFI_ADDRESS_FILENAME,MAC_WIFI_ADDRESS_OFFSET,MAC_ADDRESS_DIGITS));
                tv_bluetooth_mac.setText(getMacAddr(MAC_BT_ADDRESS_FILENAME,MAC_BT_ADDRESS_OFFSET,MAC_ADDRESS_DIGITS));
            }
        });

        bt_set_wifi_mac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mac=et_wifi_mac.getText().toString();
                if(mac.length()!=12){
                    Toast.makeText(MacActivity.this,"mac错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                mac=mac.toUpperCase();
                String data=mac.substring(0,2)+":"+mac.substring(2,4)+":"+mac.substring(4,6)+":"+mac.substring(6,8)
                        +":"+mac.substring(8,10)+":"+mac.substring(10,12);
                Toast.makeText(MacActivity.this,data,Toast.LENGTH_SHORT).show();
                updateMacAddr(MAC_WIFI_ADDRESS_FILENAME,data,MAC_WIFI_ADDRESS_OFFSET,MAC_ADDRESS_DIGITS);

            }
        });

        //et_wifi_mac.setText("2A4AEFE223ee");

        bt_set_bluetooth_mac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mac=et_bluetooth_mac.getText().toString();
                if(mac.length()!=12){
                    Toast.makeText(MacActivity.this,"mac错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                mac=mac.toUpperCase();
                String data=mac.substring(0,2)+":"+mac.substring(2,4)+":"+mac.substring(4,6)+":"+mac.substring(6,8)
                        +":"+mac.substring(8,10)+":"+mac.substring(10,12);
                Toast.makeText(MacActivity.this,data,Toast.LENGTH_SHORT).show();
                updateMacAddr(MAC_BT_ADDRESS_FILENAME,data,MAC_BT_ADDRESS_OFFSET,MAC_ADDRESS_DIGITS);

            }
        });

        //et_bluetooth_mac.setText("3E:1D:46:B3:A7:62");
        //et_bluetooth_mac.setText("3E1D46B3a762");

    }

    public String getMacAddr(String file, int offset, int size) {
        Log.d(TAG, "getMacAddress(): start");

        StringBuffer nvramBuf = new StringBuffer();
        try {
            int i = 0;
            String buff = null;
            INvram agent = INvram.getService();
            if (agent == null) {
                Log.e(TAG, "NvRAMAgent is null");
            }
            try {
                buff = agent.readFileByName(
                        file, offset + size);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.i(TAG, "Raw data:" + buff);
            if (buff.length() < 2 * (offset + size)) {
            }
            // Remove the  special character.
            int macLen = buff.length() - 1;
            for (i = offset * 2; i < macLen; i += 2) {
                if ((i + 2) < macLen) {
                    nvramBuf.append(buff.substring(i, i + 2));
                    nvramBuf.append(":");
                } else {
                    nvramBuf.append(buff.substring(i));
                }
            }
            Log.d(TAG, "buff:" + nvramBuf.toString());

        } catch (RemoteException re) {
            re.printStackTrace();

        } catch (IndexOutOfBoundsException iobe) {
            iobe.printStackTrace();

        } finally {
            Log.d(TAG, "getMacAddress(): end");
        }

        return nvramBuf.toString();
    }

    private void updateMacAddr(String file, String mac, int offset, int size) {
        try {
            int i = 0;
            INvram agent = INvram.getService();
            byte[] macAddr = new byte[size];
            if (agent == null) {
                Log.e(TAG, "NvRAMAgent is null");
                return;
            }

            //parse mac address firstly
            StringTokenizer txtBuffer = new StringTokenizer(mac, ":");
            while (txtBuffer.hasMoreTokens()) {
                macAddr[i] = (byte) Integer.parseInt(txtBuffer.nextToken(), 16);
                i++;
            }
            if (i != size) {
                Log.e(TAG, "Wrong length of macAddr:" + i);
                Log.d(TAG, "The format of mac address is not correct");
                return;
            }

            String buff = null;
            try {
                buff = agent.readFileByName(
                        file, offset + size);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }

            // Remove  in the end
            byte[] buffArr = HexDump.hexStringToByteArray(
                    buff.substring(0, buff.length() - 1));

            for (i = 0; i < size; i ++) {
                Log.e(TAG, "i:" + i);
                Log.e(TAG, "size:" + size);
                if(file.equals(MAC_WIFI_ADDRESS_FILENAME))
                    buffArr[i + MAC_WIFI_ADDRESS_OFFSET] = macAddr[i];
                else if(file.equals(MAC_BT_ADDRESS_FILENAME))
                    buffArr[i + MAC_BT_ADDRESS_OFFSET] = macAddr[i];
                else {
                    Log.e(TAG, "Wrong file of name:" + file);
                    return;
                }

            }

            ArrayList<Byte> dataArray = new ArrayList<Byte>(
                    offset + size);

            for (i = 0; i < offset + size; i++) {
                dataArray.add(i, new Byte(buffArr[i]));
            }

            int flag = 0;
            try {
                flag = agent.writeFileByNamevec(file,
                        offset + size, dataArray);
            } catch (Exception e) {
                e.printStackTrace();
                Log.d(TAG, e.getMessage() + ":" + e.getCause());
                return;
            }
            Log.d(TAG, "Update successfully.rnPlease reboot this device");
        } catch (Exception e) {
            Log.d(TAG,e.getMessage() + ":" + e.getCause());
            e.printStackTrace();
        }
    }
}

总结

读写位置和偏移量要写正确,具体要去底层看一下,其实底层是有读写源码的,搜一搜关键字/mnt/vendor/nvdata/APCFG/APRDEB/WIFI这种的,具体看在哪里使用

最后

以上就是无心枫叶为你收集整理的android10通过nvram读写wifi蓝牙MAC前言一、怎么使用NVRAM二、使用方式总结的全部内容,希望文章能够帮你解决android10通过nvram读写wifi蓝牙MAC前言一、怎么使用NVRAM二、使用方式总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部