js-apis-telephony-data.md 10.6 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# Cellular Data

>**NOTE**
>
>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```
import data from '@ohos.telephony.data';
```

S
shawn_he 已提交
13
## data.getDefaultCellularDataSlotId
S
shawn_he 已提交
14 15 16

getDefaultCellularDataSlotId(callback: AsyncCallback\<number\>): void 

S
shawn_he 已提交
17
Obtains the default SIM card used for mobile data. This API uses an asynchronous callback to return the result. 
S
shawn_he 已提交
18 19 20 21 22 23 24

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

S
shawn_he 已提交
25
| Name  | Type                   | Mandatory| Description                                      |
S
shawn_he 已提交
26
| -------- | ----------------------- | ---- | ------------------------------------------ |
S
shawn_he 已提交
27
| callback | AsyncCallback\<number\> | Yes  | Callback used to return the result.<br>**0**: card slot 1<br>**1**: card slot 2|
S
shawn_he 已提交
28 29 30 31 32 33 34 35 36

**Example**

```
data.getDefaultCellularDataSlotId((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

S
shawn_he 已提交
37
## data.getDefaultCellularDataSlotId
S
shawn_he 已提交
38 39 40

getDefaultCellularDataSlotId(): Promise\<number\> 

S
shawn_he 已提交
41
Obtains the default SIM card used for mobile data. This API uses a promise to return the result. 
S
shawn_he 已提交
42 43 44 45 46

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

S
shawn_he 已提交
47
**Return Value**
S
shawn_he 已提交
48

S
shawn_he 已提交
49
| Type             | Description                                                        |
S
shawn_he 已提交
50
| ----------------- | ------------------------------------------------------------ |
S
shawn_he 已提交
51
| Promise\<number\> | Promise used to return the result.<br>**0**: card slot 1<br>**1**: card slot 2|
S
shawn_he 已提交
52 53 54 55 56 57 58 59 60 61 62 63

**Example**

```
let promise = data.getDefaultCellularDataSlotId();
promise.then((data) => {
    console.log(`test success, promise: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`test fail, promise: err->${JSON.stringify(err)}`);
});
```

S
shawn_he 已提交
64
## data.getCellularDataFlowType
S
shawn_he 已提交
65 66 67

getCellularDataFlowType(callback: AsyncCallback\<DataFlowType\>): void

S
shawn_he 已提交
68
Obtains the cellular data flow type, which can be uplink or downlink. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
69 70 71 72 73

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

S
shawn_he 已提交
74
| Name  | Type                                          | Mandatory| Description      |
S
shawn_he 已提交
75
| -------- | ---------------------------------------------- | ---- | ---------- |
S
shawn_he 已提交
76
| callback | AsyncCallback\<[DataFlowType](#dataflowtype)\> | Yes  | Callback used to return the result.|
S
shawn_he 已提交
77 78 79 80 81 82 83 84 85

**Example**

```
data.getCellularDataFlowType((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

S
shawn_he 已提交
86
## data.getCellularDataFlowType
S
shawn_he 已提交
87 88 89

getCellularDataFlowType(): Promise\<DataFlowType\>

S
shawn_he 已提交
90
Obtains the cellular data flow type, which can be uplink or downlink. This API uses a promise to return the result.
S
shawn_he 已提交
91 92 93

**System capability**: SystemCapability.Telephony.CellularData

S
shawn_he 已提交
94
**Return Value**
S
shawn_he 已提交
95

S
shawn_he 已提交
96
| Type                                    | Description                                           |
S
shawn_he 已提交
97
| ---------------------------------------- | ----------------------------------------------- |
S
shawn_he 已提交
98
| Promise\<[DataFlowType](#dataflowtype)\> | Promise used to return the result. |
S
shawn_he 已提交
99 100 101 102 103 104 105 106 107 108 109 110

**Example**

```
let promise = data.getCellularDataFlowType();
promise.then((data) => {
    console.log(`test success, promise: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`test fail, promise: err->${JSON.stringify(err)}`);
});
```

S
shawn_he 已提交
111
## data.getCellularDataState
S
shawn_he 已提交
112 113 114

getCellularDataState(callback: AsyncCallback\<DataConnectState\>): void

S
shawn_he 已提交
115
Obtains the connection status of the packet switched (PS) domain. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
116 117 118 119 120

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

S
shawn_he 已提交
121
| Name  | Type                                                  | Mandatory| Description      |
S
shawn_he 已提交
122
| -------- | ------------------------------------------------------ | ---- | ---------- |
S
shawn_he 已提交
123
| callback | AsyncCallback\<[DataConnectState](#dataconnectstate)\> | Yes  | Callback used to return the result.|
S
shawn_he 已提交
124 125 126 127 128 129 130 131 132

**Example**

```
data.getCellularDataState((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

S
shawn_he 已提交
133
## data.getCellularDataState
S
shawn_he 已提交
134 135 136

getCellularDataState(): Promise\<DataConnectState\>

S
shawn_he 已提交
137
Obtains the connection status of the PS domain. This API uses a promise to return the result.
S
shawn_he 已提交
138 139 140

**System capability**: SystemCapability.Telephony.CellularData

S
shawn_he 已提交
141
**Return Value**
S
shawn_he 已提交
142

S
shawn_he 已提交
143
| Type                                            | Description                                 |
S
shawn_he 已提交
144
| ------------------------------------------------ | ------------------------------------- |
S
shawn_he 已提交
145
| Promise\<[DataConnectState](#dataconnectstate)\> | Promise used to return the result.|
S
shawn_he 已提交
146 147 148 149 150 151 152 153 154 155 156 157

**Example**

```
let promise = data.getCellularDataState();
promise.then((data) => {
    console.log(`test success, promise: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`test fail, promise: err->${JSON.stringify(err)}`);
});
```

S
shawn_he 已提交
158
## data.isCellularDataEnabled
S
shawn_he 已提交
159 160 161

isCellularDataEnabled(callback: AsyncCallback\<boolean\>): void

S
shawn_he 已提交
162
Checks whether the cellular data service is enabled. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
163 164 165 166 167 168 169

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

S
shawn_he 已提交
170
| Name  | Type                    | Mandatory| Description                                                        |
S
shawn_he 已提交
171
| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
172
| callback | AsyncCallback\<boolean\> | Yes  | Callback used to return the result.<br>**true**: The cellular data service is enabled.<br>**false**: The cellular data service is disabled.|
S
shawn_he 已提交
173 174 175 176 177 178 179 180 181

**Example**

```
data.isCellularDataEnabled((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

S
shawn_he 已提交
182
## data.isCellularDataEnabled
S
shawn_he 已提交
183 184 185

isCellularDataEnabled(): Promise\<boolean\>

S
shawn_he 已提交
186
Checks whether the cellular data service is enabled. This API uses a promise to return the result.
S
shawn_he 已提交
187 188 189 190 191

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

S
shawn_he 已提交
192
**Return Value**
S
shawn_he 已提交
193

S
shawn_he 已提交
194
| Type              | Description                                                        |
S
shawn_he 已提交
195
| ------------------ | ------------------------------------------------------------ |
S
shawn_he 已提交
196
| Promise\<boolean\> | Promise used to return the result.<br>**true**: The cellular data service is enabled.<br>**false**: The cellular data service is disabled.|
S
shawn_he 已提交
197 198 199 200 201 202 203 204 205 206 207 208

**Example**

```
let promise = data.isCellularDataEnabled();
promise.then((data) => {
    console.log(`test success, promise: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`test fail, promise: err->${JSON.stringify(err)}`);
});
```

S
shawn_he 已提交
209
## data.isCellularDataRoamingEnabled
S
shawn_he 已提交
210 211 212

isCellularDataRoamingEnabled(slotId: number, callback: AsyncCallback\<boolean\>): void

S
shawn_he 已提交
213
Checks whether roaming is enabled for the cellular data service. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
214 215 216 217 218 219 220

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

S
shawn_he 已提交
221
| Name  | Type                    | Mandatory| Description                                                        |
S
shawn_he 已提交
222
| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
223 224
| slotId   | number                   | Yes  | Card slot ID. <br>**0**: card slot 1<br>**1**: card slot 2                    |
| callback | AsyncCallback\<boolean\> | Yes  | Callback used to return the result.<br>**true**: Roaming is enabled for the cellular data service.<br>**false**: Roaming is disabled for the cellular data service.|
S
shawn_he 已提交
225 226 227 228 229 230 231 232 233

**Example**

```
data.isCellularDataRoamingEnabled(0,(err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

S
shawn_he 已提交
234
## data.isCellularDataRoamingEnabled
S
shawn_he 已提交
235 236 237

isCellularDataRoamingEnabled(slotId: number): Promise\<boolean\>

S
shawn_he 已提交
238
Checks whether roaming is enabled for the cellular data service. This API uses a promise to return the result.
S
shawn_he 已提交
239 240 241 242 243 244 245

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

S
shawn_he 已提交
246
| Name| Type  | Mandatory| Description                                    |
S
shawn_he 已提交
247
| ------ | ------ | ---- | ---------------------------------------- |
S
shawn_he 已提交
248
| slotId | number | Yes  | Card slot ID. <br>**0**: card slot 1<br>**1**: card slot 2|
S
shawn_he 已提交
249

S
shawn_he 已提交
250
**Return Value**
S
shawn_he 已提交
251

S
shawn_he 已提交
252
| Type              | Description                                                        |
S
shawn_he 已提交
253
| ------------------ | ------------------------------------------------------------ |
S
shawn_he 已提交
254
| Promise\<boolean\> | Promise used to return the result.<br>**true**: Roaming is enabled for the cellular data service.<br>**false**: Roaming is disabled for the cellular data service.|
S
shawn_he 已提交
255 256 257 258 259 260 261 262 263 264 265 266

**Example**

```
let promise = data.isCellularDataRoamingEnabled(0);
promise.then((data) => {
    console.log(`test success, promise: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`test fail, promise: err->${JSON.stringify(err)}`);
});
```

S
shawn_he 已提交
267
## DataFlowType
S
shawn_he 已提交
268

S
shawn_he 已提交
269
Defines the cellular data flow type.
S
shawn_he 已提交
270

S
shawn_he 已提交
271 272 273 274 275 276 277 278 279
**System capability**: SystemCapability.Telephony.CellularData

| Name                  | Value  | Description                                      |
| ---------------------- | ---- | ------------------------------------------ |
| DATA_FLOW_TYPE_NONE    | 0    | No uplink or downlink data is available.                  |
| DATA_FLOW_TYPE_DOWN    | 1    | Only the downlink data is available.                        |
| DATA_FLOW_TYPE_UP      | 2    | Only the uplink data is available.                        |
| DATA_FLOW_TYPE_UP_DOWN | 3    | Both uplink data and downlink data are available.                        |
| DATA_FLOW_TYPE_DORMANT | 4    | No uplink or downlink data is available because the lower-layer link is in the dormant state.|
S
shawn_he 已提交
280

S
shawn_he 已提交
281
## DataConnectState
S
shawn_he 已提交
282

S
shawn_he 已提交
283 284 285
Describes the connection status of a cellular data link.

**System capability**: SystemCapability.Telephony.CellularData
S
shawn_he 已提交
286

S
shawn_he 已提交
287 288 289 290 291 292 293
| Name                   | Value  | Description                      |
| ----------------------- | ---- | -------------------------- |
| DATA_STATE_UNKNOWN     | -1   | The status of the cellular data link is unknown.    |
| DATA_STATE_DISCONNECTED | 0    | The cellular data link is disconnected.    |
| DATA_STATE_CONNECTING   | 1    | The cellular data link is being connected.|
| DATA_STATE_CONNECTED    | 2    | The cellular data link is connected.  |
| DATA_STATE_SUSPENDED    | 3    | The cellular data link is suspended.  |
反馈
建议
客服 返回
顶部