Android: 设置wifi设备名

本文介绍了如何通过修改prop属性设置WiFi连接时的设备名称。默认情况下,系统会自动生成一个包含android_前缀的唯一设备名。用户也可以通过注释掉自动生成部分并在system.prop中定义net.hostname来自定义设备名。

wifi连接AP后,在AP端看到的设备名保存在prop属性

net.hostname 中,可以通过getprop net.hostname查看

通过setprop net.hostname修改

默认系统会给一个名字

frameworks/base/services/java/com/android/server/ConnectivityService.java中生成 : // setup our unique device name 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); }

如果需要自己定义,可以注释掉这一段,然后在system.prop中定义 net.hostname=xxxxxx

需要注意的是hostname不能带空格,不然dhcp那边会有问题

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- 连接区域 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="WiFi ADB 连接" android:textSize="18sp" android:textStyle="bold" android:layout_marginBottom="8dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="8dp"> <EditText android:id="@+id/etIpAddress" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:hint="设备IP地址" android:inputType="number"/> <EditText android:id="@+id/etPort" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="端口" android:inputType="number" android:text="5555"/> </LinearLayout> <Button android:id="@+id/btnConnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="连接设备"/> <Button android:id="@+id/btnDisconnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="断开连接" android:visibility="gone"/> <TextView android:id="@+id/tvStatus" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="未连接设备" android:textStyle="italic" android:layout_marginVertical="8dp"/> <!-- 命令区域 --> <TextView android:id="@+id/tvCommand" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="AppOps 权限管理" android:textSize="18sp" android:textStyle="bold" android:layout_marginTop="16dp" android:layout_marginBottom="8dp"/> <LinearLayout android:id="@+id/groupCommand" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone"> <EditText android:id="@+id/etPackageName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="应用包" android:layout_marginBottom="8dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择权限操作" android:textStyle="bold"/> <Spinner android:id="@+id/spinnerCommonOps" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cbCustomOp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义权限操作" android:layout_gravity="center_vertical"/> </LinearLayout> <EditText android:id="@+id/etCustomOp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="例如: RUN_IN_BACKGROUND" android:layout_marginBottom="8dp" android:enabled="false"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置权限模式" android:textStyle="bold"/> <Spinner android:id="@+id/spinnerMode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp"/> <Button android:id="@+id/btnExecute" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="执行命令"/> </LinearLayout> <!-- 结果区域 --> <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:layout_marginTop="8dp"/> <ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminate="true" android:visibility="gone" android:layout_marginTop="16dp"/> </androidx.constraintlayout.widget.ConstraintLayout> 修复代码
最新发布
10-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值