Android 9.0 修改WLAN热点名称为MAC地址后四位

这个需求主要是读取mac地址,mac地址一般是用写号工具写入到NVRAM,所以需要从NVRAM读取准确的地址。

  • 导入nvram操作用到的库:

frameworks/opt/net/wifi/service/Android.mk

mk 复制代码
LOCAL_STATIC_JAVA_LIBRARIES := \
  vendor.mediatek.hardware.nvram-V1.0-java
  • 读取wifi mac地址并设置热点名称和密码:

frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiApConfigStore.java

java 复制代码
//mh.add
import vendor.mediatek.hardware.nvram.V1_0.INvram;
import android.os.RemoteException;
//mh.end

private WifiConfiguration getDefaultApConfiguration() {
    WifiConfiguration config = new WifiConfiguration();
    config.apBand = WifiConfiguration.AP_BAND_2GHZ;
   //mh.@{
    /*config.SSID = mContext.getResources().getString(
            R.string.wifi_tether_configure_ssid_default) + "_" + getRandomIntForDefaultSsid();*/
    String mMac = getMacAddress();
    mMac = mMac.replace(":", "");
    mMac = mMac.substring(mMac.length() - 4, mMac.length());
    config.SSID = "CloboticsAP_" + mMac;
    //mh.@}
    config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
    String randomUUID = UUID.randomUUID().toString();
    //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
    config.preSharedKey = "Cbs_123456";/*randomUUID.substring(0, 8) + randomUUID.substring(9, 13);*///mh.modify
    return config;
}

//mh.add
private static final String DEFAULT_MAC_ADDRESS = "01:02:03:04:05:06";
private static final String MAC_ADDRESS_FILENAME = "/mnt/vendor/nvdata/APCFG/APRDEB/WIFI";
private static final int MAC_ADDRESS_OFFSET = 4;
private static final int MAC_ADDRESS_DIGITS = 6;
//读取mac地址的方法
public String getMacAddress() {
    String result = DEFAULT_MAC_ADDRESS;
    StringBuffer nvramBuf = new StringBuffer();
    try {
        int i = 0;
        String buff = null;
        INvram agent = INvram.getService();
        if (agent != null) {
            buff = agent.readFileByName(MAC_ADDRESS_FILENAME, MAC_ADDRESS_OFFSET + MAC_ADDRESS_DIGITS);
            // Log.i(TAG, "Raw data:" + encryptMessage(buff));
            if (buff.length() >= 2 * (MAC_ADDRESS_OFFSET + MAC_ADDRESS_DIGITS)) {
                // Remove the \0 special character.
                int macLen = buff.length() - 1;
                for (i = MAC_ADDRESS_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));
                    }
                }
                result = nvramBuf.toString();
            } else {
                Log.e(TAG, "Fail to read mac address");
            }
        } else {
            Log.e(TAG, "Nvram is null");
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    if (result.length() > DEFAULT_MAC_ADDRESS.length()) {
        // remove extra characters if length longer than expected
        result = result.substring(0, DEFAULT_MAC_ADDRESS.length());
    } else if (result.length() < DEFAULT_MAC_ADDRESS.length()) {
        // set to default if length shorted than expected
        result = DEFAULT_MAC_ADDRESS;
    }
    //Log.d(TAG, "result: " + encryptMessage(result));
    return result;
}
//mh.add end
相关推荐
2501_915106322 小时前
iOS混淆工具实战 金融支付类 App 的安全防护与合规落地
android·ios·小程序·https·uni-app·iphone·webview
alexhilton3 小时前
运行时着色器实战:实现元球(Metaballs)动效
android·kotlin·android jetpack
從南走到北4 小时前
JAVA国际版东郊到家同城按摩服务美容美发私教到店服务系统源码支持Android+IOS+H5
android·java·开发语言·ios·微信·微信小程序·小程序
观熵6 小时前
Android 相机系统全景架构图解
android·数码相机·架构·camera·影像
Huntto6 小时前
在Android中使用libpng
android
雨白8 小时前
Android 自定义 View:彻底搞懂 Xfermode 与官方文档陷阱
android
_小马快跑_9 小时前
从VSync心跳到SurfaceFlinger合成:拆解 Choreographer与Display刷新流程
android
_小马快跑_9 小时前
Android | 视图渲染:从invalidate()到屏幕刷新的链路解析
android
Monkey-旭11 小时前
Android 定位技术全解析:从基础实现到精准优化
android·java·kotlin·地图·定位
树獭非懒13 小时前
Android 媒体篇|吃透 MediaSession 与 MediaController
android·架构