您的位置:首页 > 移动开发 > Android开发

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

2017-09-21 17:04 330 查看
修改的文件为:

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!");    

          }

        .......

    }

......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: