Android Q及以上版本如何让路由器获取到主机名
先说答案
打开packages/modules/NetworkStack/res/values/config.xml,把config_dhcp_client_hostname配置成true
<!-- Configuration for including DHCP client hostname option -->
<bool name="config_dhcp_client_hostname">true</bool>
这种问题真的是知道的就很简单,不知道得查半天,赶紧分享给大家,以节省大家时间。
读取的设备名从哪里来
追code就可以找到packages\modules\NetworkStack\src\android\net\dhcp\DhcpClient.java
// Transliterate hostname read from system settings if RRO option is enabled.
final boolean sendHostname = deps.getSendHostnameOption(context);
mHostname = sendHostname ? new HostnameTransliterator().transliterate(
deps.getDeviceName(mContext)) : null;
mMetrics.setHostnameTransinfo(sendHostname, mHostname != null);
/**
* Get the device name from system settings.
*/
public String getDeviceName(final Context context) {
return Settings.Global.getString(context.getContentResolver(),
Settings.Global.DEVICE_NAME);
}
也就是设置里面用户可以修改的设备名
为什么修改net.hostname没有用
从网上查到的方法都说是设置net.hostname property即可,验证过没有效,当找对地方后,也就知道大致原因了:
packages\modules\NetworkStack\src\android\net\dhcp\DhcpPacket.java
public String getHostname() {
if (mHostName == null
&& !ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
return SystemProperties.get("net.hostname");
}
return mHostName;
}