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