HarmonyOS鸿蒙Next是否有API能实现获取客户当前IP的功能

HarmonyOS鸿蒙Next是否有API能实现获取客户当前IP的功能 1、@ohos.deviceInfo (设备信息)中的serial是可作为设备唯一识别码,这个序号是永久不会变的吗?是否会因为卸载APP等操作发生变化?

2、请问是否有获取客户当前IP的API(公网IP)

3 回复

1、这个api需要获取系统权限,三方应用用不了,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-device-info-V13

为更好的保障用户隐私安全,建议根据使用场景,考虑使用AAID或OAID替代

2、可以使用网络的 getConnectionProperties 接口,注意需要权限 “ohos.permission.GET_NETWORK_INFO”

您可参考以下链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/net-connection-manager-V5#%E6%A0%B9%E6%8D%AE%E6%95%B0%E6%8D%AE%E7%BD%91%E7%BB%9C%E6%9F%A5%E8%AF%A2%E7%BD%91%E7%BB%9C%E7%9A%84%E8%83%BD%E5%8A%9B%E4%BF%A1%E6%81%AF%E5%8F%8A%E8%BF%9E%E6%8E%A5%E4%BF%A1%E6%81%AF

更多关于HarmonyOS鸿蒙Next是否有API能实现获取客户当前IP的功能的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,可以通过NetManager模块提供的API来获取设备的IP地址。具体来说,可以使用netManager.getNetCapabilities()方法获取网络能力信息,然后通过NetCapabilities对象中的linkAddresses属性获取设备的IP地址。以下是一个示例代码片段:

import netManager from '@ohos.net.manager';

let netCapabilities = netManager.getNetCapabilities();
let linkAddresses = netCapabilities.linkAddresses;

if (linkAddresses && linkAddresses.length > 0) {
    let ipAddress = linkAddresses[0].address;
    console.log("Current IP Address: " + ipAddress);
} else {
    console.log("No IP address available.");
}

该代码通过netManager.getNetCapabilities()获取当前网络的能力信息,然后从linkAddresses中提取IP地址。需要注意的是,linkAddresses可能包含多个地址,具体使用哪个地址取决于网络配置。

在HarmonyOS鸿蒙Next中,可以通过NetworkManager类获取设备的网络信息,包括IP地址。使用NetworkManager.getNetworkCapabilities()方法获取网络能力信息,再通过LinkProperties获取IP地址。以下是一个示例代码片段:

NetworkManager networkManager = context.getSystemService(Context.NETWORK_SERVICE);
NetworkCapabilities capabilities = networkManager.getNetworkCapabilities(network);
LinkProperties linkProperties = networkManager.getLinkProperties(network);
List<InetAddress> addresses = linkProperties.getLinkAddresses().stream()
    .map(LinkAddress::getAddress)
    .collect(Collectors.toList());

此代码可获取当前设备的IP地址。

回到顶部