HarmonyOS 如何获取ip地址和mac地址?

如题:HarmonyOS 如何获取ip地址和mac地址?

阅读 494
1 个回答

1、获取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\#wifimanagergetlinkedinfo9

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

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