1# @system.network (Network State)
2
3> **NOTE**
4> - 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.
5>
6> - The APIs of this module are no longer maintained since API version 8. You are advised to use ['@ohos.net.connection'](js-apis-net-connection.md).
7
8## Modules to Import
9
10
11```
12import network from '@system.network';
13```
14
15
16## Required Permissions
17
18ohos.permission.GET_WIFI_INFO
19
20ohos.permission.GET_NETWORK_INFO
21
22
23## network.getType<sup>3+</sup>
24
25getType(options?: {<br>
26&nbsp;&nbsp;success?: (data: NetworkResponse) => void;<br>
27&nbsp;&nbsp;fail?: (data: any, code: number) => void;<br>
28&nbsp;&nbsp;complete?: () => void;<br>
29}): void
30
31Obtains the network type of this device.
32
33**System capability**: SystemCapability.Communication.NetManager.Core
34
35**Parameters**
36
37| Name| Type| Mandatory| Description|
38| -------- | -------- | -------- | -------- |
39| success | Function | No| Called when the API call is successful. The return value is defined by [NetworkResponse](#networkresponse3).|
40| fail | Function | No| Called when an API call fails.|
41| complete | Function | No| Called when an API call is complete.|
42
43Error codes:
44
45| Error Code| Description|
46| -------- | -------- |
47| 602 | The current permission is not declared.|
48
49**Example**
50
51```
52export default class Network {
53  getType() {
54    network.getType({
55      success: (data) => {
56        console.log('success get network type:' + data.type);
57      }
58    });
59  }
60}
61```
62
63
64## network.subscribe<sup>3+</sup>
65
66subscribe(options?:{<br>
67&nbsp;&nbsp;success?: (data: NetworkResponse) => void;<br>
68&nbsp;&nbsp;fail?: (data: any, code: number) => void;<br>
69  }): void
70
71Listens to the network connection state of this device. If this API is called multiple times, the last call takes effect.
72
73**System capability**: SystemCapability.Communication.NetManager.Core
74
75**Parameters**
76
77| Name| Type| Mandatory| Description|
78| -------- | -------- | -------- | -------- |
79| success | Function | No| Called when the network state changes. The return value is defined by [NetworkResponse](#networkresponse3).|
80| fail | Function | No| Called when an API call fails.|
81
82Error codes:
83
84| Error Code| Description|
85| -------- | -------- |
86| 602 | The current permission is not declared.|
87| 200 | Subscription failed.|
88
89**Example**
90
91```
92export default class Network {
93  subscribe() {
94    network.subscribe({
95      success: (data) => {
96        console.log('success get network type:' + data.type);
97      }
98    });
99  }
100}
101```
102
103
104## network.unsubscribe<sup>3+</sup>
105
106unsubscribe(): void
107
108Cancels listening to the network connection state of this device.
109
110**System capability**: SystemCapability.Communication.NetManager.Core
111
112**Example**
113
114```
115import network from '@system.network';
116
117network.unsubscribe();
118```
119
120
121## NetworkResponse<sup>3+</sup>
122
123**System capability**: SystemCapability.Communication.NetManager.Core
124
125| Name| Type| Mandatory| Description|
126| -------- | -------- | -------- | -------- |
127| metered | boolean | No|Whether to charge by traffic.|
128| type | string | Yes|Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**.|
129