• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Network State
2
3> **NOTE**
4> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.telephony.observer`](js-apis-observer.md) instead.
5>
6> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7
8
9## Modules to Import
10
11
12```
13import network from '@system.network';
14```
15
16
17## Required Permissions
18
19ohos.permission.GET_WIFI_INFO
20
21ohos.permission.GET_NETWORK_INFO
22
23
24## network.getType
25
26getType(Object): void
27
28Obtains the network type.
29
30**System capability**: SystemCapability.Communication.NetManager.Core
31
32**Parameters**
33
34| Name | Type | Mandatory | Description |
35| -------- | -------- | -------- | -------- |
36| success | Function | No | Called when the execution is successful. |
37| fail | Function | No | Called when the operation fails. |
38| complete | Function | No | Called when the execution is complete |
39
40The following value will be returned when the multimedia volume is obtained.
41
42| Parameter | Type | Description |
43| -------- | -------- | -------- |
44| metered | boolean | Whether the billing is based on the data volume. |
45| type | string | Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**. |
46
47One of the following error codes will be returned if the operation fails.
48
49| Error Code | Description |
50| -------- | -------- |
51| 602 | The current permission is not declared. |
52
53**Example**
54
55```
56export default {
57  getType() {
58    network.getType({
59      success: function(data) {
60        console.log('success get network type:' + data.type);
61      },
62      fail: function(data, code) {
63        console.log('fail to get network type code:' + code + ', data:' + data);
64      },
65    });
66  },
67}
68```
69
70
71## network.subscribe
72
73subscribe(Object): void
74
75Listens to the network connection state. If this method is called multiple times, the last call takes effect.
76
77**System capability**: SystemCapability.Communication.NetManager.Core
78
79**Parameters**
80
81| Name | Type | Mandatory | Description |
82| -------- | -------- | -------- | -------- |
83| success | Function | No | Called when the network connection state changes |
84| fail | Function | No | Called when the multimedia volume fails to be obtained. |
85
86The following value will be returned when the multimedia volume is obtained.
87
88| Parameter | Type | Description |
89| -------- | -------- | -------- |
90| metered | boolean | Whether the billing is based on the data volume. |
91| type | string | Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**. |
92
93One of the following error codes will be returned if the listening fails.
94
95| Error Code | Description |
96| -------- | -------- |
97| 602 | The current permission is not declared. |
98| 200 | The subscription fails. |
99
100**Example**
101
102```
103export default {
104  subscribe() {
105    network.subscribe({
106      success: function(data) {
107        console.log('network type change type:' + data.type);
108      },
109      fail: function(data, code) {
110        console.log('fail to subscribe network, code:' + code + ', data:' + data);
111      },
112    });
113  },
114}
115```
116
117
118## network.unsubscribe
119
120unsubscribe(): void
121
122Cancels listening to the network connection state.
123
124**System capability**: SystemCapability.Communication.NetManager.Core
125
126**Example**
127
128```
129export default {
130  unsubscribe() {
131    network.unsubscribe();
132  },
133}
134```