android 6.0 wifi 随机mac地址存入mvram临时处理方案

修改的文件为:

frameworks\opt\net\wifi\service\java\com\android\server\wifi\WifiServiceImpl.java


XXX_path为存储nvranm的物理路径。


......

import java.util.Random;

......


    private boolean isMYWifiMacadressEmpty() {
        final String MAC_ADDRESS_FILENAME = "XXX_path";
        final String NVRAM_AGENT_SERVICE = "NvRAMAgent";
        final String EMPTY_MAC_ADDRESS_NAME = "00:00:00:00:00:00";
        boolean ret = false;

        try {
            String dataMacaddress = Settings.Global.getString(mContext.getContentResolver(), Settings.Global.MY_WIFI_MAC_ADDRESS);
            if (dataMacaddress == null) {
                WifiNvRamAgent agent = WifiNvRamAgent.Stub.asInterface(ServiceManager.getService(NVRAM_AGENT_SERVICE));
                if (agent != null) {
                    byte[] buff = agent.readFileByName(MAC_ADDRESS_FILENAME);
                    String macFromNVRam = "";

                    if (buff != null) {
                        if (buff.length > 10) {
                            macFromNVRam = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buff[4], buff[5], buff[6], buff[7], buff[8], buff[9]);
                            if (EMPTY_MAC_ADDRESS_NAME.equals(macFromNVRam)) {
                                ret = true;
                            } else {
                                Settings.Global.putString(mContext.getContentResolver(), Settings.Global.MY_WIFI_MAC_ADDRESS, macFromNVRam);
                                ret = false;
                            }
                        } else {
                            Log.d(TAG, "getMacAddress(): over! buff.length < 10 !");
                        }
                    } else {
                        Log.d(TAG, "getMacAddress(): over buff is null!");
                    }
                } else {
                    Log.d(TAG, "getMacAddress(): over agent is null!");
                }
            }
        } catch (RemoteException re) {
            ret = false;
            re.printStackTrace();
        } catch (IndexOutOfBoundsException iobe) {
            ret = false;
            iobe.printStackTrace();
        } catch (Exception e)  {
            ret = false;
            e.printStackTrace();
        } finally {
            Log.d(TAG, "getMacAddress(): over! ret =  " + ret);
        }
        }
        return ret;
    }


    private boolean setMYWifiMacadress() {
        final String MAC_ADDRESS_FILENAME = "XXX_path";
        final String NVRAM_AGENT_SERVICE = "NvRAMAgent";
        boolean ret = false;

        try {
            WifiNvRamAgent agent = WifiNvRamAgent.Stub.asInterface(ServiceManager.getService(NVRAM_AGENT_SERVICE));
            if (agent != null) {
                byte[] buff = agent.readFileByName(MAC_ADDRESS_FILENAME);
                String macFromNVRam = "";
                Random random = new Random();
                int result = -1, value = 0;

                if (buff != null) {
                    if (buff.length > 10) {
                        buff[4] = 0x00;
                        buff[5] = 0x08;
                        buff[6] = 0x22;
                        value = random.nextInt(253) + 1;
                        buff[7] = (byte)(value&0xFF);
                        value = random.nextInt(253) + 1;
                        buff[8] = (byte)(value&0xFF);
                        value = random.nextInt(253) + 1;
                        buff[9] = (byte)(value&0xFF);
                        macFromNVRam = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buff[4], buff[5], buff[6], buff[7], buff[8], buff[9]);
                        result = agent.writeFileByName(MAC_ADDRESS_FILENAME, buff);
                        Settings.Global.putString(mContext.getContentResolver(), Settings.Global.MY_WIFI_MAC_ADDRESS, macFromNVRam);
                        Log.d(TAG, "writeFileByName over result = " + result);
                        ret = true;
                    } else {
                        Log.d(TAG, "getMacAddress(): over! buff.length < 10 !");
                    }
                } else {
                    Log.d(TAG, "getMacAddress(): over buff is null!");
                }
            } else {
                Log.d(TAG, "getMacAddress(): over agent is null!");
            }
        } catch (RemoteException re) {
            ret = false;
            re.printStackTrace();
        } catch (IndexOutOfBoundsException iobe) {
            ret = false;
            iobe.printStackTrace();
        } catch (Exception e)  {
            ret = false;
            e.printStackTrace();
        } finally {
            Log.d(TAG, "getMacAddress(): over! ret =  " + ret);
        }
        return ret;
    }


 public synchronized boolean setWifiEnabled(boolean enable) {
        enforceChangePermission();

        int wifiStatus = getWifiEnabledState();

        if ((wifiStatus == WifiManager.WIFI_STATE_DISABLED)) {
            if (isMYWifiMacadressEmpty()) {
                if (setMYWifiMacadress()) {
                    Slog.d(TAG, "write mac address!");
                } else {
                    Slog.d(TAG, "not write mac address!");
                }
            } else {
                Slog.d(TAG, "not write mac address too!");    
          }

         ......

}

.........


    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
        enforceChangePermission();
        ConnectivityManager.enforceTetherChangePermission(mContext);

        int wifiApStatus = getWifiApEnabledState();

        if ((wifiApStatus == WifiManager.WIFI_AP_STATE_DISABLED)) {
            if (isMYWifiMacadressEmpty()) {
                if (setMYWifiMacadress()) {
                    Slog.d(TAG, "ap write mac address!");
                } else {
                    Slog.d(TAG, "ap not write mac address!");
                }
            } else {
                Slog.d(TAG, "ap not write mac address too!");    
          }

        .......

    }

......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值