OpenHarmony开发者论坛
标题:
OpenHarmony如何修改设备名称
[打印本页]
作者:
onefan
时间:
昨天 17:11
标题:
OpenHarmony如何修改设备名称
[md]**需求场景:**有部分场景需要修改设备名称,用于设备名称展示和xts送测等场景。
**需求分析:**修改设备名称的入口在设置-关于设备-设备名称,同理在设置-蓝牙中开启蓝牙也会有设备名称入口。
1.Settings源码的DeviceName.ets中
[@State](
https://github.com/State
"@State") deviceName: string = AboutDeviceModel.getSystemName();
TextInput({ text: this.deviceName })
2.默认值:AboutDeviceModel.getSystemName() 从settingsData中获取,当settingsData中拿不到值时,则显示为DEVICE\_NAME = ‘OpenHarmony 2.0 Canary’;
<pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code class="lang-javascript"><span class="pln"> </span><span class="com">/**</span></code></p></li><li class="L1" data-node-id="20250121171101-sbt217c"><p><code class="lang-javascript"><span class="com"> * Get system name from SettingsData</span></code></p></li><li class="L2"><p><code class="lang-javascript"><span class="com"> */</span></code></p></li><li class="L3" data-node-id="20250121171101-eav7vi0"><p><code class="lang-javascript"><span class="pln"> </span><span class="lit">@Log</span></code></p></li><li class="L4"><p><code class="lang-javascript"><span class="pln"> </span><span class="lit">@CatchError</span><span class="pun">(</span><span class="typ">ConfigData</span><span class="pun">.</span><span class="pln">DEVICE_NAME</span><span class="pun">)</span></code></p></li><li class="L5" data-node-id="20250121171101-re30iqj"><p><code class="lang-javascript"><span class="pln"> getSystemName</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L6"><p><code class="lang-javascript"><span class="pln"> let context </span><span class="pun">=</span><span class="pln"> </span><span class="typ">GlobalContext</span><span class="pun">.</span><span class="pln">getContext</span><span class="pun">().</span><span class="pln">getObject</span><span class="pun">(</span><span class="typ">GlobalContext</span><span class="pun">.</span><span class="pln">globalKeySettingsAbilityContext</span><span class="pun">)</span><span class="pln"> as common</span><span class="pun">.</span><span class="typ">Context</span><span class="pun">;</span></code></p></li><li class="L7" data-node-id="20250121171101-9dzvsh7"><p><code class="lang-javascript"><span class="pln"> let deviceName </span><span class="pun">=</span><span class="pln"> settings</span><span class="pun">.</span><span class="pln">getValueSync</span><span class="pun">(</span><span class="pln">context</span><span class="pun">,</span><span class="pln"> </span><span class="typ">ConfigData</span><span class="pun">.</span><span class="pln">SETTINGSDATA_DEVICE_NAME</span><span class="pun">,</span><span class="pln"> </span><span class="typ">ConfigData</span><span class="pun">.</span><span class="pln">DEVICE_NAME</span><span class="pun">);</span></code></p></li><li class="L8"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">return</span><span class="pln"> deviceName</span><span class="pun">;</span></code></p></li><li class="L9" data-node-id="20250121171101-q76itj4"><p><code class="lang-javascript"><span class="pln"> </span><span class="pun">}</span></code></p></li></ol></pre>
SETTINGSDATA\_DEVICE\_NAME = settings.general.DEVICE\_NAME;
DEVICE\_NAME = ‘OpenHarmony 2.0 Canary’;
3.如果需要配置默认值则需要匹配到SettingsData中的settings.general.DEVICE\_NAME。
SettingsData中SettingsDBHelper.ets代码如下:
<pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code class="lang-javascript"><span class="pln"> let tableName</span><span class="pun">:</span><span class="pln"> string </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">getTableName</span><span class="pun">(</span><span class="typ">TableType</span><span class="pun">.</span><span class="pln">SETTINGS</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">DEFAULT_USER_ID</span><span class="pun">);</span></code></p></li><li class="L1" data-node-id="20250121171101-9ztex4k"><p><code class="lang-javascript"><span class="pln"> </span><span class="com">// 初始化设备名称</span></code></p></li><li class="L2"><p><code class="lang-javascript"><span class="pln"> let deviceName</span><span class="pun">:</span><span class="pln"> string </span><span class="pun">=</span><span class="pln"> deviceInfo</span><span class="pun">.</span><span class="pln">marketName</span><span class="pun">;</span></code></p></li><li class="L3" data-node-id="20250121171101-axn2qmw"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">deviceName</span><span class="pun">.</span><span class="pln">startsWith</span><span class="pun">(</span><span class="str">'"'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">&&</span><span class="pln"> deviceName</span><span class="pun">.</span><span class="pln">endsWith</span><span class="pun">(</span><span class="str">'"'</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L4"><p><code class="lang-javascript"><span class="pln"> deviceName </span><span class="pun">=</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">deviceName</span><span class="pun">);</span></code></p></li><li class="L5" data-node-id="20250121171101-3qj7gp8"><p><code class="lang-javascript"><span class="pln"> </span><span class="pun">}</span></code></p></li><li class="L6"><p><code class="lang-javascript"><span class="pln"> await </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">loadTableSettings</span><span class="pun">(</span><span class="pln">settings</span><span class="pun">.</span><span class="pln">general</span><span class="pun">.</span><span class="pln">DEVICE_NAME</span><span class="pun">,</span><span class="pln"> deviceName</span><span class="pun">,</span><span class="pln"> tableName</span><span class="pun">);</span></code></p></li></ol></pre>
4.实际读得是deviceInfo.marketName得默认值,其对应的属性为const.product.name。
./startup/init/interfaces/innerkits/syspara/parameter.c:137: return GetProperty(“const.product.name”, &marketName);
修改此属性为想要的设备名称即可。
./startup/init/services/etc/param/ohos.para:22:const.product.name=”OpenHarmony 3.2”
5.开发版中替换验证,可以在./etc/param/ohos.para中修改const.product.name。重启即可生效。
6.同理在设置的蓝牙目录这一块也是可以显示设备名称。当在配置中修改默认值或手动修改设备名称后,再搜索蓝牙即可方便快捷的找到对应的设备。
<pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code class="lang-javascript"><span class="pln"> registerBluetoothStateObserver</span><span class="pun">(</span><span class="pln">callback</span><span class="pun">:</span><span class="pln"> </span><span class="pun">(</span><span class="pln">data</span><span class="pun">:</span><span class="pln"> boolean</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="kwd">void</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L1" data-node-id="20250121171101-3bks602"><p><code class="lang-javascript"><span class="pln"> </span><span class="typ">LogUtil</span><span class="pun">.</span><span class="pln">debug</span><span class="pun">(`</span><span class="pln">$</span><span class="pun">{</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">TAG</span><span class="pun">}</span><span class="pln"> subscribeStateChange call</span><span class="pun">`)</span></code></p></li><li class="L2"><p><code class="lang-javascript"><span class="pln"> bluetooth</span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="str">'stateChange'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L3" data-node-id="20250121171101-p1a1nbv"><p><code class="lang-javascript"><span class="pln"> </span><span class="typ">LogUtil</span><span class="pun">.</span><span class="pln">debug</span><span class="pun">(`</span><span class="pln">$</span><span class="pun">{</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">TAG</span><span class="pun">}</span><span class="pln"> subscribeStateChange</span><span class="pun">-></span><span class="pln">stateChange data</span><span class="pun">:</span><span class="pln">$</span><span class="pun">{</span><span class="pln">data</span><span class="pun">}`);</span></code></p></li><li class="L4"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L5" data-node-id="20250121171101-4ea3khq"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">case</span><span class="pln"> bluetooth</span><span class="pun">.</span><span class="typ">BluetoothState</span><span class="pun">.</span><span class="pln">STATE_ON</span><span class="pun">:</span></code></p></li><li class="L6"><p><code class="lang-javascript"><span class="pln"> let deviceName</span><span class="pun">:</span><span class="pln"> string </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">getSystemName</span><span class="pun">()</span></code></p></li><li class="L7" data-node-id="20250121171101-yxwzkw9"><p><code class="lang-javascript"><span class="pln"> let curDeviceName</span><span class="pun">:</span><span class="pln"> string </span><span class="pun">=</span><span class="pln"> bluetooth</span><span class="pun">.</span><span class="pln">getLocalName</span><span class="pun">()</span></code></p></li><li class="L8"><p><code class="lang-javascript"><span class="pln"> </span><span class="typ">LogUtil</span><span class="pun">.</span><span class="pln">debug</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">TAG </span><span class="pun">+</span><span class="pln"> </span><span class="str">"subscribeStateChange get deviceName: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> deviceName </span><span class="pun">+</span><span class="pln"> </span><span class="str">" ; curDeviceName: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> curDeviceName</span><span class="pun">)</span></code></p></li><li class="L9" data-node-id="20250121171101-08ahppe"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">deviceName </span><span class="pun">!==</span><span class="pln"> curDeviceName</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L0"><p><code class="lang-javascript"><span class="pln"> </span><span class="typ">LogUtil</span><span class="pun">.</span><span class="pln">debug</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">TAG </span><span class="pun">+</span><span class="pln"> </span><span class="str">"subscribeStateChange deviceName != curDeviceName"</span><span class="pun">)</span></code></p></li><li class="L1" data-node-id="20250121171101-y5drlp5"><p><code class="lang-javascript"><span class="pln"> bluetooth</span><span class="pun">.</span><span class="pln">setLocalName</span><span class="pun">(</span><span class="pln">deviceName</span><span class="pun">)</span></code></p></li><li class="L2"><p><code class="lang-javascript"><span class="pln"> </span><span class="pun">}</span></code></p></li><li class="L3" data-node-id="20250121171101-9zct601"><p><code class="lang-javascript"><span class="pln"> bluetooth</span><span class="pun">.</span><span class="pln">setBluetoothScanMode</span><span class="pun">(</span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></p></li><li class="L4"><p><code class="lang-javascript"><span class="pln"> </span><span class="typ">LogUtil</span><span class="pun">.</span><span class="pln">debug</span><span class="pun">(`</span><span class="pln">$</span><span class="pun">{</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">TAG</span><span class="pun">}</span><span class="pln"> subscribeStateChange</span><span class="pun">-></span><span class="pln">stateChange </span><span class="kwd">return</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">`)</span></code></p></li><li class="L5" data-node-id="20250121171101-snhjgz9"><p><code class="lang-javascript"><span class="pln"> callback </span><span class="pun">&&</span><span class="pln"> callback</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">)</span></code></p></li><li class="L6"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">break</span><span class="pun">;</span></code></p></li><li class="L7" data-node-id="20250121171101-qlwf0jk"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">case</span><span class="pln"> bluetooth</span><span class="pun">.</span><span class="typ">BluetoothState</span><span class="pun">.</span><span class="pln">STATE_OFF</span><span class="pun">:</span></code></p></li><li class="L8"><p><code class="lang-javascript"><span class="pln"> </span><span class="typ">LogUtil</span><span class="pun">.</span><span class="pln">debug</span><span class="pun">(`</span><span class="pln">$</span><span class="pun">{</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">TAG</span><span class="pun">}</span><span class="pln"> subscribeStateChange</span><span class="pun">-></span><span class="pln">stateChange </span><span class="kwd">return</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">`)</span></code></p></li><li class="L9" data-node-id="20250121171101-l5j66pa"><p><code class="lang-javascript"><span class="pln"> callback </span><span class="pun">&&</span><span class="pln"> callback</span><span class="pun">(</span><span class="kwd">false</span><span class="pun">)</span></code></p></li><li class="L0"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">break</span><span class="pun">;</span></code></p></li><li class="L1" data-node-id="20250121171101-qf0sdeo"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">default</span><span class="pun">:</span></code></p></li><li class="L2"><p><code class="lang-javascript"><span class="pln"> </span><span class="kwd">break</span><span class="pun">;</span></code></p></li><li class="L3" data-node-id="20250121171101-ix6afd9"><p><code class="lang-javascript"><span class="pln"> </span><span class="pun">}</span></code></p></li><li class="L4"><p><code class="lang-javascript"><span class="pln"> </span><span class="pun">})</span></code></p></li><li class="L5" data-node-id="20250121171101-kvczoc4"><p><code class="lang-javascript"><span class="pln"> </span><span class="pun">}</span></code></p></li></ol></pre>
7.之前遇到过各种场景导致设备名称不生效的,比如const.product.name属性配置被覆盖,settingsData未安装未被发现等等,如有类似问题可以一一排查。
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5