这个需求主要是读取mac地址,mac地址一般是用写号工具写入到NVRAM,所以需要从NVRAM读取准确的地址。
- 导入nvram操作用到的库:
frameworks/opt/net/wifi/service/Android.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
//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
标签:WLAN,MAC,mh,length,result,ADDRESS,Android,mMac
From: https://blog.csdn.net/yesdehua/article/details/143229426