[TOC]
修改记录
| 版本 | 修改内容 | 日期 |
|---|---|---|
| V1.0.0 | 初始版本 | 2022.3.5 |
设备名称
- 获取设备名称
public void getDeviceName(ContentResolver resolver) {
String deviceName = Settings.Global.getString(resolver, Settings.Global.DEVICE_NAME);
if (deviceName == null) {
deviceName = Build.MODEL;
}
return deviceName;
}
参考代码
文件路径:android11-r48/platform/packages/apps/settings/src/com/android/settings/deviceinfo/DeviceNamePreferenceController.java
函数名称:initializeDeviceName()
- 设置设备名称
public void setDeviceName(String deviceName) {
// 更新设备名称
setSettingsGlobalDeviceName(deviceName);
// 更新蓝牙设备名称
setBluetoothDeviceName(deviceName);
// 更新热点名称,设备作为WIFI热点时SSID名称
setTetherSsidName(deviceName);
}
private void setSettingsGlobalDeviceName(String deviceName) {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.DEVICE_NAME,
deviceName);
}
private void setBluetoothDeviceName(String deviceName) {
if (mBluetoothAdapter != null) {
mBluetoothAdapter.setName(getFilteredBluetoothString(deviceName));
}
}
/**
* Using a UTF8ByteLengthFilter, we can filter a string to be compliant with the Bluetooth spec.
* For more information, see {@link com.android.settings.bluetooth.BluetoothNameDialogFragment}.
*/
private static final String getFilteredBluetoothString(final String deviceName) {
CharSequence filteredSequence = new BluetoothLengthDeviceNameFilter().filter(deviceName, 0,
deviceName.length(),
new SpannedString(""),
0, 0);
// null -> use the original
if (filteredSequence == null) {
return deviceName;
}
return filteredSequence.toString();
}
private void setTetherSsidName(String deviceName) {
final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
// TODO: If tether is running, turn off the AP and restart it after setting config.
mWifiManager.setSoftApConfiguration(
new SoftApConfiguration.Builder(config).setSsid(deviceName).build());
}
参考代码
文件路径:android11-r48/platform/packages/apps/settings/src/com/android/settings/deviceinfo/DeviceNamePreferenceController.java
函数名称:setDeviceName
注意:此方法假定调用者已通过安全和有效性检查,已测试系统级应用可以正常调用。


