js-apis-system-network.md 3.3 KB
Newer Older
S
shawn_he 已提交
1
# @system.network (Network State)
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
> **NOTE**
S
shawn_he 已提交
4 5
> - 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).
>
Z
zengyawen 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
> - 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.


## Modules to Import


```
import network from '@system.network';
```


## Required Permissions

ohos.permission.GET_WIFI_INFO

ohos.permission.GET_NETWORK_INFO


## network.getType

getType(Object): void

Obtains the network type.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
34
| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
35
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
36 37 38
| success | Function | No| Called when the API call is successful. The return value is defined by [NetworkResponse](#networkresponse).|
| fail | Function | No| Called when API call has failed.|
| complete | Function | No| Called when the API call is complete.|
Z
zengyawen 已提交
39

S
shawn_he 已提交
40
One of the following error codes will be returned if the API call has failed.
Z
zengyawen 已提交
41

S
shawn_he 已提交
42
| Error Code| Description|
Z
zengyawen 已提交
43
| -------- | -------- |
S
shawn_he 已提交
44
| 602 | The current permission is not declared.|
Z
zengyawen 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

**Example**

```
export default {    
  getType() {        
    network.getType({            
      success: function(data) {                
        console.log('success get network type:' + data.type);            
      },            
      fail: function(data, code) {                
        console.log('fail to get network type code:' + code + ', data:' + data);            
      },
    });    
  },
}
```


## network.subscribe

subscribe(Object): void

Listens to the network connection state. If this method is called multiple times, the last call takes effect.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
74
| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
75
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
76 77
| success | Function | No| Called when the network state changes. The return value is defined by [NetworkResponse](#networkresponse).|
| fail | Function | No| Called when API call has failed.|
Z
zengyawen 已提交
78

S
shawn_he 已提交
79
One of the following error codes will be returned if the API call has failed.
Z
zengyawen 已提交
80

S
shawn_he 已提交
81
| Error Code| Description|
Z
zengyawen 已提交
82
| -------- | -------- |
S
shawn_he 已提交
83 84
| 602 | The current permission is not declared.|
| 200 | Subscription failed.|
Z
zengyawen 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

**Example**

```
export default {    
  subscribe() {        
    network.subscribe({            
      success: function(data) {                
        console.log('network type change type:' + data.type);            
      },            
      fail: function(data, code) {                
        console.log('fail to subscribe network, code:' + code + ', data:' + data);            
      },
    });    
  },
}
```


## network.unsubscribe

unsubscribe(): void

Cancels listening to the network connection state.

**System capability**: SystemCapability.Communication.NetManager.Core

**Example**

```
export default {    
  unsubscribe() {        
    network.unsubscribe();    
  },
}
S
shawn_he 已提交
120 121
```

S
shawn_he 已提交
122

S
shawn_he 已提交
123 124
## NetworkResponse

S
shawn_he 已提交
125 126 127 128 129 130
**System capability**: SystemCapability.Communication.NetManager.Core

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| metered | boolean | No|Whether to charge by traffic.|
| type | string | Yes|Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**.|