HarmonyOS 如何获取主机名、主机ip、mac地址等信息?

通过代码获取,不是使用命令获取,需要在应用内部获取到进行加密作为数据传参

阅读 658
1 个回答

1、设备名:

static test(context: Context) {
  settings.getValue(context, settings.general.DEVICE_NAME).then((value) => {
    console.log(`...test value -> ${JSON.stringify(value)}`)
  }).catch((err: BusinessError) => {
    console.error(`...test error code: ${err.code}, message: ${err.message}`);
  });
}

2、ip地址

权限:“ohos.permission.GET\_NETWORK\_INFO”

代码:

let netHandle = await connection.getDefaultNet();
if (netHandle) {
  let connectionProperties = await connection.getConnectionProperties(netHandle)
  if (connectionProperties && connectionProperties.linkAddresses) {
    connectionProperties.linkAddresses.forEach((address: connection.LinkAddress, index: number) => {
      console.info("get address info: " + JSON.stringify(address));
    })
  }
}

参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-net-connection-V5\#connectiongetconnectionproperties

3、mac地址

权限:“ohos.permission.GET\_WIFI\_INFO”

代码:

wifiManager.getLinkedInfo().then(data => {
  console.info("get wifi linked info: " + JSON.stringify(data));
}).catch((error:number) => {
  console.info("get linked info error");
});

参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-wifimanager-V5\#ZH-CN\_TOPIC\_0000001893370769\_\_wifimanagergetlinkedinfo9-1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进