1# @system.network (Network State) 2 3> **NOTE** 4> - The APIs of this module are no longer maintained since API version 7. You are advised to use [`@ohos.telephony.observer`](js-apis-observer.md). 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 API call is successful. The return value is defined by [NetworkResponse](#networkresponse).| 37| fail | Function | No| Called when API call has failed.| 38| complete | Function | No| Called when the API call is complete.| 39 40One of the following error codes will be returned if the API call has failed. 41 42| Error Code| Description| 43| -------- | -------- | 44| 602 | The current permission is not declared.| 45 46**Example** 47 48``` 49export default { 50 getType() { 51 network.getType({ 52 success: function(data) { 53 console.log('success get network type:' + data.type); 54 }, 55 fail: function(data, code) { 56 console.log('fail to get network type code:' + code + ', data:' + data); 57 }, 58 }); 59 }, 60} 61``` 62 63 64## network.subscribe 65 66subscribe(Object): void 67 68Listens to the network connection state. If this method is called multiple times, the last call takes effect. 69 70**System capability**: SystemCapability.Communication.NetManager.Core 71 72**Parameters** 73 74| Name| Type| Mandatory| Description| 75| -------- | -------- | -------- | -------- | 76| success | Function | No| Called when the network state changes. The return value is defined by [NetworkResponse](#networkresponse).| 77| fail | Function | No| Called when API call has failed.| 78 79One of the following error codes will be returned if the API call has failed. 80 81| Error Code| Description| 82| -------- | -------- | 83| 602 | The current permission is not declared.| 84| 200 | Subscription failed.| 85 86**Example** 87 88``` 89export default { 90 subscribe() { 91 network.subscribe({ 92 success: function(data) { 93 console.log('network type change type:' + data.type); 94 }, 95 fail: function(data, code) { 96 console.log('fail to subscribe network, code:' + code + ', data:' + data); 97 }, 98 }); 99 }, 100} 101``` 102 103 104## network.unsubscribe 105 106unsubscribe(): void 107 108Cancels listening to the network connection state. 109 110**System capability**: SystemCapability.Communication.NetManager.Core 111 112**Example** 113 114``` 115export default { 116 unsubscribe() { 117 network.unsubscribe(); 118 }, 119} 120``` 121 122 123## NetworkResponse 124 125**System capability**: SystemCapability.Communication.NetManager.Core 126 127| Name| Type| Mandatory| Description| 128| -------- | -------- | -------- | -------- | 129| metered | boolean | No|Whether to charge by traffic.| 130| type | string | Yes|Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**.| 131