|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| figures/ | | 12-May-2024 | - | | |
| frameworks/ | | 12-May-2024 | - | 18,907 | 14,391 |
| interfaces/ | | 12-May-2024 | - | 4,582 | 2,314 |
| sa_profile/ | | 12-May-2024 | - | 253 | 114 |
| services/ | | 12-May-2024 | - | 34,927 | 24,318 |
| test/ | | 12-May-2024 | - | 19,950 | 14,162 |
| utils/ | | 12-May-2024 | - | 3,694 | 2,715 |
| .clang-format | D | 12-May-2024 | 7.2 KiB | 173 | 172 |
| .gitignore | D | 12-May-2024 | 65 | 4 | 4 |
| LICENSE | D | 12-May-2024 | 9.9 KiB | 177 | 150 |
| OAT.xml | D | 12-May-2024 | 4.2 KiB | 67 | 15 |
| README.md | D | 12-May-2024 | 21.7 KiB | 165 | 145 |
| README_zh.md | D | 12-May-2024 | 20.9 KiB | 291 | 247 |
| bundle.json | D | 12-May-2024 | 6.3 KiB | 136 | 135 |
| hisysevent.yaml | D | 12-May-2024 | 4.2 KiB | 90 | 64 |
| netmanager_base_config.gni | D | 12-May-2024 | 2.7 KiB | 64 | 57 |
README.md
1# NetManager
2
3## Introduction
4
5The NetManager module is a tailorable component of the Telephony subsystem. Key components of the module include NetConnService (connection management), NetPolicyService (policy management), NetStatsService (traffic management), TetheringService (network sharing), VpnService (VPN management), and EtherNetService (Ethernet connection).
6
7**Figure 1** NetManager architecture
8
9![net_conn_manager_arch](figures/net_conn_manager_arch.png)
10
11## Directory Structure
12
13```
14foundation/communication/netmanager_base/
15├─figures // Architecture
16├─frameworks // Framework layer
17│ ├─js // JavaScript APIs
18│ └─native // Native APIs
19├─interfaces // API definitions
20│ ├─innerkits // JavaScript APIs
21│ └─kits // Native APIs
22├─sa_profile // SA profile
23├─services // IPC server-side implementation
24├─test // Test code
25└─utils // Utilities
26```
27
28## Available APIs
29
30| Type| API| Description|
31| ---- | ---- | ---- |
32| ohos.net.connection | function getDefaultNet(callback: AsyncCallback\<NetHandle>): void; |Creates a **NetHandle** object that contains the **netId** of the default network. This API uses an asynchronous callback to return the result.|
33| ohos.net.connection | function getDefaultNet(): Promise\<NetHandle>; |Creates a **NetHandle** object that contains the **netId** of the default network. This API uses a promise to return the result.|
34| ohos.net.connection | function getAllNets(callback: AsyncCallback\<Array\<NetHandle>>): void;| Obtains the list of **NetHandle** objects of the connected network. This API uses an asynchronous callback to return the result.|
35| ohos.net.connection | function getAllNets(): Promise\<Array\<NetHandle>>;| Obtains the list of **NetHandle** objects of the connected network. This API uses a promise to return the result.|
36| ohos.net.connection | function getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void; |Obtains link information of the default network. This API uses an asynchronous callback to return the result.|
37| ohos.net.connection | function getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>; |Obtains link information of the default network. This API uses a promise to return the result.|
38| ohos.net.connection | function getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void; |Obtains the capability set of the default network. This API uses an asynchronous callback to return the result.|
39| ohos.net.connection | function getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>; |Obtains the capability set of the default network. This API uses a promise to return the result.|
40| ohos.net.connection | function hasDefaultNet(callback: AsyncCallback\<boolean>): void; |Checks whether the default network is available. This API uses an asynchronous callback to return the result.|
41| ohos.net.connection | function hasDefaultNet(): Promise\<boolean>; |Checks whether the default network is available. This API uses a promise to return the result.|
42| ohos.net.connection | function getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
43| ohos.net.connection | function getAddressesByName(host: string): Promise\<Array\<NetAddress>>; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses a promise to return the result.|
44| ohos.net.connection | function createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection; | Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.|
45| ohos.net.connection | function enableAirplaneMode(callback: AsyncCallback\<void>): void; | Enables the airplane mode. This API uses an asynchronous callback to return the result.|
46| ohos.net.connection | function enableAirplaneMode(): Promise\<void>;|Enables the airplane mode. This API uses a promise to return the result.|
47| ohos.net.connection | function disableAirplaneMode(callback: AsyncCallback\<void>): void;| Disables the airplane mode. This API uses an asynchronous callback to return the result.|
48| ohos.net.connection | function disableAirplaneMode(): Promise\<void>;| Disables the airplane mode. This API uses a promise to return the result.|
49| ohos.net.connection | function reportNetConnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.|
50| ohos.net.connection | function reportNetConnected(netHandle: NetHandle): Promise\<void>;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses a promise to return the result.|
51| ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Reports a **netUnavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.|
52| ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle): Promise\<void>;| Reports a **netUnavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses a promise to return the result.|
53| ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void; | Binds TCPSocket or UDPSocket to the current network. This API uses an asynchronous callback to return the result.|
54| ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;| Binds TCPSocket or UDPSocket to the current network. This API uses a promise to return the result.|
55| ohos.net.connection.NetHandle | getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |Obtains all IP addresses of the default network by resolving the domain name. This API uses an asynchronous callback to return the result.|
56| ohos.net.connection.NetHandle | getAddressesByName(host: string): Promise\<Array\<NetAddress>>; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses a promise to return the result.|
57| ohos.net.connection.NetHandle | getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void; |Obtains an IP address of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
58| ohos.net.connection.NetHandle | getAddressByName(host: string): Promise\<NetAddress>; |Obtains an IP address of the specified network by resolving the domain name. This API uses a promise to return the result.|
59| ohos.net.connection.NetConnection | on(type: 'netAvailable', callback: Callback\<NetHandle>): void; |Listens to **netAvailable** events.|
60| ohos.net.connection.NetConnection | on(type: 'netCapabilitiesChange', callback: Callback\<{ netHandle: NetHandle, netCap: NetCapabilities }>): void; |Listens to **netCapabilitiesChange** events.|
61| ohos.net.connection.NetConnection | on(type: 'netConnectionPropertiesChange', callback: Callback\<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void; |Listens to **netConnectionPropertiesChange** events.|
62| ohos.net.connection.NetConnection | on(type: 'netLost', callback: Callback\<NetHandle>): void; |Listens to **netLost** events.|
63| ohos.net.connection.NetConnection | on(type: 'netUnavailable', callback: Callback\<void>): void; |Listens to **netUnavailable** events.|
64| ohos.net.connection.NetConnection | register(callback: AsyncCallback\<void>): void; |Registers an observer for the default network or the network specified in **createNetConnection**.|
65| ohos.net.connection.NetConnection | unregister(callback: AsyncCallback\<void>): void; |Unregisters the observer for the default network or the network specified in **createNetConnection**.|
66| @ohos.net.policy | function setBackgroundPolicy(allow: boolean, callback: AsyncCallback\<void>): void; | Sets the background network policy. This API uses an asynchronous callback to return the result.|
67| @ohos.net.policy | function setBackgroundPolicy(allow: boolean): Promise\<void>; | Sets the background network policy. This API uses a promise to return the result.|
68| @ohos.net.policy | function getBackgroundPolicy(callback: AsyncCallback\<NetBackgroundPolicy>): void; | Obtains the background network policy. This API uses an asynchronous callback to return the result.|
69| @ohos.net.policy | function getBackgroundPolicy(): Promise\<NetBackgroundPolicy>; | Obtains the background network policy. This API uses a promise to return the result.|
70| @ohos.net.policy | function setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\<void>): void; | Sets the network access policy for the application by the specified UID. This API uses an asynchronous callback to return the result.|
71| @ohos.net.policy | function setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>; | Sets the network access policy for the application by the specified UID. This API uses a promise to return the result.|
72| @ohos.net.policy | function getPolicyByUid(uid: number, callback: AsyncCallback\<NetUidPolicy>): void; | Obtains the network access policy of the application by the specified UID. This API uses an asynchronous callback to return the result.|
73| @ohos.net.policy | function getPolicyByUid(uid: number): Promise\<NetUidPolicy>; | Obtains the network access policy of the application by the specified UID. This API uses a promise to return the result.|
74| @ohos.net.policy | function getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\<Array\<number>>): void; | Obtains the UID array of the application by the network access policy. This API uses an asynchronous callback to return the result.|
75| @ohos.net.policy | function getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>; | Obtains the UID array of the application by the network access policy. This API uses a promise to return the result.|
76| @ohos.net.policy | function getNetQuotaPolicies(callback: AsyncCallback\<Array\<NetQuotaPolicy>>): void; | Obtains the network quota policy. This API uses an asynchronous callback to return the result.|
77| @ohos.net.policy | function getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>; | Obtains the network quota policy. This API uses a promise to return the result.|
78| @ohos.net.policy | function setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>, callback: AsyncCallback\<void>): void; | Sets the network quota policy. This API uses an asynchronous callback to return the result.|
79| @ohos.net.policy | function setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>): Promise\<void>; | Sets the network quota policy. This API uses a promise to return the result.|
80| @ohos.net.policy | function restoreAllPolicies(simId: string, callback: AsyncCallback\<void>): void; | Restores all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses an asynchronous callback to return the result.|
81| @ohos.net.policy | function restoreAllPolicies(simId: string): Promise\<void>; | Restores all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses a promise to return the result.|
82| @ohos.net.policy | function isUidNetAllowedIsMetered(uid: number, isMetered: boolean, callback: AsyncCallback\<boolean>): void; | Checks whether the application corresponding to the specified UID can access the metering or non-metering network. This API uses an asynchronous callback to return the result.|
83| @ohos.net.policy | function isUidNetAllowedIsMetered(uid: number, isMetered: boolean): Promise\<boolean>; | Checks whether the application corresponding to the specified UID can access the metering or non-metering network. This API uses a promise to return the result.|
84| @ohos.net.policy | function isUidNetAllowedIface(uid: number, iface: string, callback: AsyncCallback\<boolean>): void; | Checks whether the application corresponding to the specified UID can access the network corresponding to the specified iface. This API uses an asynchronous callback to return the result.|
85| @ohos.net.policy | function isUidNetAllowedIface(uid: number, iface: string): Promise\<boolean>; | Checks whether the application corresponding to the specified UID can access the network corresponding to the specified iface. This API uses a promise to return the result.|
86| @ohos.net.policy | function setDeviceIdleAllowlist(uid: number, isAllow: boolean, callback: AsyncCallback\<void>): void | Sets whether the application corresponding to the specified UID is in the device idle allowlist. This API uses an asynchronous callback to return the result.|
87| @ohos.net.policy | function setDeviceIdleAllowlist(uid: number, isAllow: boolean): Promise\<void>; | Sets whether the application corresponding to the specified UID is in the device idle allowlist. This API uses a promise to return the result.|
88| @ohos.net.policy | function getDeviceIdleAllowlist(callback: AsyncCallback\<Array\<number>>): void | Obtains the UID array of the application in the device idle allowlist. This API uses an asynchronous callback to return the result.|
89| @ohos.net.policy | function getDeviceIdleAllowlist(): Promise\<Array\<number>>; | Obtains the UID array of the application in the device idle allowlist. This API uses a promise to return the result.|
90| @ohos.net.policy | function getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\<NetBackgroundPolicy>): void | Checks whether the application corresponding to the specified UID can access the background network. This API uses an asynchronous callback to return the result.|
91| @ohos.net.policy | function getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>; | Checks whether the application corresponding to the specified UID can access the background network. This API uses a promise to return the result.|
92| @ohos.net.policy | function resetPolicies(simId: string, callback: AsyncCallback\<void>): void; | Resets all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses an asynchronous callback to return the result.|
93| @ohos.net.policy | function resetPolicies(simId: string): Promise\<void>; | Resets all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses a promise to return the result.|
94| @ohos.net.policy | function updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType, callback: AsyncCallback\<void>): void | Updates the reminder policy. **netType** indicates the network type, **simId** indicates the SIM card ID, and **remindType** indicates the reminder type. This API uses an asynchronous callback to return the result.|
95| @ohos.net.policy | function updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType): Promise\<void>; | Updates the reminder policy. **netType** indicates the network type, **simId** indicates the SIM card ID, and **remindType** indicates the reminder type. This API uses a promise to return the result.|
96| @ohos.net.policy | function on(type: 'netUidPolicyChange', callback: Callback\<{ uid: number, policy: NetUidPolicy }>): void; | Registers the callback for policy changes.|
97| @ohos.net.policy | function off(type: 'netUidPolicyChange', callback: Callback\<void>): void; | Unregisters the callback for policy changes.|
98| @ohos.net.policy | function on(type: "netUidRuleChange", callback: Callback\<{ uid: number, rule: NetUidRule }>): void; | Registers the callback for rule changes.|
99| @ohos.net.policy | function off(type: "netUidRuleChange", callback: Callback\<void>): void; | Unregisters the callback for rule changes.|
100| @ohos.net.policy | function on(type: "netMeteredIfacesChange", callback: Callback\<Array\<string>>): void; | Registers the callback for metered iface changes.|
101| @ohos.net.policy | function off(type: "netMeteredIfacesChange", callback: Callback\<void>): void; | Unregisters the callback for metered iface changes.|
102| @ohos.net.policy | function on(type: "netQuotaPolicyChange", callback: Callback\<Array\<NetQuotaPolicy>>): void; | Registers the callback for network quota policy changes.|
103| @ohos.net.policy | function off(type: "netQuotaPolicyChange", callback: Callback\<void>): void; | Unregisters the callback for network quota policy changes.|
104| @ohos.net.policy | function on(type: "netBackgroundPolicyChange", callback: Callback\<boolean>): void; | Registers the callback for background network policy changes.|
105| @ohos.net.policy | function off(type: "netBackgroundPolicyChange", callback: Callback\<void>): void; | Unregisters the callback for background network policy changes.|
106| ohos.net.statistics | function getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void; |Obtains the downlink traffic data of the specified NIC. This API uses an asynchronous callback to return the result.|
107| ohos.net.statistics | function getIfaceRxBytes(nic: string): Promise\<number>; |Obtains the downlink traffic data of the specified NIC. This API uses a promise to return the result.|
108| ohos.net.statistics | function getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void; |Obtains the uplink traffic data of the specified NIC. This API uses an asynchronous callback to return the result.|
109| ohos.net.statistics | function getIfaceRxBytes(nic: string): Promise\<number>; |Obtains the uplink traffic data of the specified NIC. This API uses a promise to return the result.|
110| ohos.net.statistics | function getCellularRxBytes(callback: AsyncCallback\<number>): void; |Obtains the downlink traffic data of the specified cellular network. This API uses an asynchronous callback to return the result.|
111| ohos.net.statistics | function getCellularRxBytes(): Promise\<number>; |Obtains the downlink traffic data of the specified cellular network. This API uses a promise to return the result.|
112| ohos.net.statistics | function getCellularTxBytes(callback: AsyncCallback\<number>): void; |Obtains the uplink traffic data of the specified cellular network. This API uses an asynchronous callback to return the result.|
113| ohos.net.statistics | function getCellularTxBytes(): Promise\<number>; |Obtains the uplink traffic data of the specified cellular network. This API uses a promise to return the result.|
114| ohos.net.statistics | function getAllRxBytes(callback: AsyncCallback\<number>): void; |Obtains the downlink traffic data of all NICs. This API uses an asynchronous callback to return the result.|
115| ohos.net.statistics | function getAllRxBytes(): Promise\<number>; |Obtains the downlink traffic data of all NICs. This API uses a promise to return the result.|
116| ohos.net.statistics | function getAllTxBytes(callback: AsyncCallback\<number>): void; |Obtains the uplink traffic data of all NICs. This API uses an asynchronous callback to return the result.|
117| ohos.net.statistics | function getAllTxBytes(): Promise\<number>; |Obtains the uplink traffic data of all NICs. This API uses a promise to return the result.|
118| ohos.net.statistics | function getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void; |Obtains the downlink traffic data of the specified application. This API uses an asynchronous callback to return the result.|
119| ohos.net.statistics | function getUidRxBytes(uid: number): Promise\<number>; |Obtains the downlink traffic data of the specified application. This API uses a promise to return the result.|
120| ohos.net.statistics | function getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void; |Obtains the uplink traffic data of the specified application. This API uses an asynchronous callback to return the result.|
121| ohos.net.statistics | function getUidTxBytes(uid: number): Promise\<number>; |Obtains the uplink traffic data of the specified application. This API uses a promise to return the result.|
122
123
124## Usage
125
126### Obtaining IP Addresses of the Default Network by Resolving the Domain Name (Promise Mode)
127
128* Example
129 ```javascript
130 import net_connection from "@ohos.net.connection"
131 ```
132 ```javascript
133 net_connection.getAddressesByName("www.example.com").then(function (addresses) {
134 console.log(JSON.stringify(addresses))
135 })
136 ```
137
138### Registering an Observer for the Default Network
139
140* Example
141 ```javascript
142 import net_connection from "@ohos.net.connection"
143 ```
144 ```javascript
145 let netConnection = net_connection.createNetConnection()
146 netConnection.on('netAvailable', function(data) {
147 console.log(JSON.stringify(data))
148 })
149 netConnection.register(function (error) {
150 if (error) {
151 console.log(JSON.stringify(error))
152 }
153 })
154 ```
155
156## Repositories Involved
157
158Network Management Subsystem
159
160**communication_netmanager_base**
161
162[communication_netmanager_ext](https://gitee.com/openharmony/communication_netmanager_ext)
163
164[communication_netstack](https://gitee.com/openharmony/communication_netstack)
165
README_zh.md
1# Net Manager
2
3## 简介
4
5网络管理主要分为网络管理、策略管理、流量管理、网络共享、VPN管理以及以太网连接等模块,其中网络管理、策略管理、流量管理为基础服务,归档在netmanager_base仓,以太网连接、网络共享、VPN管理三个模块为可裁剪扩展模块,归档在netmanager_ext仓,netmanager_ext编译构建依赖netmanager_base库内容。如图1:网络管理架构图;
6
7**图 1** 网络管理架构图
8
9![net_manager_arch](figures/net_manager_arch.png)
10
11## 目录
12
13```
14foundation/communication/netmanager_base/
15├─figures # 架构图
16├─frameworks # 接口实现
17│ ├─js # JS接口
18│ └─native # native接口
19├─interfaces # 接口定义
20│ ├─innerkits # native接口
21│ └─kits # JS接口
22├─sa_profile # sa定义
23├─services # IPC服务端实现
24│ ├─common # 公用代码存放目录
25│ ├─etc # 进程配置文件目录
26│ ├─netconnmanager # 网络管理核心代码目录
27│ ├─netmanagernative # 网络子系统服务端代码
28│ ├─netpolicymanager # 策略管理核心代码目录
29│ ├─netstatsmanager # 流量管理核心代码目录
30│ ├─netsys_bpf_stats # bpfReader为service提供功能接口目录
31│ └─netsyscontroller # netsys客户端代码目录
32├─test # 测试代码
33│ ├─fuzztest # FUZZ测试目录
34│ ├─netconnmanager # 网络管理单元测试目录
35│ ├─netmanagernative # 网络子系统服务端单元测试目录
36│ ├─netpolicymanager # 策略管理单元测试目录
37│ ├─netstatsmanager # 流量统计单元测试目录
38│ └─netsys_bpf_stats # bpfReader单元测试目录
39└─utils # 公共功能
40 ├─common_utils # 共同库目录
41 ├─log # 日志实现目录
42 └─napi_utils # 公用NAPI代码目录
43```
44
45## 约束
46
47- 开发语言:C++ JS
48
49## 接口说明
50
51| 类型 | 接口 | 功能说明 |
52| ---- | ---- | ---- |
53| ohos.net.connection | function getDefaultNet(callback: AsyncCallback\<NetHandle>): void; |获取一个含有默认网络的netId的NetHandle对象,使用callback回调 |
54| ohos.net.connection | function getDefaultNet(): Promise\<NetHandle>; |获取一个含有默认网络的netId的NetHandle对象,使用Promise回调 |
55| ohos.net.connection | function getAllNets(callback: AsyncCallback\<Array\<NetHandle>>): void;| 获取所处于连接状态的网络的MetHandle对象列表,使用callback回调 |
56| ohos.net.connection | function getAllNets(): Promise\<Array\<NetHandle>>;| 获取所有处于连接状态的网络的NetHandle对象列表,使用Promise回调 |
57| ohos.net.connection | function getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void; |查询默认网络的链路信息,使用callback回调 |
58| ohos.net.connection | function getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>; |查询默认网络的链路信息,使用Promise回调 |
59| ohos.net.connection | function getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void; |查询默认网络的能力集信息,使用callback回调 |
60| ohos.net.connection | function getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>; |查询默认网络的能力集信息,使用Promise回调 |
61| ohos.net.connection | function hasDefaultNet(callback: AsyncCallback\<boolean>): void; |查询是否有默认网络,使用callback回调 |
62| ohos.net.connection | function hasDefaultNet(): Promise\<boolean>; |查询是否有默认网络,使用Promise回调 |
63| ohos.net.connection | function getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |使用对应网络解析域名,获取所有IP,使用callback回调 |
64| ohos.net.connection | function getAddressesByName(host: string): Promise\<Array\<NetAddress>>; |使用默认网络解析域名,获取所有IP,使用Promise回调 |
65| ohos.net.connection | function createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection; | 返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征,timeout是超时时间(单位是毫秒),netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络 |
66| ohos.net.connection | function enableAirplaneMode(callback: AsyncCallback\<void>): void; | 设置网络为飞行模式,使用callback回调 |
67| ohos.net.connection | function enableAirplaneMode(): Promise\<void>;|设置网络为飞行模式,使用Promise回调 |
68| ohos.net.connection | function disableAirplaneMode(callback: AsyncCallback\<void>): void;| 关闭网络飞行模式,使用callback回调 |
69| ohos.net.connection | function disableAirplaneMode(): Promise\<void>;| 关闭网络飞行模式,使用Promise回调 |
70| ohos.net.connection | function reportNetConnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| 向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。使用callback回调 |
71| ohos.net.connection | function reportNetConnected(netHandle: NetHandle): Promise\<void>;| 向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。使用Promise回调 |
72| ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| 向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。使用callback回调 |
73| ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle): Promise\<void>;| 向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。使用Promise回调 |
74| ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void; | 将TCPSocket或UDPSockett绑定到当前网络,使用callback回调 |
75| ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;| 将TCPSocket或UDPSockett绑定到当前网络,使用Promise回调 |
76| ohos.net.connection.NetHandle | getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |使用默认网络解析域名,获取所有IP,使用callback回调 |
77| ohos.net.connection.NetHandle | getAddressesByName(host: string): Promise\<Array\<NetAddress>>; |使用对应网络解析域名,获取所有IP,使用Promise回调 |
78| ohos.net.connection.NetHandle | getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void; |使用对应网络解析域名,获取一个IP,调用callbac |
79| ohos.net.connection.NetHandle | getAddressByName(host: string): Promise\<NetAddress>; |使用对应网络解析域名,获取一个IP,使用Promise回调 |
80| ohos.net.connection.NetConnection | on(type: 'netAvailable', callback: Callback\<NetHandle>): void; |监听收到网络可用的事件 |
81| ohos.net.connection.NetConnection | on(type: 'netCapabilitiesChange', callback: Callback\<{ netHandle: NetHandle, netCap: NetCapabilities }>): void; |监听网络能力变化的事件 |
82| ohos.net.connection.NetConnection | on(type: 'netConnectionPropertiesChange', callback: Callback\<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void; |监听网络连接信息变化的事件 |
83| ohos.net.connection.NetConnection | on(type: 'netLost', callback: Callback\<NetHandle>): void; |监听网络丢失的事件 |
84| ohos.net.connection.NetConnection | on(type: 'netUnavailable', callback: Callback\<void>): void; |监听网络不可用的事件 |
85| ohos.net.connection.NetConnection | register(callback: AsyncCallback\<void>): void; |注册默认网络或者createNetConnection中指定的网络的监听 |
86| ohos.net.connection.NetConnection | unregister(callback: AsyncCallback\<void>): void; |注销默认网络或者createNetConnection中指定的网络的监听 |
87| @ohos.net.policy | function setBackgroundPolicy(allow: boolean, callback: AsyncCallback\<void>): void; | 设置后台网络策略,callback为回调函数 |
88| @ohos.net.policy | function setBackgroundPolicy(allow: boolean): Promise\<void>; | 设置后台网络策略 |
89| @ohos.net.policy | function getBackgroundPolicy(callback: AsyncCallback\<NetBackgroundPolicy>): void; | 获取后台网络限制策略,callback为回调函数 |
90| @ohos.net.policy | function getBackgroundPolicy(): Promise\<NetBackgroundPolicy>; | 获取后台网络限制策略 |
91| @ohos.net.policy | function setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\<void>): void; | 设置对应uid应用的访问计量网络的策略,callback为回调函数 |
92| @ohos.net.policy | function setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>; | 设置对应uid应用的访问计量网络的策略 |
93| @ohos.net.policy | function getPolicyByUid(uid: number, callback: AsyncCallback\<NetUidPolicy>): void; | 通过应用uid获取策略,callback为回调函数 |
94| @ohos.net.policy | function getPolicyByUid(uid: number): Promise\<NetUidPolicy>; | 通过应用uid获取策略 |
95| @ohos.net.policy | function getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\<Array\<number>>): void; | 通过策略获取设置这一策略的应用uid数组,callback为回调函数 |
96| @ohos.net.policy | function getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>; | 通过策略获取设置这一策略的应用uid数组 |
97| @ohos.net.policy | function getNetQuotaPolicies(callback: AsyncCallback\<Array\<NetQuotaPolicy>>): void; | 获取计量网络策略,callback为回调函数 |
98| @ohos.net.policy | function getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>; | 获取计量网络策略 |
99| @ohos.net.policy | function setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>, callback: AsyncCallback\<void>): void; | 设置计量网络策略,callback为回调函数 |
100| @ohos.net.policy | function setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>): Promise\<void>; | 设置计量网络策略 |
101| @ohos.net.policy | function restoreAllPolicies(simId: string, callback: AsyncCallback\<void>): void; | 重置对应sim卡id的蜂窝网络、后台网络策略、防火墙策略、应用对应的策略,callback为回调函数 |
102| @ohos.net.policy | function restoreAllPolicies(simId: string): Promise\<void>; | 重置对应sim卡id的蜂窝网络、后台网络策略、防火墙策略、应用对应的策略 |
103| @ohos.net.policy | function isUidNetAllowedIsMetered(uid: number, isMetered: boolean, callback: AsyncCallback\<boolean>): void; | 获取对应uid能否访问计量或非计量网络,callback为回调函数 |
104| @ohos.net.policy | function isUidNetAllowedIsMetered(uid: number, isMetered: boolean): Promise\<boolean>; | 获取对应uid能否访问计量或非计量网络 |
105| @ohos.net.policy | function isUidNetAllowedIface(uid: number, iface: string, callback: AsyncCallback\<boolean>): void; | 获取对应uid能否访问指定的iface的网络,callback为回调函数 |
106| @ohos.net.policy | function isUidNetAllowedIface(uid: number, iface: string): Promise\<boolean>; | 获取对应uid能否访问指定的iface的网络 |
107| @ohos.net.policy | function setDeviceIdleAllowlist(uid: number, isAllow: boolean, callback: AsyncCallback\<void>): void | 设置指定uid能应用是否在休眠防火墙的白名单,callback为回调函数 |
108| @ohos.net.policy | function setDeviceIdleAllowlist(uid: number, isAllow: boolean): Promise\<void>; | 设置指定uid能应用是否在休眠防火墙的白名单 |
109| @ohos.net.policy | function getDeviceIdleAllowlist(callback: AsyncCallback\<Array\<number>>): void | 获取休眠模式白名单所包含的uid数组,callback为回调函数 |
110| @ohos.net.policy | function getDeviceIdleAllowlist(): Promise\<Array\<number>>; | 获取休眠模式白名单所包含的uid数组 |
111| @ohos.net.policy | function getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\<NetBackgroundPolicy>): void | 获取指定uid能否访问后台网络,callback为回调函数 |
112| @ohos.net.policy | function getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>; | 获取指定uid能否访问后台网络 |
113| @ohos.net.policy | function resetPolicies(simId: string, callback: AsyncCallback\<void>): void; | 重置对应sim卡id的蜂窝网络、后台网络策略、防火墙策略、应用对应的策略,callback为回调函数 |
114| @ohos.net.policy | function resetPolicies(simId: string): Promise\<void>; | 重置对应sim卡id的蜂窝网络、后台网络策略、防火墙策略、应用对应的策略 |
115| @ohos.net.policy | function updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType, callback: AsyncCallback\<void>): void | 更新提醒策略,netType为网络类型,simId为SIM卡id, remindType为提醒类型。callback为回调函数|
116| @ohos.net.policy | function updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType): Promise\<void>; | 更新提醒策略,netType为网络类型,simId为SIM卡id, remindType为提醒类型 |
117| @ohos.net.policy | function on(type: 'netUidPolicyChange', callback: Callback\<{ uid: number, policy: NetUidPolicy }>): void; | 注册policy发生改变时的回调 |
118| @ohos.net.policy | function off(type: 'netUidPolicyChange', callback: Callback\<void>): void; | 反注册policy发生改变时的回调 |
119| @ohos.net.policy | function on(type: "netUidRuleChange", callback: Callback\<{ uid: number, rule: NetUidRule }>): void; | 注册rule发生改变时的回调 |
120| @ohos.net.policy | function off(type: "netUidRuleChange", callback: Callback\<void>): void; | 反注册rule发生改变时的回调 |
121| @ohos.net.policy | function on(type: "netMeteredIfacesChange", callback: Callback\<Array\<string>>): void; | 注册计量iface发生改变时的回调 |
122| @ohos.net.policy | function off(type: "netMeteredIfacesChange", callback: Callback\<void>): void; | 反注册计量iface发生改变时的回调 |
123| @ohos.net.policy | function on(type: "netQuotaPolicyChange", callback: Callback\<Array\<NetQuotaPolicy>>): void; | 注册计量网络策略发生改变时的回调 |
124| @ohos.net.policy | function off(type: "netQuotaPolicyChange", callback: Callback\<void>): void; | 反注册计量网络策略发生改变时的回调 |
125| @ohos.net.policy | function on(type: "netBackgroundPolicyChange", callback: Callback\<boolean>): void; | 注册后台网络策略发生改变时的回调 |
126| @ohos.net.policy | function off(type: "netBackgroundPolicyChange", callback: Callback\<void>): void; | 反注册后台网络策略发生改变时的回调 |
127| ohos.net.statistics | function getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void; |查询指定网卡的下行流量数据,使用callback回调|
128| ohos.net.statistics | function getIfaceRxBytes(nic: string): Promise\<number>; |获取指定网卡的下行流量数据,使用Promise回调 |
129| ohos.net.statistics | function getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void; |查询指定网卡的上行流量数据,使用callback回调 |
130| ohos.net.statistics | function getIfaceRxBytes(nic: string): Promise\<number>; |获取指定网卡的上行流量数据,使用Promise回调 |
131| ohos.net.statistics | function getCellularRxBytes(callback: AsyncCallback\<number>): void; |查询指蜂窝网的下行流量数据,使用callback回调 |
132| ohos.net.statistics | function getCellularRxBytes(): Promise\<number>; |查询指蜂窝网的下行流量数据,使用Promise回调 |
133| ohos.net.statistics | function getCellularTxBytes(callback: AsyncCallback\<number>): void; |查询蜂窝网的上行流量数据,使用callback回调 |
134| ohos.net.statistics | function getCellularTxBytes(): Promise\<number>; |查询蜂窝网的上行流量数据,使用Promise回调 |
135| ohos.net.statistics | function getAllRxBytes(callback: AsyncCallback\<number>): void; |查询所有网卡的下行流量数据,使用callback回调 |
136| ohos.net.statistics | function getAllRxBytes(): Promise\<number>; |查询所有网卡的下行流量数据,使用Promise回调 |
137| ohos.net.statistics | function getAllTxBytes(callback: AsyncCallback\<number>): void; |查询所有网卡的上行流量数据,使用callback回调 |
138| ohos.net.statistics | function getAllTxBytes(): Promise\<number>; |查询所有网卡的上行流量数据,使用Promise回调 |
139| ohos.net.statistics | function getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void; |查询指定应用的下行流量数据,使用callback回调 |
140| ohos.net.statistics | function getUidRxBytes(uid: number): Promise\<number>; |查询指定应用的下行流量数据,使用Promise回调 |
141| ohos.net.statistics | function getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void; |查询指定应用的上行流量数据,使用callback回调 |
142| ohos.net.statistics | function getUidTxBytes(uid: number): Promise\<number>; |查询指定应用的上行流量数据,使用Promise回调 |
143
144完整的JS API说明以及实例代码请参考:[网络管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-net-connection.md)。
145
146## 接口使用说明
147
148### 注册默认网络或者createNetConnection中指定的网络的监听
149
150* 示例
151 ```javascript
152 import connection from '@ohos.net.connection'
153 ```
154 ```javascript
155 let connection = connection.createNetConnection()
156 connection.on('netAvailable', function(data) {
157 console.log(JSON.stringify(data))
158 })
159 connection.register(function (error) {
160 if (error) {
161 console.log(JSON.stringify(error))
162 }
163 })
164 ```
165
166### 获取所有注册的网络
167
168* 示例
169 ```javascript
170 import connection from '@ohos.net.connection'
171 ```
172 ```javascript
173 connection.getAllNets((err, data) => {
174 this.callBack(err, data);
175 if (data) {
176 this.netList = data;
177 }
178 })
179 ```
180
181### 查询默认网络的链路信息
182
183* 示例
184 ```javascript
185 import connection from '@ohos.net.connection'
186 ```
187 ```javascript
188 connection.getConnectionProperties(this.netHandle, (err, data) => {
189 this.callBack(err, data);
190 })
191 ```
192
193### 使用对应网络解析域名,获取所有IP
194
195* 示例
196 ```javascript
197 import connection from '@ohos.net.connection'
198 ```
199 ```javascript
200 connection.getAddressesByName(this.host, (err, data) => {
201 this.callBack(err, data);
202 })
203 ```
204
205### 设置后台网络策略
206
207* 示例
208
209 ```javascript
210 import policy from '@ohos.net.policy'
211 ```
212
213 ```javascript
214 policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean)), (err, data) => {
215 this.callBack(err, data);
216 })
217 ```
218
219### 获取后台网络限制策略
220
221* 示例
222
223 ```javascript
224 import policy from '@ohos.net.policy'
225 ```
226 ```javascript
227 policy.getBackgroundPolicy((err, data) => {
228 this.callBack(err, data);
229 })
230 ```
231
232### 设置对应uid应用的访问计量网络的策略
233
234* 示例
235
236 ```javascript
237 import policy from '@ohos.net.policy'
238 ```
239 ```javascript
240 policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy), (err, data) => {
241 this.callBack(err, data);
242 })
243 ```
244
245### 设置指定uid能应用是否在休眠防火墙的白名单
246
247* 示例
248
249 ```javascript
250 import policy from '@ohos.net.policy'
251 ```
252
253 ```javascript
254 policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (err, data) => {
255 this.callBack(err, data);
256 })
257
258### 查询指定网卡的下行流量数据,以Promise的方式异步返回执行结果。
259
260* 示例
261 ```javascript
262 import statistics from '@ohos.net.statistics'
263 ```
264 ```javascript
265 statistics.getIfaceRxBytes("wlan0").then(function (addresses) {
266 console.log(JSON.stringify(addresses))
267 })
268 ```
269
270### 查询指定网卡的上行流量数据,以Promise的方式异步返回执行结果。。
271
272* 示例
273 ```javascript
274 import statistics from '@ohos.net.statistics'
275 ```
276 ```javascript
277 statistics.getIfaceTxBytes("wlan0").then(function (addresses) {
278 console.log(JSON.stringify(addresses))
279 })
280 ```
281
282## 相关仓
283
284[网络管理子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
285
286**communication_netmanager_base**
287
288[communication_netmanager_ext](https://gitee.com/openharmony/communication_netmanager_ext)
289
290[communication_netstack](https://gitee.com/openharmony/communication_netstack)
291