1、Android系統代碼中實現設備名分配java
1. \frameworks\base\services\core\java\com\android\server\ConnectivityService.javaandroid
創建鏈接時讀取 "net.hostname" 屬性值是否爲空,若是爲空則獲取設備id,加上"android-"前綴並設置到屬性:ide
protected ConnectivityService(Context context, INetworkManagementService netManager, INetworkStatsService statsService, INetworkPolicyManager policyManager, IpConnectivityLog logger) { mHandlerThread = new HandlerThread("ConnectivityServiceThread"); mHandlerThread.start(); mHandler = new InternalHandler(mHandlerThread.getLooper()); mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); // setup our unique device name if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { String id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); if (id != null && id.length() > 0) { String name = new String("android-").concat(id); SystemProperties.set("net.hostname", name); } }
...... }
2. \frameworks\base\services\net\java\android\net\dhcp\DhcpInformPacket.javaoop
經過 addCommonClientTlvs() 接口獲取設備ID並填入buffer:優化
/**
* Adds additional parameters to the INFORM packet.
*/
void finishPacket(ByteBuffer buffer) {
addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_INFORM);
addTlv(buffer, DHCP_CLIENT_IDENTIFIER, getClientId());
addCommonClientTlvs(buffer);
addTlv(buffer, DHCP_PARAMETER_LIST, mRequestedParams);
addTlvEnd(buffer);
}
調用到 \frameworks\base\services\net\java\android\net\dhcp\DhcpPacket.java 中獲取 "net.hostname" 屬性值,ui
添加到Tlv信息中(TLV是一種編碼方式,由數據的類型Tag,數據的長度Length,數據的內容Value構成的一組數據報文) 編碼
protected void addCommonClientTlvs(ByteBuffer buf) { addTlv(buf, DHCP_MAX_MESSAGE_SIZE, (short) MAX_LENGTH); Option60 option60 = null; if (mIface != null) { option60 = OptionManager.getInstance().getOption60(mIface); } if (option60 != null && option60.getOptionSwitch() != 0 && option60.getLogin() != null && option60.getPassword() != null){ byte[] dhcpPlusBuf = buildDhcpPlusMsg(option60.getLogin(),option60.getPassword()); addTlv(buf,DHCP_VENDOR_CLASS_ID,dhcpPlusBuf); } else { addTlv(buf, DHCP_VENDOR_CLASS_ID, getVendorId()); } final String hn = getHostname(); if (!TextUtils.isEmpty(hn)) addTlv(buf, DHCP_HOST_NAME, hn); }
getHostname() 實現:
private String getHostname() { if (testOverrideHostname != null) return testOverrideHostname; return SystemProperties.get("net.hostname"); }
addTlv() 實現:spa
/** * Adds an optional parameter containing an array of bytes. */ protected static void addTlv(ByteBuffer buf, byte type, byte[] payload) { if (payload != null) { if (payload.length > MAX_OPTION_LEN) { throw new IllegalArgumentException("DHCP option too long: " + payload.length + " vs. " + MAX_OPTION_LEN); } buf.put(type); buf.put((byte) payload.length); buf.put(payload); } }
編譯及更新:code
mmm frameworks/base/services/ adb push out/target/product/almond/system/framework/services.jar /system/framework/ adb push out/target/product/almond/system/framework/oat/arm64/services.odex /system/framework/oat/arm64/ (優化編譯纔有此目錄)
2、經過build.prop文件配置設備名orm
1. 修改 /system/build.prop 最後添加,### 爲本身設置的設備名:
net.hostname=###
2. 重啓、重連wifi,從熱點或路由器後臺查看設備名即爲修改後的名稱。