Android:修改连接到AP端显示的设备名

一、Android系统代码中实现设备名分配

  1. \frameworks\base\services\core\java\com\android\server\ConnectivityService.java

    建立连接时读取 "net.hostname" 属性值是否为空,如果为空则获取设备id,加上"android-"前缀并设置到属性:

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.java

   通过 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" 属性值,

  添加到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() 实现:

    /**
     * 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);
        }
    }

  编译及更新:

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/   (优化编译才有此目录)

  

二、通过build.prop文件配置设备名

  1. 修改 /system/build.prop 最后添加,### 为自己设置的设备名:

net.hostname=###

  2. 重启、重连wifi,从热点或路由器后台查看设备名即为修改后的名称。

 

 Android:修改连接到AP端显示的设备名

 

Android:修改连接到AP端显示的设备名

上一篇:Django学习:创建第一个app


下一篇:命令查看当前运行APP的包名和Activity