diff --git a/zh-cn/react-native-community-netinfo.md b/zh-cn/react-native-community-netinfo.md index 2ae2054effcf4e81eaf8f70d65317a683c8fd362..1dc2f10448c2e58ef76943cd6c4c8e25ee01b29b 100644 --- a/zh-cn/react-native-community-netinfo.md +++ b/zh-cn/react-native-community-netinfo.md @@ -1,4 +1,3 @@ - > 模板版本:v0.2.2

@@ -49,27 +48,27 @@ import { View, Text } from "react-native"; import NetInfo, { useNetInfo } from "@react-native-community/netinfo"; const App = () => { - const [fetchInfo, setFetchInfo] = React.useState(""); - const [refreshInfo, setRefreshInfo] = React.useState(""); - const netInfo = useNetInfo(); - - NetInfo.fetch().then((state) => { - setFetchInfo(JSON.stringify(state)); - }); - NetInfo.refresh().then((state) => { - setRefreshInfo(JSON.stringify(state)); - }); - - return ( - - Type: {netInfo.type} - Is Connected? {netInfo.isConnected?.toString()} - fetch(): - {fetchInfo} - refresh(): - {refreshInfo} - - ); + const [fetchInfo, setFetchInfo] = React.useState(""); + const [refreshInfo, setRefreshInfo] = React.useState(""); + const netInfo = useNetInfo(); + + NetInfo.fetch().then((state) => { + setFetchInfo(JSON.stringify(state)); + }); + NetInfo.refresh().then((state) => { + setRefreshInfo(JSON.stringify(state)); + }); + + return ( + + Type: {netInfo.type} + Is Connected? {netInfo.isConnected?.toString()} + fetch(): + {fetchInfo} + refresh(): + {refreshInfo} + + ); }; export default App; ``` @@ -80,6 +79,17 @@ export default App; 首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony` +### 在工程根目录的 `oh-package.json5` 添加 overrides 字段 + +```json +{ + ... + "overrides": { + "@rnoh/react-native-openharmony" : "./react_native_openharmony" + } +} +``` + ### 引入原生端代码 目前有两种方法: @@ -96,8 +106,7 @@ export default App; ```json "dependencies": { "@rnoh/react-native-openharmony": "file:../react_native_openharmony", - - "rnoh-netinfo": "file:../../node_modules/@react-native-oh-tpl/netinfo/harmony/netinfo.har" + "@react-native-oh-tpl/netinfo": "file:../../node_modules/@react-native-oh-tpl/netinfo/harmony/netinfo.har" } ``` @@ -121,34 +130,44 @@ ohpm install ```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(OH_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules") +set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules") ++ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules") set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp") +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(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: add_package_subdirectories +# RNOH_BEGIN: manual_package_linking_1 add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package) -+ add_subdirectory("${OH_MODULE_DIR}/rnoh-netinfo/src/main/cpp" ./netinfo) -# RNOH_END: add_package_subdirectories ++ add_subdirectory("${OH_MODULE}/@react-native-oh-tpl/netinfo/src/main/cpp" ./netinfo) +# RNOH_END: manual_package_linking_1 + +file(GLOB GENERATED_CPP_FILES "./generated/*.cpp") add_library(rnoh_app SHARED + ${GENERATED_CPP_FILES} "./PackageProvider.cpp" "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp" ) - target_link_libraries(rnoh_app PUBLIC rnoh) -# RNOH_BEGIN: link_packages +# RNOH_BEGIN: manual_package_linking_2 target_link_libraries(rnoh_app PUBLIC rnoh_sample_package) + target_link_libraries(rnoh_app PUBLIC rnoh_netinfo) -# RNOH_END: link_packages +# RNOH_END: manual_package_linking_2 ``` 打开 `entry/src/main/cpp/PackageProvider.cpp`,添加: ```diff #include "RNOH/PackageProvider.h" +#include "generated/RNOHGeneratedPackage.h" #include "SamplePackage.h" + #include "RNCNetInfoPackage.h" @@ -156,8 +175,9 @@ using namespace rnoh; std::vector> PackageProvider::getPackages(Package::Context ctx) { return { - std::make_shared(ctx), -+ std::make_shared(ctx) + std::make_shared(ctx), + std::make_shared(ctx), ++ std::make_shared(ctx) }; } ``` @@ -167,9 +187,8 @@ std::vector> PackageProvider::getPackages(Package::Cont 打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加: ```diff -import type {RNPackageContext, RNPackage} from 'rnoh/ts'; -import {SamplePackage} from 'rnoh-sample-package/ts'; -+ import {NetInfoPackage} from 'rnoh-netinfo/ts'; + ... ++ import {NetInfoPackage} from '@react-native-oh-tpl/netinfo/ts'; export function createRNPackages(ctx: RNPackageContext): RNPackage[] { return [ @@ -192,11 +211,13 @@ ohpm install 然后编译、运行即可。 -## 兼容性 +## 约束与限制 -本文档内容基于以下版本验证通过: +### 兼容性 -RNOH:0.72.27; SDK:HarmonyOS-Next-DB1 5.0.0.29(SP1) ; IDE:DevEco Studio 5.0.3.400; ROM:3.0.0.25; +要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。 + +请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:[@react-native-oh-tpl/netinfo Releases](https://github.com/react-native-oh-library/react-native-netinfo/releases) ## 属性 @@ -207,91 +228,89 @@ RNOH:0.72.27; SDK:HarmonyOS-Next-DB1 5.0.0.29(SP1) ; IDE:DevEco Studio 5.0 #### NetInfoStateType -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| ----------- | ---------------------------------------------------------- | ------ | -------- | ------------ | ----------------- | -| `none` | No network connection is active | String | no | All | yes | -| `unknown` | The network state could not or has yet to be be determined | String | no | All | yes | -| `cellular` | Active network over cellular | String | no | All | no | -| `wifi` | Active network over Wifi | String | no | All | yes | -| `bluetooth` | Active network over Bluetooth | String | no | Android, Web | no | -| `ethernet` | Active network over wired ethernet | String | no | All | no | -| `wimax` | Active network over WiMax | String | no | Android, Web | no | -| `vpn` | Active network over VPN | String | no | Android | no | -| `other` | Active network over another type of network | String | no | All | no | +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|-------------|------------------------------------------------------------|--------|----------|-------------|-------------------| +| `none` | No network connection is active | String | no | iOS Android | yes | +| `unknown` | The network state could not or has yet to be be determined | String | no | iOS Android | yes | +| `cellular` | Active network over cellular | String | no | iOS Android | no | +| `wifi` | Active network over Wifi | String | no | iOS Android | yes | +| `bluetooth` | Active network over Bluetooth | String | no | Android | no | +| `ethernet` | Active network over wired ethernet | String | no | iOS Android | no | +| `wimax` | Active network over WiMax | String | no | Android | no | +| `vpn` | Active network over VPN | String | no | Android | no | +| `other` | Active network over another type of network | String | no | iOS Android | no | #### NetInfoCellularGeneration -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| ------ | ----------------------------------------------------------------------------------------------------------------- | ------ | -------- | --------------------- | ----------------- | -| `null` | Either we are not currently connected to a cellular network or type could not be determined | String | no | Android, iOS, Windows | no | -| `2g` | Currently connected to a 2G cellular network. Includes CDMA, EDGE, GPRS, and IDEN type connections | String | no | Android, iOS, Windows | no | -| `3g` | Currently connected to a 3G cellular network. Includes EHRPD, EVDO, HSPA, HSUPA, HSDPA, and UTMS type connections | String | no | Android, iOS, Windows | no | -| `4g` | Currently connected to a 4G cellular network. Includes HSPAP and LTE type connections | String | no | Android, iOS, Windows | no | -| `5g` | Currently connected to a 5G cellular network. Includes NRNSA (iOS only) and NR type connections | String | no | Android, iOS, Windows | no | +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|--------|-------------------------------------------------------------------------------------------------------------------|--------|----------|-------------|-------------------| +| `null` | Either we are not currently connected to a cellular network or type could not be determined | String | no | iOS Android | no | +| `2g` | Currently connected to a 2G cellular network. Includes CDMA, EDGE, GPRS, and IDEN type connections | String | no | iOS Android | no | +| `3g` | Currently connected to a 3G cellular network. Includes EHRPD, EVDO, HSPA, HSUPA, HSDPA, and UTMS type connections | String | no | iOS Android | no | +| `4g` | Currently connected to a 4G cellular network. Includes HSPAP and LTE type connections | String | no | iOS Android | no | +| `5g` | Currently connected to a 5G cellular network. Includes NRNSA (iOS only) and NR type connections | String | no | iOS Android | no | #### NetInfoState -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------- | -------- | -------- | ----------------- | -| `type` | The type of the current connection. | NetInfoStateType | yes | All | yes | -| `isConnected` | If there is an active network connection. Defaults to null on most platforms for unknown networks. Note: Web browsers report network type unknown for many otherwise valid networks (https://caniuse.com/netinfo), so isConnected may be true or false and represent a real connection status even for unknown network types in certain cases. | boolean, null | yes | All | yes | -| `isInternetReachable` | If the internet is reachable with the currently active network connection. If unknown defaults to null | boolean, null | yes | All | yes | -| `isWifiEnabled` | (Android only) Whether the device's WiFi is ON or OFF. | boolean | yes | All | yes | -| `details` | The value depends on the type value. See below. | | yes | All | yes | +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|----------|-------------|-------------------| +| `type` | The type of the current connection. | NetInfoStateType | yes | iOS Android | yes | +| `isConnected` | If there is an active network connection.
Defaults to null on most platforms for unknown networks.
Note: Web browsers report network type unknown for many otherwise valid networks (https://caniuse.com/netinfo),
so isConnected may be true or false and represent a real connection status even for unknown network types in certain cases. | boolean, null | yes | iOS Android | yes | +| `isInternetReachable` | If the internet is reachable with the currently active network connection.
If unknown defaults to null | boolean, null | yes | iOS Android | yes | +| `isWifiEnabled` | (Android only) Whether the device's WiFi is ON or OFF. | boolean | yes | iOS Android | yes | +| `details` | The value depends on the type value. See below. | | yes | iOS Android | yes | ### Details -#### Type is none or unknown - -`details` is `null`. - #### Type is wifi -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | -------- | ---------------------------------- | ----------------- | -| `isConnectionExpensive` | If the network connection is considered "expensive". This could be in either energy or monetary terms. | boolean | yes | All | yes | -| `ssid` | The SSID of the network. May not be present, null, or an empty string if it cannot be determined. On iOS, your app must meet at least one of the following requirements and you must set the shouldFetchWiFiSSID configuration option or no attempt will be made to fetch the SSID. On Android, you need to have the ACCESS_FINE_LOCATION permission in your AndroidManifest.xml and accepted by the user. | string | yes | Android, iOS (not tvOS), Windows | yes | -| `bssid` | The BSSID of the network. May not be present, null, or an empty string if it cannot be determined. On iOS, make sure your app meets at least one of the following requirements. On Android, you need to have the ACCESS_FINE_LOCATION permission in your AndroidManifest.xml and accepted by the user. | string | yes | Android, iOS (not tvOS), Windows\* | yes | -| `strength` | An integer number from 0 to 100 for the signal strength. May not be present if the signal strength cannot be determined. | number | yes | Android, Windows | yes | -| `ipAddress` | The external IP address. Can be in IPv4 or IPv6 format. May not be present if it cannot be determined. | string | yes | Android, iOS, macOS, Windows | yes | -| `subnet` | The subnet mask in IPv4 format. May not be present if it cannot be determined. | string | yes | Android, iOS, macOS | yes | -| `frequency` | Network frequency. Example: For 2.4 GHz networks, the method will return 2457. May not be present if it cannot be determined. | number | yes | Android, Windows\* | yes | -| `linkSpeed` | The link speed in Mbps. | number | yes | Android | yes | -| `rxLinkSpeed` | The current receive link speed in Mbps. | number | yes | Android | yes | -| `txLinkSpeed` | The current transmit link speed in Mbps. | number | yes | Android | yes | +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|-------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|----------|-------------|-------------------| +| `isConnectionExpensive` | If the network connection is considered "expensive".
This could be in either energy or monetary terms. | boolean | yes | iOS Android | yes | +| `ssid` | The SSID of the network.
May not be present, null, or an empty string if it cannot be determined.
On iOS, your app must meet at least one of the following requirements and you must set the shouldFetchWiFiSSID configuration option or no attempt will be made to fetch the SSID.
On Android, you need to have the ACCESS_FINE_LOCATION permission in your AndroidManifest.
xml and accepted by the user. | string | yes | iOS Android | yes | +| `bssid` | The BSSID of the network.
May not be present, null, or an empty string if it cannot be determined.
On iOS, make sure your app meets at least one of the following requirements.
On Android, you need to have the ACCESS_FINE_LOCATION permission in your AndroidManifest.
xml and accepted by the user. | string | yes | iOS Android | yes | +| `strength` | An integer number from 0 to 100 for the signal strength.
May not be present if the signal strength cannot be determined. | number | yes | Android | yes | +| `ipAddress` | The external IP address. Can be in IPv4 or IPv6 format.
May not be present if it cannot be determined. | string | yes | iOS Android | yes | +| `subnet` | The subnet mask in IPv4 format.
May not be present if it cannot be determined. | string | yes | iOS Android | yes | +| `frequency` | Network frequency. Example: For 2.4 GHz networks, the method will return 2457.
May not be present if it cannot be determined. | number | yes | Android | yes | +| `linkSpeed` | The link speed in Mbps. | number | yes | Android | yes | +| `rxLinkSpeed` | The current receive link speed in Mbps. | number | yes | Android | yes | +| `txLinkSpeed` | The current transmit link speed in Mbps. | number | yes | Android | yes | #### Type is cellular -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| ----------------------- | --------------------------------------------------------------------------------------------------------------------- | :-----------------------: | :------: | :-------------------: | :---------------: | -| `isConnectionExpensive` | If the network connection is considered "expensive". This could be in either energy or monetary terms. | boolean | yes | All | no | -| `cellularGeneration` | The generation of the cell network the user is connected to. This can give an indication of speed, but no guarantees. | NetInfoCellularGeneration | yes | Android, iOS, Windows | no | -| `carrier` | The network carrier name. May not be present or may be empty if none can be determined. | +string | yes | Android, iOS | no | +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|-------------------------|-----------------------------------------------------------------------------------------------------------------------|:-------------------------:|:--------:|:-----------:|:-----------------:| +| `isConnectionExpensive` | If the network connection is considered "expensive". This could be in either energy or monetary terms. | boolean | yes | iOS Android | no | +| `cellularGeneration` | The generation of the cell network the user is connected to. This can give an indication of speed, but no guarantees. | NetInfoCellularGeneration | yes | iOS Android | no | +| `carrier` | The network carrier name. May not be present or may be empty if none can be determined. | +string | yes | iOS Android | no | #### Type is bluetooth, ethernet, wimax, vpn, or other -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| ----------------------- | ------------------------------------------------------------------------------------------------------ | :-----: | :------: | :------: | :---------------: | -| `isConnectionExpensive` | If the network connection is considered "expensive". This could be in either energy or monetary terms. | boolean | yes | All | no | +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|-------------------------|-------------------------------------------------------------------------------------------------------------|:-------:|:--------:|:-----------:|:-----------------:| +| `isConnectionExpensive` | If the network connection is considered "expensive".
This could be in either energy or monetary terms. | boolean | yes | iOS Android | no | ## 静态方法 -| Name | Description | Type | Required | Platform | HarmonyOS Support | -| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------: | :------: | :------: | :---------------: | -| `fetch()` | Returns a Promise that resolves to a NetInfoState object. | function | yes | All | yes | -| `refresh()` | Updates NetInfo's internal state, then returns a Promise that resolves to a NetInfoState object. This is similar to fetch(), but really only useful on platforms that do not supply internet reachability natively. For example, you can use it to immediately re-run an internet reachability test if a network request fails unexpectedly. | function | yes | All | yes | -| `addEventListener()` | Subscribe to connection information. The callback is called with a parameter of type NetInfoState whenever the connection state changes. Your listener will be called with the latest information soon after you subscribe and then with any subsequent changes afterwards. You should not assume that the listener is called in the same way across devices or platforms. | function | yes | All | yes | -| `useNetInfo()` | A React Hook which can be used to get access to the latest state from the global instance. It returns a hook with the NetInfoState type. | function | yes | All | yes | -| `useNetInfoInstance()` | A React Hook which can be used to create and manage an isolated instance of a network manager class. It returns a refresh function and the current NetInfoState. | function | yes | All | yes | +> [!tip] "Platform"列表示该属性在原三方库上支持的平台。 + +> [!tip] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。 + +| Name | Description | Type | Required | Platform | HarmonyOS Support | +|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------:|:--------:|:-----------:|:-----------------:| +| `fetch()` | Returns a Promise that resolves to a NetInfoState object. | function | yes | iOS Android | yes | +| `refresh()` | Updates NetInfo's internal state, then returns a Promise that resolves to a NetInfoState object.
This is similar to fetch(), but really only useful on platforms that do not supply internet reachability natively.
For example, you can use it to immediately re-run an internet reachability test if a network request fails unexpectedly. | function | yes | iOS Android | yes | +| `addEventListener()` | Subscribe to connection information.
The callback is called with a parameter of type NetInfoState whenever the connection state changes.
Your listener will be called with the latest information soon after you subscribe and then with any subsequent changes afterwards.
You should not assume that the listener is called in the same way across devices or platforms. | function | yes | iOS Android | yes | +| `useNetInfo()` | A React Hook which can be used to get access to the latest state from the global instance.
It returns a hook with the NetInfoState type. | function | yes | iOS Android | yes | +| `useNetInfoInstance()` | A React Hook which can be used to create and manage an isolated instance of a network manager class.
It returns a refresh function and the current NetInfoState. | function | yes | iOS Android | yes | ## 遗留问题 -- 涉及非 wifi 环境下的接口属性未适配 +- [ ] 涉及非 wifi 环境下的接口属性未适配: [issue#](https://github.com/issue地址1) ## 其他 ## 开源协议 本项目基于 [The MIT License (MIT)](https://github.com/react-native-netinfo/react-native-netinfo/blob/master/LICENSE) ,请自由地享受和参与开源。 - - \ No newline at end of file