From 1e502678d2768379af4ffdf39e21bbb22dbc29ed Mon Sep 17 00:00:00 2001
From: chenwenhui133 <2984202073@qq.com>
Date: Thu, 26 Feb 2026 10:27:02 +0800
Subject: [PATCH 1/3] =?UTF-8?q?docs:=20[Issues:=20#IE6HIL]=20=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0react-native-ping=E4=B8=89=E6=96=B9=E5=BA=93=E7=9A=84?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
zh-cn/react-native-ping.md | 625 +++++++++++++++++++++++++++++++++++++
1 file changed, 625 insertions(+)
create mode 100644 zh-cn/react-native-ping.md
diff --git a/zh-cn/react-native-ping.md b/zh-cn/react-native-ping.md
new file mode 100644
index 00000000..ca03282f
--- /dev/null
+++ b/zh-cn/react-native-ping.md
@@ -0,0 +1,625 @@
+> 模板版本:v0.3.0
+
+
+
react-native-ping
+
+
+本项目基于 [react-native-ping@[v1.2.8](https://github.com/RoJoHub/react-native-ping)]开发。
+
+请到三方库的 Releases 发布地址查看配套的版本信息:([@react-native-ohos/react-native-ping Releases]())。对于未发布到 npm 的旧版本,请参考[安装指南](/zh-cn/tgz-usage.md)安装 tgz 包。
+
+
+| 三方库版本 | 发布信息 | 支持 RN 版本 |
+| ---------- | ------------------------------------------------------------------------------------------------------ | ------------ |
+| v1.3.1 | [@react-native-ohos/react-native-ping Releases]() | 0.72/0.77/0.82 |
+
+## 1. 安装与使用
+
+进入到工程目录并输入以下命令:
+
+#### npm
+
+```bash
+npm install @react-native-ohos/react-native-ping
+```
+
+#### yarn
+
+```bash
+yarn add @react-native-ohos/react-native-ping
+```
+
+
+
+
+
+
+下面的代码展示了这个库的基本使用场景:
+
+> [!WARNING] 使用时 import 的库名不变。
+
+### react-native-ping example
+
+```js
+/**
+ * react-native-ping 示例
+ * 展示 React Native 中的 ICMP Ping 功能
+ */
+
+import React, { useState, useEffect } from 'react';
+import {
+ View,
+ Text,
+ StyleSheet,
+ ScrollView,
+ TouchableOpacity,
+ ActivityIndicator,
+ Alert,
+} from 'react-native';
+import Ping from '@react-native-ohos/react-native-ping';
+
+const TestCase = ({
+ title,
+ description,
+ onPress,
+ isLoading,
+ result,
+}) => (
+
+ {title}
+ {description && {description}}
+
+ {isLoading ? (
+
+ ) : (
+ 开始测试
+ )}
+
+ {result !== null && {result}}
+
+);
+
+interface TrafficStats {
+ receivedNetworkSpeed: number;
+ sendNetworkSpeed: number;
+ receivedNetworkTotal: number;
+ sendNetworkTotal: number;
+}
+
+interface TestResults {
+ [key: string]: string | null;
+}
+
+interface LoadingTests {
+ [key: string]: boolean;
+}
+
+const ReactNativePingDemo = () => {
+ const [loadingTests, setLoadingTests] = useState({});
+ const [testResults, setTestResults] = useState({});
+ const [trafficStats, setTrafficStats] = useState(null);
+ const [isLoadingTraffic, setIsLoadingTraffic] = useState(false);
+
+ const runTest = async (testId: string, testFunction: () => Promise) => {
+ setLoadingTests(prev => ({ ...prev, [testId]: true }));
+ setTestResults(prev => ({ ...prev, [testId]: null }));
+
+ try {
+ const result = await testFunction();
+ setTestResults(prev => ({
+ ...prev,
+ [testId]: `测试结果: ${result}`,
+ }));
+ } catch (error) {
+ const errorMessage = error.code
+ ? `测试失败 (${error.code}): ${error.message}`
+ : `测试失败: ${error.message || '未知错误'}`;
+ setTestResults(prev => ({
+ ...prev,
+ [testId]: errorMessage,
+ }));
+ } finally {
+ setLoadingTests(prev => ({ ...prev, [testId]: false }));
+ }
+ };
+
+ const runAllTests = async () => {
+ const testIds = [
+ 'testGoogleDNS',
+ 'test114DNS',
+ 'testInvalidHost',
+ 'testNoHost'
+ ];
+
+ for (const testId of testIds) {
+ if (!loadingTests[testId]) {
+ await runTest(testId, () => runSingleTest(testId));
+ }
+ }
+
+ Alert.alert('测试完成', '所有测试已执行完毕');
+ };
+
+ const runSingleTest = async (testId) => {
+ switch (testId) {
+ case 'testGoogleDNS':
+ return testPing('8.8.8.8', 'Google DNS');
+ case 'test114DNS':
+ return testPing('114.114.114.114', '114 DNS', { timeout: 5000 });
+ case 'testInvalidHost':
+ return testPing('invalid-host.example.com', '无效主机', { timeout: 5000 });
+ case 'testNoHost':
+ return testPing('', '未设置 host', { timeout: 5000 });
+ default:
+ throw new Error('未知测试');
+ }
+ };
+
+ const testPing = async (ipAddress: string, name: string, options: { timeout?: number } = {}) => {
+ try {
+ const start = Date.now();
+ const rtt = await Ping.start(ipAddress, options);
+ console.log("chy rrt:", rtt)
+ return `${name} - RTT: ${rtt}ms`;
+ } catch (e) {
+ return JSON.stringify(e.message)
+ }
+ };
+
+ const loadTrafficStats = async () => {
+ setIsLoadingTraffic(true);
+ try {
+ const stats = await Ping.getTrafficStats();
+ console.log("chy stats:", stats)
+ setTrafficStats(stats);
+ } catch (error) {
+ Alert.alert('获取流量统计失败', error.message);
+ } finally {
+ setIsLoadingTraffic(false);
+ }
+ };
+
+ useEffect(() => {
+ loadTrafficStats();
+ }, []);
+
+ return (
+
+
+ react-native-ping 演示
+
+ 高性能 ICMP Ping 控制器
+
+
+
+
+ 流量统计
+ {isLoadingTraffic ? (
+
+ ) : trafficStats ? (
+
+
+ 接收速度: {trafficStats.receivedNetworkSpeed}
+
+
+ 发送速度: {trafficStats.sendNetworkSpeed}
+
+
+ 接收总量: {trafficStats.receivedNetworkTotal}
+
+
+ 发送总量: {trafficStats.sendNetworkTotal}
+
+
+ ) : null}
+
+ 刷新统计
+
+
+
+
+ Ping 测试
+ runTest('testGoogleDNS', () => runSingleTest('testGoogleDNS'))}
+ isLoading={loadingTests['testGoogleDNS']}
+ result={testResults['testGoogleDNS']}
+ />
+ runTest('test114DNS', () => runSingleTest('test114DNS'))}
+ isLoading={loadingTests['test114DNS']}
+ result={testResults['test114DNS']}
+ />
+ runTest('testInvalidHost', () => runSingleTest('testInvalidHost'))}
+ isLoading={loadingTests['testInvalidHost']}
+ result={testResults['testInvalidHost']}
+ />
+
+ runTest('testNoHost', () => runSingleTest('testNoHost'))}
+ isLoading={loadingTests['testNoHost']}
+ result={testResults['testNoHost']}
+ />
+
+
+
+ v) && styles.disabledButton]}
+ onPress={runAllTests}
+ disabled={Object.values(loadingTests).some(v => v)}
+ >
+ {Object.values(loadingTests).some(v => v) ? (
+
+ ) : (
+ 运行所有测试
+ )}
+
+
+
+ );
+};
+
+const styles = StyleSheet.create({
+ container: {
+ flex: 1,
+ backgroundColor: '#F5F5F5',
+ },
+ header: {
+ backgroundColor: '#007AFF',
+ paddingVertical: 24,
+ paddingHorizontal: 16,
+ alignItems: 'center',
+ },
+ title: {
+ fontSize: 24,
+ fontWeight: 'bold',
+ color: '#FFFFFF',
+ textAlign: 'center',
+ marginBottom: 8,
+ },
+ subtitle: {
+ fontSize: 14,
+ color: 'rgba(255, 255, 255, 0.8)',
+ textAlign: 'center',
+ },
+ sectionContainer: {
+ backgroundColor: '#FFFFFF',
+ marginVertical: 8,
+ paddingHorizontal: 16,
+ paddingVertical: 16,
+ },
+ sectionTitle: {
+ fontSize: 18,
+ fontWeight: 'bold',
+ marginBottom: 16,
+ color: '#333333',
+ },
+ testCaseContainer: {
+ backgroundColor: '#F8F8F8',
+ padding: 12,
+ borderRadius: 8,
+ marginBottom: 16,
+ },
+ testCaseTitle: {
+ fontSize: 16,
+ fontWeight: '600',
+ marginBottom: 8,
+ color: '#333333',
+ },
+ testCaseDescription: {
+ fontSize: 12,
+ color: '#666666',
+ marginBottom: 12,
+ lineHeight: 16,
+ },
+ testButton: {
+ backgroundColor: '#007AFF',
+ paddingVertical: 8,
+ paddingHorizontal: 16,
+ borderRadius: 6,
+ alignItems: 'center',
+ },
+ disabledButton: {
+ backgroundColor: '#CCCCCC',
+ },
+ testButtonText: {
+ color: '#FFFFFF',
+ fontSize: 14,
+ fontWeight: '500',
+ },
+ testResult: {
+ marginTop: 12,
+ fontSize: 14,
+ lineHeight: 20,
+ color: '#333333',
+ },
+ statsContainer: {
+ backgroundColor: '#F8F8F8',
+ padding: 12,
+ borderRadius: 8,
+ marginBottom: 12,
+ },
+ statItem: {
+ fontSize: 14,
+ color: '#333333',
+ marginBottom: 8,
+ },
+ statValue: {
+ fontWeight: '600',
+ color: '#007AFF',
+ },
+ refreshButton: {
+ backgroundColor: '#007AFF',
+ paddingVertical: 8,
+ paddingHorizontal: 16,
+ borderRadius: 6,
+ alignItems: 'center',
+ },
+ refreshButtonText: {
+ color: '#FFFFFF',
+ fontSize: 14,
+ fontWeight: '500',
+ },
+ runAllButton: {
+ backgroundColor: '#34C759',
+ paddingVertical: 12,
+ paddingHorizontal: 16,
+ borderRadius: 8,
+ alignItems: 'center',
+ justifyContent: 'center',
+ },
+ runAllButtonText: {
+ color: '#FFFFFF',
+ fontSize: 16,
+ fontWeight: '600',
+ },
+});
+
+export default ReactNativePingDemo;
+
+```
+
+## 2.Manual Link
+
+此步骤为手动配置原生依赖项的指导。
+
+首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
+
+### 1.在工程根目录的 `oh-package.json` 添加 overrides字段
+
+为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 `oh-package.json5` 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
+
+关于该字段的作用请阅读[官方说明](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-oh-package-json5-V5#zh-cn_topic_0000001792256137_overrides):
+
+```json
+{
+ "overrides": {
+ "@rnoh/react-native-openharmony" : "./react_native_openharmony"
+ }
+}
+```
+
+### 2. 引入原生端代码
+
+目前有两种方法:
+
+1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
+2. 直接链接源码。
+
+方法一:通过 har 包引入(推荐)
+
+> [!TIP] har 包位于三方库安装路径的 `harmony` 文件夹下。
+
+打开 `entry/oh-package.json5`,添加以下依赖
+
+```json
+"dependencies": {
+ "@rnoh/react-native-openharmony": "0.72.32",
+ "@react-native-ohos/react-native-ping": "file:../../node_modules/@react-native-ohos/react-native-ping/harmony/RNPing.har"
+ }
+```
+
+点击右上角的 `sync` 按钮
+
+或者在终端执行:
+
+```bash
+cd entry
+ohpm install
+```
+
+方法二:直接链接源码
+
+> [!TIP] 如需使用直接链接源码,请参考[直接链接源码说明](/zh-cn/link-source-code.md)
+
+### 3. 配置 CMakeLists 和引入 WatermelonDBPackage
+
+打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
+
+```diff
+project(rnapp)
+cmake_minimum_required(VERSION 3.4.1)
+set(CMAKE_SKIP_BUILD_RPATH TRUE)
+set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+set(OH_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
+set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules/@rnoh/react-native-openharmony/src/main/cpp")
+set(RNOH_GENERATED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/generated")
+set(LOG_VERBOSITY_LEVEL 1)
+set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
+set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
+set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
+
+set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
+add_compile_definitions(WITH_HITRACE_SYSTRACE)
+
+add_subdirectory("${RNOH_CPP_DIR}" ./rn)
+
+# RNOH_BEGIN: manual_package_linking_1
+# RNOH_END: manual_package_linking_1
+
+file(GLOB GENERATED_CPP_FILES "${CMAKE_CURRENT_SOURCE_DIR}/generated/*.cpp") # this line is needed by codegen v1
+
++ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-ping/src/main/cpp" ./ping)
+
+
+
+add_library(rnoh_app SHARED
+ ${GENERATED_CPP_FILES}
+ "./PackageProvider.cpp"
+ "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
+)
+target_link_libraries(rnoh_app PUBLIC rnoh)
+
+# RNOH_BEGIN: manual_package_linking_2
++ target_link_libraries(rnoh_app PUBLIC rnoh_ping)
+
+
+# RNOH_END: manual_package_linking_2
+```
+
+打开 `entry/src/main/cpp/PackageProvider.cpp`,添加:
+
+```diff
+#include "RNOH/PackageProvider.h"
+#include "generated/RNOHGeneratedPackage.h"
++ #include "RNCPingPackage.h"
+
+using namespace rnoh;
+
+std::vector> PackageProvider::getPackages(
+ Package::Context ctx) {
+ return {
+ std::make_shared(ctx), // generated by codegen v1
++ std::make_shared(ctx)
+ };
+}
+```
+
+### 4. 在 ArkTs 侧引入 react-native-ping 组件
+
+找到 `entry/src/main/ets/RNPackagesFactory.ets`,添加:
+
+```diff
+...
+import type {RNPackageContext, RNPackage} from '@rnoh/react-native-openharmony/ts';
++ import {RNReactNativePingPackage} from '@react-native-ohos/react-native-ping/ts';
+
+export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
+return [
++ new RNReactNativePingPackage(ctx),
+ ];
+ }
+...
+...
+```
+
+### 5.运行
+
+点击右上角的 `sync` 按钮
+
+或者在终端执行:
+
+```bash
+cd entry
+ohpm install
+```
+
+然后编译、运行即可。
+
+## 3.约束与限制
+
+### 兼容性
+
+本文档内容基于以下版本验证通过:
+ 此三方库仅支持API20及以上版本
+1. RNOH:0.72.59; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio: 6.0.1.260; ROM:6.0.0.38;
+2. RNOH:0.77.18; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio 6.0.0.858; ROM:6.0.0.112;
+2. RNOH:0.82.X; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio 6.0.0.858; ROM:6.0.0.112;
+
+### 权限要求
+
+#### 在 entry 目录下添加申请以上权限的原因
+
+打开 `entry/src/main/resources/base/element/string.json`,添加:
+
+```diff
+...
+{
+ "string": [
++ {
++ "name": "ohos.permission.INTERNET",
++ },
+ ]
+}
+```
+
+### 方法
+
+#### start(ipAddress: string, options: { timeout?: number } = {})
+
+| Name | Description | Type | default | platform | HarmonyOS Support |
+| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
+| ipAddress| ip地址 | `string` | `undefined` | All | YES |
+| options | 配置项 | `object` | `{}` | All | NO |
+
+#### getTrafficStats()
+
+| Name | Description | Type | default | platform | HarmonyOS Support |
+| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
+| receivedNetworkSpeed| 接收速度 | `number` | `0` | All | YES |
+| sendNetworkSpeed | 发送速度 | `number` | `0` | All | YES |
+| receivedNetworkTotal| 接收总量 | `number` | `0` | All | YES |
+| sendNetworkTotal | 发送总量 | `number` | `0` | All | YES |
+
+
+### 错误码
+
+| Name | Description | Type | default | platform | HarmonyOS Support |
+| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
+| PingUtil_Message_Timeout | 超时 | `string` | `0` | iOS,Android | NO |
+| PingUtil_Message_PreviousPingIsStillRunning | 正在运行 | `string` | `1` | / | NO |
+| PingUtil_Message_HostErrorNotSetHost | 未设置ip | `string` | `2` | iOS,Android | YES |
+| PingUtil_Message_HostErrorUnknown | host未知错误 | `string` | `3` | iOS,Android | YES |
+| PingUtil_Message_HostErrorHostNotFound | host错误、host找不到| `string` | `4` | Only iOS | NO |
+| PingUtil_Message_Unknown | 未知错误 | `string` | `5` | Only iOS | NO |
+
+## 5.遗留问题
+
+- [ ] start方法的options配置不生效
+- [ ] 错误码只支持PingUtil_Message_HostErrorNotSetHost和PingUtil_Message_HostErrorUnknown,其他不支持
+
+## 7.开源协议
+
+本项目基于 [MIT License (MIT)]() ,请自由地享受和参与开源。
--
Gitee
From 4f06c23dfc68edf7e7d454ec9596eb598a366bc4 Mon Sep 17 00:00:00 2001
From: chenwenhui133 <2984202073@qq.com>
Date: Fri, 27 Feb 2026 15:31:08 +0800
Subject: [PATCH 2/3] =?UTF-8?q?docs:=20[Issues:=20#IE6HIL]=20=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0react-native-ping=E4=B8=89=E6=96=B9=E5=BA=93=E7=9A=84?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
zh-cn/react-native-ping.md | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/zh-cn/react-native-ping.md b/zh-cn/react-native-ping.md
index ca03282f..5f9b7405 100644
--- a/zh-cn/react-native-ping.md
+++ b/zh-cn/react-native-ping.md
@@ -11,7 +11,9 @@
| 三方库版本 | 发布信息 | 支持 RN 版本 |
| ---------- | ------------------------------------------------------------------------------------------------------ | ------------ |
-| v1.3.1 | [@react-native-ohos/react-native-ping Releases]() | 0.72/0.77/0.82 |
+| v1.3.0 | [@react-native-ohos/react-native-ping Releases]() | 0.72 |
+| v1.4.0 | [@react-native-ohos/react-native-ping Releases]() | 0.77 |
+| v1.5.0 | [@react-native-ohos/react-native-ping Releases]() | 0.82 |
## 1. 安装与使用
@@ -412,7 +414,18 @@ export default ReactNativePingDemo;
## 2.Manual Link
-此步骤为手动配置原生依赖项的指导。
+| | 是否支持autolink | RN框架版本 |
+|--------------------------------------|-----------------|------------|
+| ~1.3.0 | Yes | 0.72 |
+| ~1.4.0 | No | 0.77 |
+| ~1.5.0 | No | 0.82 |
+
+使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
+
+如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
+
+
+ ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
@@ -544,6 +557,7 @@ return [
...
...
```
+
### 5.运行
--
Gitee
From b97b246562f5732c7dd7da8404f012eaa5c007a5 Mon Sep 17 00:00:00 2001
From: chenwenhui133 <2984202073@qq.com>
Date: Sat, 28 Feb 2026 19:03:44 +0800
Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0react-native-ping?=
=?UTF-8?q?=E4=B8=89=E6=96=B9=E5=BA=93=E7=9A=84=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
en/react-native-ping.md | 596 +++++++++++++++++++++++++++++++++++++
zh-cn/react-native-ping.md | 201 +++++--------
2 files changed, 675 insertions(+), 122 deletions(-)
create mode 100644 en/react-native-ping.md
diff --git a/en/react-native-ping.md b/en/react-native-ping.md
new file mode 100644
index 00000000..cc1e7de1
--- /dev/null
+++ b/en/react-native-ping.md
@@ -0,0 +1,596 @@
+> 模板版本:v0.4.0
+
+
+
react-native-ping
+
+
+本项目基于 [react-native-ping](https://github.com/RoJoHub/react-native-ping) 开发。
+
+该第三方库支持直接从 npm 下载,包名为:`@react-native-ohos/react-native-ping` 版本所属关系如下:
+| 三方库名称 | 三方库版本 | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | npm地址 |
+| ------------ | ------------ | ------------------------------ | ------------- | ------------- |------------------------ | ------------- | ------------- |
+| @react-native-ohos/react-native-ping | ~ 1.3.0 | [Github Releases](https://github.com/react-native-oh-library/react-native-ping/releases) | 0.72.* | 否 | API20+ | 1.2.8 | [Npm Address](https://www.npmjs.com/package/@react-native-ohos/react-native-ping) |
+
+| @react-native-ohos/react-native-ping | ~ 1.4.0 | [Github Releases](https://github.com/react-native-oh-library/react-native-ping/releases) | 0.77.* | 否 | API20+ | 1.2.8 | [Npm Address](https://www.npmjs.com/package/@react-native-ohos/react-native-ping) |
+
+| @react-native-ohos/react-native-ping | ~ 1.5.0 | [Github Releases](https://github.com/react-native-oh-library/react-native-ping/releases) | 0.82.* | 否 | API20+ | 1.2.8 | [Npm Address](https://www.npmjs.com/package/@react-native-ohos/react-native-ping) |
+
+
+## 1. 安装与使用
+
+进入到工程目录并输入以下命令:
+
+
+
+#### **npm**
+
+```bash
+npm install @react-native-ohos/react-native-ping
+```
+
+#### **yarn**
+
+```bash
+yarn add @react-native-ohos/react-native-ping
+```
+
+
+
+下面的代码展示了这个库的基本使用场景:
+
+> [!WARNING] 使用时 import 的库名不变。
+
+```js
+/**
+ * react-native-ping 示例
+ * 展示 React Native 中的 ICMP Ping 功能
+ */
+
+import React, { useState, useEffect } from 'react';
+import {
+ View,
+ Text,
+ StyleSheet,
+ ScrollView,
+ TouchableOpacity,
+ ActivityIndicator,
+ Alert,
+} from 'react-native';
+import Ping from '@react-native-ohos/react-native-ping';
+
+const TestCase = ({
+ title,
+ description,
+ onPress,
+ isLoading,
+ result,
+}) => (
+
+ {title}
+ {description && {description}}
+
+ {isLoading ? (
+
+ ) : (
+ 开始测试
+ )}
+
+ {result !== null && {result}}
+
+);
+
+interface TrafficStats {
+ receivedNetworkSpeed: number;
+ sendNetworkSpeed: number;
+ receivedNetworkTotal: number;
+ sendNetworkTotal: number;
+}
+
+interface TestResults {
+ [key: string]: string | null;
+}
+
+interface LoadingTests {
+ [key: string]: boolean;
+}
+
+const ReactNativePingDemo = () => {
+ const [loadingTests, setLoadingTests] = useState({});
+ const [testResults, setTestResults] = useState({});
+ const [trafficStats, setTrafficStats] = useState(null);
+ const [isLoadingTraffic, setIsLoadingTraffic] = useState(false);
+
+ const runTest = async (testId: string, testFunction: () => Promise) => {
+ setLoadingTests(prev => ({ ...prev, [testId]: true }));
+ setTestResults(prev => ({ ...prev, [testId]: null }));
+
+ try {
+ const result = await testFunction();
+ setTestResults(prev => ({
+ ...prev,
+ [testId]: `测试结果: ${result}`,
+ }));
+ } catch (error) {
+ const errorMessage = error.code
+ ? `测试失败 (${error.code}): ${error.message}`
+ : `测试失败: ${error.message || '未知错误'}`;
+ setTestResults(prev => ({
+ ...prev,
+ [testId]: errorMessage,
+ }));
+ } finally {
+ setLoadingTests(prev => ({ ...prev, [testId]: false }));
+ }
+ };
+
+ const runAllTests = async () => {
+ const testIds = [
+ 'testGoogleDNS',
+ 'test114DNS',
+ 'testInvalidHost',
+ 'testNoHost'
+ ];
+
+ for (const testId of testIds) {
+ if (!loadingTests[testId]) {
+ await runTest(testId, () => runSingleTest(testId));
+ }
+ }
+
+ Alert.alert('测试完成', '所有测试已执行完毕');
+ };
+
+ const runSingleTest = async (testId) => {
+ switch (testId) {
+ case 'testGoogleDNS':
+ return testPing('8.8.8.8', 'Google DNS');
+ case 'test114DNS':
+ return testPing('114.114.114.114', '114 DNS', { timeout: 5000 });
+ case 'testInvalidHost':
+ return testPing('invalid-host.example.com', '无效主机', { timeout: 5000 });
+ case 'testNoHost':
+ return testPing('', '未设置 host', { timeout: 5000 });
+ default:
+ throw new Error('未知测试');
+ }
+ };
+
+ const testPing = async (ipAddress: string, name: string, options: { timeout?: number } = {}) => {
+ try {
+ const start = Date.now();
+ const rtt = await Ping.start(ipAddress, options);
+ console.log("chy rrt:", rtt)
+ return `${name} - RTT: ${rtt}ms`;
+ } catch (e) {
+ return JSON.stringify(e.message)
+ }
+ };
+
+ const loadTrafficStats = async () => {
+ setIsLoadingTraffic(true);
+ try {
+ const stats = await Ping.getTrafficStats();
+ console.log("chy stats:", stats)
+ setTrafficStats(stats);
+ } catch (error) {
+ Alert.alert('获取流量统计失败', error.message);
+ } finally {
+ setIsLoadingTraffic(false);
+ }
+ };
+
+ useEffect(() => {
+ loadTrafficStats();
+ }, []);
+
+ return (
+
+
+ react-native-ping 演示
+
+ 高性能 ICMP Ping 控制器
+
+
+
+
+ 流量统计
+ {isLoadingTraffic ? (
+
+ ) : trafficStats ? (
+
+
+ 接收速度: {trafficStats.receivedNetworkSpeed}
+
+
+ 发送速度: {trafficStats.sendNetworkSpeed}
+
+
+ 接收总量: {trafficStats.receivedNetworkTotal}
+
+
+ 发送总量: {trafficStats.sendNetworkTotal}
+
+
+ ) : null}
+
+ 刷新统计
+
+
+
+
+ Ping 测试
+ runTest('testGoogleDNS', () => runSingleTest('testGoogleDNS'))}
+ isLoading={loadingTests['testGoogleDNS']}
+ result={testResults['testGoogleDNS']}
+ />
+ runTest('test114DNS', () => runSingleTest('test114DNS'))}
+ isLoading={loadingTests['test114DNS']}
+ result={testResults['test114DNS']}
+ />
+ runTest('testInvalidHost', () => runSingleTest('testInvalidHost'))}
+ isLoading={loadingTests['testInvalidHost']}
+ result={testResults['testInvalidHost']}
+ />
+
+ runTest('testNoHost', () => runSingleTest('testNoHost'))}
+ isLoading={loadingTests['testNoHost']}
+ result={testResults['testNoHost']}
+ />
+
+
+
+ v) && styles.disabledButton]}
+ onPress={runAllTests}
+ disabled={Object.values(loadingTests).some(v => v)}
+ >
+ {Object.values(loadingTests).some(v => v) ? (
+
+ ) : (
+ 运行所有测试
+ )}
+
+
+
+ );
+};
+
+const styles = StyleSheet.create({
+ container: {
+ flex: 1,
+ backgroundColor: '#F5F5F5',
+ },
+ header: {
+ backgroundColor: '#007AFF',
+ paddingVertical: 24,
+ paddingHorizontal: 16,
+ alignItems: 'center',
+ },
+ title: {
+ fontSize: 24,
+ fontWeight: 'bold',
+ color: '#FFFFFF',
+ textAlign: 'center',
+ marginBottom: 8,
+ },
+ subtitle: {
+ fontSize: 14,
+ color: 'rgba(255, 255, 255, 0.8)',
+ textAlign: 'center',
+ },
+ sectionContainer: {
+ backgroundColor: '#FFFFFF',
+ marginVertical: 8,
+ paddingHorizontal: 16,
+ paddingVertical: 16,
+ },
+ sectionTitle: {
+ fontSize: 18,
+ fontWeight: 'bold',
+ marginBottom: 16,
+ color: '#333333',
+ },
+ testCaseContainer: {
+ backgroundColor: '#F8F8F8',
+ padding: 12,
+ borderRadius: 8,
+ marginBottom: 16,
+ },
+ testCaseTitle: {
+ fontSize: 16,
+ fontWeight: '600',
+ marginBottom: 8,
+ color: '#333333',
+ },
+ testCaseDescription: {
+ fontSize: 12,
+ color: '#666666',
+ marginBottom: 12,
+ lineHeight: 16,
+ },
+ testButton: {
+ backgroundColor: '#007AFF',
+ paddingVertical: 8,
+ paddingHorizontal: 16,
+ borderRadius: 6,
+ alignItems: 'center',
+ },
+ disabledButton: {
+ backgroundColor: '#CCCCCC',
+ },
+ testButtonText: {
+ color: '#FFFFFF',
+ fontSize: 14,
+ fontWeight: '500',
+ },
+ testResult: {
+ marginTop: 12,
+ fontSize: 14,
+ lineHeight: 20,
+ color: '#333333',
+ },
+ statsContainer: {
+ backgroundColor: '#F8F8F8',
+ padding: 12,
+ borderRadius: 8,
+ marginBottom: 12,
+ },
+ statItem: {
+ fontSize: 14,
+ color: '#333333',
+ marginBottom: 8,
+ },
+ statValue: {
+ fontWeight: '600',
+ color: '#007AFF',
+ },
+ refreshButton: {
+ backgroundColor: '#007AFF',
+ paddingVertical: 8,
+ paddingHorizontal: 16,
+ borderRadius: 6,
+ alignItems: 'center',
+ },
+ refreshButtonText: {
+ color: '#FFFFFF',
+ fontSize: 14,
+ fontWeight: '500',
+ },
+ runAllButton: {
+ backgroundColor: '#34C759',
+ paddingVertical: 12,
+ paddingHorizontal: 16,
+ borderRadius: 8,
+ alignItems: 'center',
+ justifyContent: 'center',
+ },
+ runAllButtonText: {
+ color: '#FFFFFF',
+ fontSize: 16,
+ fontWeight: '600',
+ },
+});
+
+export default ReactNativePingDemo;
+```
+
+## 2. Link
+
+| | 是否支持autolink | RN框架版本 |
+|--------------------------------------|------------------|-----------|
+| ~1.5.0 | No | 0.82 |
+| ~1.4.0 | No | 0.77 |
+| ~1.3.0 | Yes | 0.72 |
+
+使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
+
+如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
+
+ ManualLink: 此步骤为手动配置原生依赖项的指导
+
+首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
+
+此步骤为手动配置原生依赖项的指导。
+
+首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
+
+### 2.1. Overrides RN SDK
+
+为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 `oh-package.json5` 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
+
+关于该字段的作用请阅读[官方说明](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-oh-package-json5-V5#zh-cn_topic_0000001792256137_overrides)
+
+```json
+{
+ "overrides": {
+ "@rnoh/react-native-openharmony": "^0.72.38" // ohpm 在线版本
+ // "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
+ // "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
+ }
+}
+```
+
+### 2.2. 引入原生端代码
+
+目前有两种方法:
+
+- 通过 har 包引入;
+- 直接链接源码。
+
+方法一:通过 har 包引入(推荐)
+
+> [!TIP] har 包位于三方库安装路径的 `harmony` 文件夹下。
+
+打开 `entry/oh-package.json5`,添加以下依赖
+
+```json
+"dependencies": {
+ "@react-native-ohos/react-native-ping": "file:../../node_modules/@react-native-ohos/react-native-ping/harmony/RNPing.har"
+ }
+```
+
+点击右上角的 `sync` 按钮
+
+或者在命令行终端执行:
+
+```bash
+cd entry
+ohpm install
+```
+
+方法二:直接链接源码
+
+> [!TIP] 如需使用直接链接源码,请参考[直接链接源码说明](/zh-cn/link-source-code.md)
+
+### 2.3. 配置 CMakeLists 和引入 xxxPackge
+
+**若涉及接入 codegen-lib 导致的配置项新增,需要在配置项前明确声明:> [!TIP] 版本 vx.x.x 及以上需要**(删除)
+
+打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
+
+```diff
++ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
+
+# RNOH_BEGIN: manual_package_linking_1
++ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-ping/src/main/cpp" ./ping)
+# RNOH_END: manual_package_linking_1
+
+# RNOH_BEGIN: manual_package_linking_2
++ target_link_libraries(rnoh_app PUBLIC rnoh_ping)
+# RNOH_END: manual_package_linking_2
+```
+
+打开 `entry/src/main/cpp/PackageProvider.cpp`,添加:
+
+```diff
+#include "RNOH/PackageProvider.h"
+#include "generated/RNOHGeneratedPackage.h"
++ #include "RNCPingPackage.h"
+
+using namespace rnoh;
+
+std::vector> PackageProvider::getPackages(Package::Context ctx) {
+ return {
+ std::make_shared(ctx),
++ std::make_shared(ctx),
+ };
+}
+```
+
+### 2.5. 在 ArkTs 侧引入 react-native-ping Package
+
+打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加:
+
+```diff
+ ...
++ import {RNReactNativePingPackage} from '@react-native-ohos/react-native-ping/ts';
+
+export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
+ return [
++ new RNReactNativePingPackage(ctx),
+ ];
+}
+```
+
+
+### 2.6. 运行
+
+点击右上角的 `sync` 按钮
+
+或者在命令行终端执行:
+
+```bash
+cd entry
+ohpm install
+```
+
+然后编译、运行即可。
+
+## 3. 约束与限制
+
+### 3.1. 兼容性
+
+请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:[<@react-native-ohos/react-native-ping> Releases](https://github.com/react-native-oh-library/react-native-ping/releases)
+
+本文档内容基于以下版本验证通过:
+
+1. RNOH: 0.72.59; SDK: HarmonyOS-6.0.0(API20); IDE: DevEco Studio 6.0.1.260; ROM: NEXT.0.0.71;
+1. RNOH: 0.77.18-1; SDK: HarmonyOS-6.0.0(API20); IDE: DevEco Studio 6.0.1.260; ROM: NEXT.0.0.71;
+1. RNOH: 0.82.7; SDK: HarmonyOS-6.0.0(API20); IDE: DevEco Studio 6.0.1.260; ROM: NEXT.0.0.71;
+
+### 3.2. 权限要求(如有)
+
+(填入相关权限配置)
+打开 `entry/src/main/resources/base/element/string.json`,添加:
+
+```diff
+...
+{
+ "string": [
++ {
++ "name": "ohos.permission.INTERNET",
++ },
+ ]
+}
+```
+
+### 3.3. 编译运行API要求
+
+> [!TIP] 当前三方库所有版本均已实现版本隔离,支持在 `API20+` 工程编译,及 `API20+` ROM运行。
+
+> [!TIP] 以下功能依赖特定版本的API,使用 `低于指定API版本的工程编译` 或 `低于指定API版本的ROM运行` 均可能导致部分功能受限。
+1. 1.3.0版本引入[NetConn_ProbeResultInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-netconnection-netconn-proberesultinfo),来定义探测结果信息,此API需要在支持`API20+`的工程编译,并在支持`API20+`的ROM上运行,方可生效。
+
+## 6. API
+
+> [!TIP] "Platform"列表示该属性在原三方库上支持的平台。
+
+> [!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
+
+| Name | Description | Type | Required | Platform | HarmonyOS Support |
+| ---- | ----------- | ---- | -------- | -------- | ------------------ |
+| start | 检测网络连接状态(ping Ip) | function | yes | ALL | partially(支持参数ipAddress,options配置不生效) |
+| getTrafficStats | 获取网络状态信息 | function | yes | ALL | yes |
+
+## 7. 错误码
+
+| Name | Description | Type | default | platform | HarmonyOS Support |
+| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
+| PingUtil_Message_Timeout | 超时 | `string` | `0` | iOS,Android | NO |
+| PingUtil_Message_PreviousPingIsStillRunning | 正在运行 | `string` | `1` | / | NO |
+| PingUtil_Message_HostErrorNotSetHost | 未设置ip | `string` | `2` | iOS,Android | YES |
+| PingUtil_Message_HostErrorUnknown | host未知错误 | `string` | `3` | iOS,Android | YES |
+| PingUtil_Message_HostErrorHostNotFound | host错误、host找不到| `string` | `4` | Only iOS | NO |
+| PingUtil_Message_Unknown | 未知错误 | `string` | `5` | Only iOS | NO |
+
+## 7. 遗留问题
+
+- [ ] start方法的options配置不生效()
+- [ ] 错误码只支持PingUtil_Message_HostErrorNotSetHost和PingUtil_Message_HostErrorUnknown,其他不支持()
+
+## 8. 其他
+
+
+## 9. 开源协议
+
+本项目基于 [MIT License (MIT)]() ,请自由地享受和参与开源。
diff --git a/zh-cn/react-native-ping.md b/zh-cn/react-native-ping.md
index 5f9b7405..cc1e7de1 100644
--- a/zh-cn/react-native-ping.md
+++ b/zh-cn/react-native-ping.md
@@ -1,62 +1,45 @@
-> 模板版本:v0.3.0
+> 模板版本:v0.4.0
react-native-ping
-本项目基于 [react-native-ping@[v1.2.8](https://github.com/RoJoHub/react-native-ping)]开发。
+本项目基于 [react-native-ping](https://github.com/RoJoHub/react-native-ping) 开发。
-请到三方库的 Releases 发布地址查看配套的版本信息:([@react-native-ohos/react-native-ping Releases]())。对于未发布到 npm 的旧版本,请参考[安装指南](/zh-cn/tgz-usage.md)安装 tgz 包。
+该第三方库支持直接从 npm 下载,包名为:`@react-native-ohos/react-native-ping` 版本所属关系如下:
+| 三方库名称 | 三方库版本 | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | npm地址 |
+| ------------ | ------------ | ------------------------------ | ------------- | ------------- |------------------------ | ------------- | ------------- |
+| @react-native-ohos/react-native-ping | ~ 1.3.0 | [Github Releases](https://github.com/react-native-oh-library/react-native-ping/releases) | 0.72.* | 否 | API20+ | 1.2.8 | [Npm Address](https://www.npmjs.com/package/@react-native-ohos/react-native-ping) |
+| @react-native-ohos/react-native-ping | ~ 1.4.0 | [Github Releases](https://github.com/react-native-oh-library/react-native-ping/releases) | 0.77.* | 否 | API20+ | 1.2.8 | [Npm Address](https://www.npmjs.com/package/@react-native-ohos/react-native-ping) |
+
+| @react-native-ohos/react-native-ping | ~ 1.5.0 | [Github Releases](https://github.com/react-native-oh-library/react-native-ping/releases) | 0.82.* | 否 | API20+ | 1.2.8 | [Npm Address](https://www.npmjs.com/package/@react-native-ohos/react-native-ping) |
-| 三方库版本 | 发布信息 | 支持 RN 版本 |
-| ---------- | ------------------------------------------------------------------------------------------------------ | ------------ |
-| v1.3.0 | [@react-native-ohos/react-native-ping Releases]() | 0.72 |
-| v1.4.0 | [@react-native-ohos/react-native-ping Releases]() | 0.77 |
-| v1.5.0 | [@react-native-ohos/react-native-ping Releases]() | 0.82 |
## 1. 安装与使用
进入到工程目录并输入以下命令:
-#### npm
+
+
+#### **npm**
```bash
npm install @react-native-ohos/react-native-ping
```
-#### yarn
+#### **yarn**
```bash
yarn add @react-native-ohos/react-native-ping
```
-
-
-
-
+
下面的代码展示了这个库的基本使用场景:
> [!WARNING] 使用时 import 的库名不变。
-### react-native-ping example
-
```js
/**
* react-native-ping 示例
@@ -409,46 +392,50 @@ const styles = StyleSheet.create({
});
export default ReactNativePingDemo;
-
```
-## 2.Manual Link
+## 2. Link
| | 是否支持autolink | RN框架版本 |
-|--------------------------------------|-----------------|------------|
-| ~1.3.0 | Yes | 0.72 |
-| ~1.4.0 | No | 0.77 |
-| ~1.5.0 | No | 0.82 |
+|--------------------------------------|------------------|-----------|
+| ~1.5.0 | No | 0.82 |
+| ~1.4.0 | No | 0.77 |
+| ~1.3.0 | Yes | 0.72 |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
-
ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
-### 1.在工程根目录的 `oh-package.json` 添加 overrides字段
+此步骤为手动配置原生依赖项的指导。
+
+首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
+
+### 2.1. Overrides RN SDK
为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 `oh-package.json5` 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
-关于该字段的作用请阅读[官方说明](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-oh-package-json5-V5#zh-cn_topic_0000001792256137_overrides):
+关于该字段的作用请阅读[官方说明](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-oh-package-json5-V5#zh-cn_topic_0000001792256137_overrides)
```json
{
"overrides": {
- "@rnoh/react-native-openharmony" : "./react_native_openharmony"
+ "@rnoh/react-native-openharmony": "^0.72.38" // ohpm 在线版本
+ // "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
+ // "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
}
}
```
-### 2. 引入原生端代码
+### 2.2. 引入原生端代码
目前有两种方法:
-1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
-2. 直接链接源码。
+- 通过 har 包引入;
+- 直接链接源码。
方法一:通过 har 包引入(推荐)
@@ -458,14 +445,13 @@ export default ReactNativePingDemo;
```json
"dependencies": {
- "@rnoh/react-native-openharmony": "0.72.32",
"@react-native-ohos/react-native-ping": "file:../../node_modules/@react-native-ohos/react-native-ping/harmony/RNPing.har"
}
```
点击右上角的 `sync` 按钮
-或者在终端执行:
+或者在命令行终端执行:
```bash
cd entry
@@ -476,49 +462,21 @@ ohpm install
> [!TIP] 如需使用直接链接源码,请参考[直接链接源码说明](/zh-cn/link-source-code.md)
-### 3. 配置 CMakeLists 和引入 WatermelonDBPackage
+### 2.3. 配置 CMakeLists 和引入 xxxPackge
+
+**若涉及接入 codegen-lib 导致的配置项新增,需要在配置项前明确声明:> [!TIP] 版本 vx.x.x 及以上需要**(删除)
打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
```diff
-project(rnapp)
-cmake_minimum_required(VERSION 3.4.1)
-set(CMAKE_SKIP_BUILD_RPATH TRUE)
-set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
-set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
-set(OH_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
-set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules/@rnoh/react-native-openharmony/src/main/cpp")
-set(RNOH_GENERATED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/generated")
-set(LOG_VERBOSITY_LEVEL 1)
-set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
-set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
-set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
-
-set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
-add_compile_definitions(WITH_HITRACE_SYSTRACE)
-
-add_subdirectory("${RNOH_CPP_DIR}" ./rn)
++ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
# RNOH_BEGIN: manual_package_linking_1
-# RNOH_END: manual_package_linking_1
-
-file(GLOB GENERATED_CPP_FILES "${CMAKE_CURRENT_SOURCE_DIR}/generated/*.cpp") # this line is needed by codegen v1
-
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-ping/src/main/cpp" ./ping)
-
-
-
-add_library(rnoh_app SHARED
- ${GENERATED_CPP_FILES}
- "./PackageProvider.cpp"
- "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
-)
-target_link_libraries(rnoh_app PUBLIC rnoh)
+# RNOH_END: manual_package_linking_1
# RNOH_BEGIN: manual_package_linking_2
+ target_link_libraries(rnoh_app PUBLIC rnoh_ping)
-
-
# RNOH_END: manual_package_linking_2
```
@@ -531,39 +489,35 @@ target_link_libraries(rnoh_app PUBLIC rnoh)
using namespace rnoh;
-std::vector> PackageProvider::getPackages(
- Package::Context ctx) {
- return {
- std::make_shared(ctx), // generated by codegen v1
-+ std::make_shared(ctx)
- };
+std::vector> PackageProvider::getPackages(Package::Context ctx) {
+ return {
+ std::make_shared(ctx),
++ std::make_shared(ctx),
+ };
}
```
-### 4. 在 ArkTs 侧引入 react-native-ping 组件
+### 2.5. 在 ArkTs 侧引入 react-native-ping Package
-找到 `entry/src/main/ets/RNPackagesFactory.ets`,添加:
+打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加:
```diff
-...
-import type {RNPackageContext, RNPackage} from '@rnoh/react-native-openharmony/ts';
+ ...
+ import {RNReactNativePingPackage} from '@react-native-ohos/react-native-ping/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
-return [
-+ new RNReactNativePingPackage(ctx),
+ return [
++ new RNReactNativePingPackage(ctx),
];
- }
-...
-...
+}
```
-### 5.运行
+### 2.6. 运行
点击右上角的 `sync` 按钮
-或者在终端执行:
+或者在命令行终端执行:
```bash
cd entry
@@ -572,20 +526,21 @@ ohpm install
然后编译、运行即可。
-## 3.约束与限制
+## 3. 约束与限制
+
+### 3.1. 兼容性
-### 兼容性
+请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:[<@react-native-ohos/react-native-ping> Releases](https://github.com/react-native-oh-library/react-native-ping/releases)
本文档内容基于以下版本验证通过:
- 此三方库仅支持API20及以上版本
-1. RNOH:0.72.59; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio: 6.0.1.260; ROM:6.0.0.38;
-2. RNOH:0.77.18; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio 6.0.0.858; ROM:6.0.0.112;
-2. RNOH:0.82.X; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio 6.0.0.858; ROM:6.0.0.112;
-### 权限要求
+1. RNOH: 0.72.59; SDK: HarmonyOS-6.0.0(API20); IDE: DevEco Studio 6.0.1.260; ROM: NEXT.0.0.71;
+1. RNOH: 0.77.18-1; SDK: HarmonyOS-6.0.0(API20); IDE: DevEco Studio 6.0.1.260; ROM: NEXT.0.0.71;
+1. RNOH: 0.82.7; SDK: HarmonyOS-6.0.0(API20); IDE: DevEco Studio 6.0.1.260; ROM: NEXT.0.0.71;
-#### 在 entry 目录下添加申请以上权限的原因
+### 3.2. 权限要求(如有)
+(填入相关权限配置)
打开 `entry/src/main/resources/base/element/string.json`,添加:
```diff
@@ -599,26 +554,25 @@ ohpm install
}
```
-### 方法
+### 3.3. 编译运行API要求
-#### start(ipAddress: string, options: { timeout?: number } = {})
+> [!TIP] 当前三方库所有版本均已实现版本隔离,支持在 `API20+` 工程编译,及 `API20+` ROM运行。
-| Name | Description | Type | default | platform | HarmonyOS Support |
-| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
-| ipAddress| ip地址 | `string` | `undefined` | All | YES |
-| options | 配置项 | `object` | `{}` | All | NO |
+> [!TIP] 以下功能依赖特定版本的API,使用 `低于指定API版本的工程编译` 或 `低于指定API版本的ROM运行` 均可能导致部分功能受限。
+1. 1.3.0版本引入[NetConn_ProbeResultInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-netconnection-netconn-proberesultinfo),来定义探测结果信息,此API需要在支持`API20+`的工程编译,并在支持`API20+`的ROM上运行,方可生效。
-#### getTrafficStats()
+## 6. API
-| Name | Description | Type | default | platform | HarmonyOS Support |
-| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
-| receivedNetworkSpeed| 接收速度 | `number` | `0` | All | YES |
-| sendNetworkSpeed | 发送速度 | `number` | `0` | All | YES |
-| receivedNetworkTotal| 接收总量 | `number` | `0` | All | YES |
-| sendNetworkTotal | 发送总量 | `number` | `0` | All | YES |
+> [!TIP] "Platform"列表示该属性在原三方库上支持的平台。
+
+> [!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
+| Name | Description | Type | Required | Platform | HarmonyOS Support |
+| ---- | ----------- | ---- | -------- | -------- | ------------------ |
+| start | 检测网络连接状态(ping Ip) | function | yes | ALL | partially(支持参数ipAddress,options配置不生效) |
+| getTrafficStats | 获取网络状态信息 | function | yes | ALL | yes |
-### 错误码
+## 7. 错误码
| Name | Description | Type | default | platform | HarmonyOS Support |
| :------- | :----------- | :------: | :---------: | -------- | ----------------- |
@@ -629,11 +583,14 @@ ohpm install
| PingUtil_Message_HostErrorHostNotFound | host错误、host找不到| `string` | `4` | Only iOS | NO |
| PingUtil_Message_Unknown | 未知错误 | `string` | `5` | Only iOS | NO |
-## 5.遗留问题
+## 7. 遗留问题
+
+- [ ] start方法的options配置不生效()
+- [ ] 错误码只支持PingUtil_Message_HostErrorNotSetHost和PingUtil_Message_HostErrorUnknown,其他不支持()
+
+## 8. 其他
-- [ ] start方法的options配置不生效
-- [ ] 错误码只支持PingUtil_Message_HostErrorNotSetHost和PingUtil_Message_HostErrorUnknown,其他不支持
-## 7.开源协议
+## 9. 开源协议
本项目基于 [MIT License (MIT)]() ,请自由地享受和参与开源。
--
Gitee