更新记录
1.0.1(2026-05-24)
调整鸿蒙发现逻辑。
1.0.0(2026-05-24)
初始化发布。
平台兼容性
uni-app(5.0)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | 5.0 | 1.0.0 | 12 | 1.0.0 | 5.0.0 | 1.0.0 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × | × |
uni-app x(5.0)
| Chrome | Safari | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 | 微信小程序 |
|---|---|---|---|---|---|---|---|---|
| × | × | 5.0 | 1.0.0 | 12 | 1.0.0 | 5.0.0 | 1.0.0 | × |
概述
XF-pingUTS封装了Android、iOS和Harmony系统的网络连通性探测能力,也提供获取本机当前网络IP、扫描当前网络 IPv4 网段的能力,支持当前网络 IPv4 网段扫描。
Android使用系统ping命令执行ICMP探测;iOS使用本地ICMP socket实现;Harmony使用@ohos-rs/ping执行ICMP探测,并在扫描时使用TCP常见端口探测作为补充。
Android实现参考了“通过系统ping命令判断目标IP是否可达”、枚举本机网卡地址、通过UDP触发ARP表更新后读取ARP缓存的常见实现思路。getCurrentIp只枚举本机当前网络地址,不请求公网IP服务。scanCurrentNetworkIps只扫描当前手机所在的IPv4局域网网段,不请求公网IP服务,扫描结果只返回IPv4地址。
使用前提
Android
- 需要网络访问权限:
android.permission.INTERNET。 - 插件已在
utssdk/app-android/config.json声明该权限,使用方仍需确认最终AndroidManifest中包含该权限。 - Android实现依赖系统
ping命令,不需要root权限。 - scanCurrentNetworkIps 使用 UDP 触发 ARP 表更新,并读取
/proc/net/arp解析当前网段内可见设备;随后对未发现的IPv4补充TCP常见端口探测;不会执行arp -a。
iOS
- 需要允许应用访问网络。
- 插件使用本地ICMP socket,不需要额外三方库。
- 如果目标网络屏蔽ICMP,结果会显示丢包或失败。
- scanCurrentNetworkIps 使用本地ICMP socket扫描当前IPv4网段,并对ICMP未回包的IPv4补充TCP常见端口探测;iOS不会返回局域网设备MAC地址。
Harmony
- 需要网络访问权限:
ohos.permission.INTERNET。 - 获取当前网络IP还需要网络状态权限:
ohos.permission.GET_NETWORK_INFO。 - 使用方需要在Harmony工程
module.json5中声明上述权限;插件已在utssdk/app-harmony/config.json中声明。 - Harmony依赖
@ohos-rs/ping,插件已在utssdk/app-harmony/config.json中配置@ohos-rs/ping: 0.0.2。 - scanCurrentNetworkIps 在Harmony上优先使用ICMP扫描当前IPv4网段,再使用TCP常见端口探测作为补充;普通Harmony应用可能无法读取ARP缓存表,因此不会返回设备MAC地址。TCP连接成功或连接被拒绝也会视为该IP在线;如果目标设备同时屏蔽ICMP和TCP探测,仍可能无法被发现。
插件接口
ping
对传入的域名或IP进行网络探测,并通过回调返回探测结果。
uni-app项目中(nvue)调用示例:
import { ping } from "@/uni_modules/XF-pingUTS"
ping({
host: "www.baidu.com",
count: 4,
timeout: 3000,
packetSize: 56,
complete: (res) => {
console.log(res)
}
});
uni-app x项目(uvue)中调用示例:
import { ping } from "@/uni_modules/XF-pingUTS";
import { PingOptions } from "@/uni_modules/XF-pingUTS/utssdk/interface.uts";
let options = {
host: "www.baidu.com",
count: 4,
timeout: 3000,
packetSize: 56,
complete: (res : any) => {
console.log(res)
}
} as PingOptions;
ping(options);
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| host | string | 是 | 域名或IP,不允许包含空格、换行、;、&、|。 |
| count | number | 否 | 探测次数,默认4,范围1到10。 |
| timeout | number | 否 | 单次超时时间,单位毫秒,默认3000。 |
| packetSize | number | 否 | ICMP payload大小,默认56;Harmony保留该字段但不参与TCP探测。 |
| success | function | 否 | 探测成功回调。 |
| fail | function | 否 | 探测失败回调。 |
| complete | function | 否 | 完成回调。 |
返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| host | string | 原始host。 |
| ip | string | 解析到的IP地址。 |
| success | boolean | 是否收到有效响应。 |
| transmitted | number | 发送次数。 |
| received | number | 成功响应次数。 |
| loss | number | 丢包率百分比。 |
| minTime | number | 最小耗时,单位毫秒。 |
| avgTime | number | 平均耗时,单位毫秒。 |
| maxTime | number | 最大耗时,单位毫秒。 |
| stddevTime | number | 耗时标准差,单位毫秒。 |
| rawOutput | string | 原始输出或插件生成的同形态输出。 |
| platform | string | android、ios或harmony。 |
| costTime | number | 本次调用总耗时,单位毫秒。 |
错误码
| 错误码 | 说明 |
|---|---|
| 9011001 | host为空或包含不安全字符。 |
| 9011002 | 命令执行失败、DNS失败或探测失败。 |
| 9011003 | 探测超时。 |
| 9011004 | 当前平台能力受限。 |
可用性
iOS、Android、Harmony系统
可提供的1.0.0及更高版本
getCurrentIp
获取当前设备本机网络IP,返回IPv4、IPv6、地址列表和当前命中的网卡名。该接口只读取本机网络接口,不请求公网IP服务。
uni-app项目中(nvue)调用示例:
import { getCurrentIp } from "@/uni_modules/XF-pingUTS"
getCurrentIp({
preferIPv6: false,
complete: (res) => {
console.log(res)
}
});
uni-app x项目(uvue)中调用示例:
import { getCurrentIp } from "@/uni_modules/XF-pingUTS";
import { GetCurrentIpOptions } from "@/uni_modules/XF-pingUTS/utssdk/interface.uts";
let options = {
preferIPv6: false,
complete: (res : any) => {
console.log(res)
}
} as GetCurrentIpOptions;
getCurrentIp(options);
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| preferIPv6 | boolean | 否 | 是否优先返回IPv6。默认false,优先IPv4。 |
| success | function | 否 | 获取成功回调。 |
| fail | function | 否 | 获取失败回调。 |
| complete | function | 否 | 完成回调。 |
返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| ip | string | 按优先级选出的当前IP。 |
| ipv4 | string | 首个IPv4地址。 |
| ipv6 | string | 首个IPv6地址。 |
| ipv4List | string[] | 当前网络枚举到的IPv4地址列表。 |
| ipv6List | string[] | 当前网络枚举到的IPv6地址列表。 |
| interfaceName | string | 命中的网卡名,例如Android的wlan0、iOS的en0、Harmony的接口名。 |
| platform | string | android、ios或harmony。 |
错误码
| 错误码 | 说明 |
|---|---|
| 9011005 | 未获取到当前网络IP,或平台网络状态接口调用失败。 |
可用性
iOS、Android、Harmony系统
可提供的1.0.0及更高版本
scanCurrentNetworkIps
扫描当前手机网络所在的IPv4网段,返回可探测到的IP设备列表。扫描结果只返回IPv4地址。Android 使用 UDP 触发 ARP 表更新并读取/proc/net/arp后补充TCP探测,iOS 使用 ICMP 探测后补充TCP探测,Harmony 使用@ohos-rs/ping执行ICMP探测后补充TCP探测。TCP连接成功或连接被拒绝也会视为该IP在线。
uni-app项目中(nvue)调用示例:
import { scanCurrentNetworkIps } from "@/uni_modules/XF-pingUTS"
scanCurrentNetworkIps({
timeout: 350,
startHost: 1,
endHost: 254,
complete: (res) => {
console.log(res)
}
});
uni-app x项目(uvue)中调用示例:
import { scanCurrentNetworkIps } from "@/uni_modules/XF-pingUTS";
import { ScanCurrentNetworkIpsOptions } from "@/uni_modules/XF-pingUTS/utssdk/interface.uts";
let options = {
timeout: 350,
startHost: 1,
endHost: 254,
complete: (res : any) => {
console.log(res)
}
} as ScanCurrentNetworkIpsOptions;
scanCurrentNetworkIps(options);
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| timeout | number | 否 | 单个探测或等待ARP表刷新的超时时间,单位毫秒,默认350,范围150到2000。 |
| startHost | number | 否 | 起始主机位,默认1,范围1到254。 |
| endHost | number | 否 | 结束主机位,默认254,范围1到254。 |
| success | function | 否 | 扫描成功回调。 |
| fail | function | 否 | 扫描失败回调。 |
| complete | function | 否 | 完成回调。 |
返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| hostIp | string | 当前手机命中的IPv4地址。 |
| subnetPrefix | string | 当前网段前缀,例如192.168.1.。 |
| scanned | number | 已扫描的主机数量。 |
| found | number | 发现的设备数量。 |
| devices | ScannedIpDevice[] | 设备列表,只包含IPv4地址。 |
| rawOutput | string | 扫描过程输出。 |
| platform | string | android、ios或harmony。 |
| costTime | number | 本次调用总耗时,单位毫秒。 |
ScannedIpDevice
| 字段 | 类型 | 说明 |
|---|---|---|
| ip | string | 设备IP。 |
| mac | string | 设备MAC地址。Android尽量从ARP表返回;iOS和Harmony为空字符串。 |
| reachable | boolean | 是否可探测。 |
错误码
| 错误码 | 说明 |
|---|---|
| 9011006 | 当前网络IP扫描失败,通常是未获取到当前IPv4或平台网络探测失败。 |
可用性
iOS、Android、Harmony系统
可提供的1.0.0及更高版本

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 265
赞赏 0
下载 12023054
赞赏 1917
赞赏
京公网安备:11010802035340号