1# @ohos.telephony.data (Cellular Data)
2
3The **data** module provides basic mobile data management functions. You can obtain the default slot of the SIM card used for mobile data, and obtain the uplink and downlink connection status of cellular data services and connection status of the packet switched (PS) domain. Besides, you can check whether cellular data services and data roaming are enabled.
4
5>**NOTE**
6>
7>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.
8
9## Modules to Import
10
11```ts
12import { data } from '@kit.TelephonyKit';
13```
14
15## data.getDefaultCellularDataSlotId
16
17getDefaultCellularDataSlotId(callback: AsyncCallback\<number\>): void
18
19Obtains the default slot of the SIM card used for mobile data. This API uses an asynchronous callback to return the result.
20
21**System capability**: SystemCapability.Telephony.CellularData
22
23**Parameters**
24
25| Name  | Type                   | Mandatory| Description                                      |
26| -------- | ----------------------- | ---- | ------------------------------------------ |
27| callback | AsyncCallback\<number\> | Yes  | Callback used to return the result.<br>**0**: card slot 1.<br>**1**: card slot 2.|
28
29**Example**
30
31```ts
32import { data } from '@kit.TelephonyKit';
33import { BusinessError } from '@kit.BasicServicesKit';
34
35data.getDefaultCellularDataSlotId((err: BusinessError, contextData: number) => {
36    if(err){
37        console.error(`getDefaultCellularDataSlotId fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
38    }else{
39        console.log(`getDefaultCellularDataSlotId success`);
40    }
41});
42```
43
44## data.getDefaultCellularDataSlotId
45
46getDefaultCellularDataSlotId(): Promise\<number\>
47
48Obtains the default slot of the SIM card used for mobile data. This API uses a promise to return the result.
49
50**System capability**: SystemCapability.Telephony.CellularData
51
52**Return value**
53
54| Type             | Description                                                        |
55| ----------------- | ------------------------------------------------------------ |
56| Promise\<number\> | Promise used to return the result.<br>**0**: card slot 1.<br>**1**: card slot 2.|
57
58**Example**
59
60```ts
61import { data } from '@kit.TelephonyKit';
62import { BusinessError } from '@kit.BasicServicesKit';
63
64data.getDefaultCellularDataSlotId().then((contextData: number) => {
65    console.log(`getDefaultCellularDataSlotId success, promise: contextData->${JSON.stringify(contextData)}`);
66}).catch((err: BusinessError) => {
67    console.error(`getDefaultCellularDataSlotId fail, promise: err->${JSON.stringify(err)}`);
68});
69```
70
71## data.getDefaultCellularDataSlotIdSync<sup>9+</sup>
72
73getDefaultCellularDataSlotIdSync(): number
74
75Obtains the default SIM card used for mobile data synchronously.
76
77**System capability**: SystemCapability.Telephony.CellularData
78
79**Return value**
80
81| Type             | Description                                                        |
82| ------ | -------------------------------------------------- |
83| number | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
84
85**Example**
86
87```ts
88import { data } from '@kit.TelephonyKit';
89
90console.log("Result: "+ data.getDefaultCellularDataSlotIdSync())
91```
92
93
94## data.getCellularDataFlowType
95
96getCellularDataFlowType(callback: AsyncCallback\<DataFlowType\>): void
97
98Obtains the cellular data flow type, which can be uplink or downlink. This API uses an asynchronous callback to return the result.
99
100**System capability**: SystemCapability.Telephony.CellularData
101
102**Parameters**
103
104| Name  | Type                                          | Mandatory| Description      |
105| -------- | ---------------------------------------------- | ---- | ---------- |
106| callback | AsyncCallback\<[DataFlowType](#dataflowtype)\> | Yes  | Callback used to return the result.|
107
108**Example**
109
110```ts
111import { data } from '@kit.TelephonyKit';
112import { BusinessError } from '@kit.BasicServicesKit';
113
114data.getCellularDataFlowType((err: BusinessError, contextData: data.DataFlowType) => {
115    if(err){
116        console.error(`getCellularDataFlowType fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
117    }else{
118        console.log(`getCellularDataFlowType success`);
119    }
120});
121```
122
123## data.getCellularDataFlowType
124
125getCellularDataFlowType(): Promise\<DataFlowType\>
126
127Obtains the cellular data flow type, which can be uplink or downlink. This API uses a promise to return the result.
128
129**System capability**: SystemCapability.Telephony.CellularData
130
131**Return value**
132
133| Type                                    | Description                                           |
134| ---------------------------------------- | ----------------------------------------------- |
135| Promise\<[DataFlowType](#dataflowtype)\> | Promise used to return the result. |
136
137**Example**
138
139```ts
140import { data } from '@kit.TelephonyKit';
141import { BusinessError } from '@kit.BasicServicesKit';
142
143data.getCellularDataFlowType().then((contextData: data.DataFlowType) => {
144    console.log(`getCellularDataFlowType success, promise: contextData->${JSON.stringify(contextData)}`);
145}).catch((err: BusinessError) => {
146    console.error(`getCellularDataFlowType fail, promise: err->${JSON.stringify(err)}`);
147});
148```
149
150## data.getCellularDataState
151
152getCellularDataState(callback: AsyncCallback\<DataConnectState\>): void
153
154Obtains the connection status of the packet switched (PS) domain. This API uses an asynchronous callback to return the result.
155
156**System capability**: SystemCapability.Telephony.CellularData
157
158**Parameters**
159
160| Name  | Type                                                  | Mandatory| Description      |
161| -------- | ------------------------------------------------------ | ---- | ---------- |
162| callback | AsyncCallback\<[DataConnectState](#dataconnectstate)\> | Yes  | Callback used to return the result.|
163
164**Example**
165
166```ts
167import { data } from '@kit.TelephonyKit';
168import { BusinessError } from '@kit.BasicServicesKit';
169
170data.getCellularDataState((err: BusinessError, contextData: data.DataConnectState) => {
171    if(err){
172        console.error(`getCellularDataState fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
173    }else{
174        console.log(`getCellularDataState success`);
175    }
176});
177```
178
179## data.getCellularDataState
180
181getCellularDataState(): Promise\<DataConnectState\>
182
183Obtains the connection status of the PS domain. This API uses a promise to return the result.
184
185**System capability**: SystemCapability.Telephony.CellularData
186
187**Return value**
188
189| Type                                            | Description                                 |
190| ------------------------------------------------ | ------------------------------------- |
191| Promise\<[DataConnectState](#dataconnectstate)\> | Promise used to return the result.|
192
193**Example**
194
195```ts
196import { data } from '@kit.TelephonyKit';
197import { BusinessError } from '@kit.BasicServicesKit';
198
199data.getCellularDataState().then((contextData: data.DataConnectState) => {
200    console.log(`getCellularDataState success, promise: contextData->${JSON.stringify(contextData)}`);
201}).catch((err: BusinessError) => {
202    console.error(`getCellularDataState fail, promise: err->${JSON.stringify(err)}`);
203});
204```
205
206## data.isCellularDataEnabled
207
208isCellularDataEnabled(callback: AsyncCallback\<boolean\>): void
209
210Checks whether the cellular data service is enabled. This API uses an asynchronous callback to return the result.
211
212**Required permission**: ohos.permission.GET_NETWORK_INFO
213
214**System capability**: SystemCapability.Telephony.CellularData
215
216**Parameters**
217
218| Name  | Type                    | Mandatory| Description                                                        |
219| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
220| 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.|
221
222**Error codes**
223
224For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
225
226| ID|                 Error Message                    |
227| -------- | -------------------------------------------- |
228| 201      | Permission denied.                           |
229| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
230| 8300001  | Invalid parameter value.                     |
231| 8300002  | Service connection failed.                   |
232| 8300003  | System internal error.                       |
233| 8300999  | Unknown error.                               |
234
235**Example**
236
237```ts
238import { data } from '@kit.TelephonyKit';
239import { BusinessError } from '@kit.BasicServicesKit';
240
241data.isCellularDataEnabled((err: BusinessError, contextData: boolean) => {
242    if(err){
243        console.error(`isCellularDataEnabled fail,callback: callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
244    }else{
245        console.log(`isCellularDataEnabled success`);
246    }
247});
248```
249
250## data.isCellularDataEnabled
251
252isCellularDataEnabled(): Promise\<boolean\>
253
254Checks whether the cellular data service is enabled. This API uses a promise to return the result.
255
256**Required permission**: ohos.permission.GET_NETWORK_INFO
257
258**System capability**: SystemCapability.Telephony.CellularData
259
260**Return value**
261
262| Type              | Description                                                        |
263| ------------------ | ------------------------------------------------------------ |
264| Promise\<boolean\> | Promise used to return the result.<br>**true**: The cellular data service is enabled.<br>**false**: The cellular data service is disabled.|
265
266**Error codes**
267
268For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
269
270| ID|                 Error Message                    |
271| -------- | -------------------------------------------- |
272| 201      | Permission denied.                           |
273| 8300002  | Service connection failed.                   |
274| 8300003  | System internal error.                       |
275| 8300999  | Unknown error.                               |
276
277**Example**
278
279```ts
280import { data } from '@kit.TelephonyKit';
281import { BusinessError } from '@kit.BasicServicesKit';
282
283data.isCellularDataEnabled().then((contextData: boolean) => {
284    console.log(`isCellularDataEnabled success, promise: contextData->${JSON.stringify(contextData)}`);
285}).catch((err: BusinessError) => {
286    console.error(`isCellularDataEnabled fail, promise: err->${JSON.stringify(err)}`);
287});
288```
289
290## data.isCellularDataEnabledSync<sup>12+</sup>
291
292isCellularDataEnabledSync(): boolean
293
294Checks whether the cellular data service is enabled. This API returns the result synchronously.
295
296**Required permission**: ohos.permission.GET_NETWORK_INFO
297
298**System capability**: SystemCapability.Telephony.CellularData
299
300**Return value**
301
302| Type   | Description                                                        |
303| ------- | ------------------------------------------------------------ |
304| boolean | Whether the cellular data service is enabled.<br>**true**: The cellular data service is enabled.<br>**false**: The cellular data service is disabled.|
305
306**Error codes**
307
308For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
309
310| ID| Error Message                                    |
311| -------- | -------------------------------------------- |
312| 201      | Permission denied.                           |
313| 8300002  | Operation failed. Cannot connect to service. |
314| 8300003  | System internal error.                       |
315| 8300999  | Unknown error code.                          |
316
317**Example**
318
319<!--code_no_check-->
320
321```ts
322import { data } from '@kit.TelephonyKit';
323
324try {
325    let isEnabled: boolean = data.isCellularDataEnabledSync();
326    console.log(`isCellularDataEnabledSync success : ${isEnabled}`);
327} catch (error) {
328    console.error(`isCellularDataEnabledSync fail : err->${JSON.stringify(error)}`);
329}
330```
331
332## data.isCellularDataRoamingEnabled
333
334isCellularDataRoamingEnabled(slotId: number, callback: AsyncCallback\<boolean\>): void
335
336Checks whether roaming is enabled for the cellular data service. This API uses an asynchronous callback to return the result.
337
338**Required permission**: ohos.permission.GET_NETWORK_INFO
339
340**System capability**: SystemCapability.Telephony.CellularData
341
342**Parameters**
343
344| Name  | Type                    | Mandatory| Description                                                        |
345| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
346| slotId   | number                   | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.                    |
347| 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.|
348
349**Error codes**
350
351For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
352
353| ID|                  Error Message                   |
354| -------- | -------------------------------------------- |
355| 201      | Permission denied.                           |
356| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
357| 8300001  | Invalid parameter value.                     |
358| 8300002  | Service connection failed.                   |
359| 8300003  | System internal error.                       |
360| 8300999  | Unknown error.                               |
361
362**Example**
363
364```ts
365import { data } from '@kit.TelephonyKit';
366import { BusinessError } from '@kit.BasicServicesKit';
367
368data.isCellularDataRoamingEnabled(0, (err: BusinessError, contextData: boolean) => {
369    if(err){
370        console.error(`isCellularDataRoamingEnabled fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
371    }else{
372        console.log(`isCellularDataRoamingEnabled success`);
373    }
374});
375```
376
377## data.isCellularDataRoamingEnabled
378
379isCellularDataRoamingEnabled(slotId: number): Promise\<boolean\>
380
381Checks whether roaming is enabled for the cellular data service. This API uses a promise to return the result.
382
383**Required permission**: ohos.permission.GET_NETWORK_INFO
384
385**System capability**: SystemCapability.Telephony.CellularData
386
387**Parameters**
388
389| Name| Type  | Mandatory| Description                                    |
390| ------ | ------ | ---- | ---------------------------------------- |
391| slotId | number | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
392
393**Return value**
394
395| Type              | Description                                                        |
396| ------------------ | ------------------------------------------------------------ |
397| 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.|
398
399**Error codes**
400
401For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
402
403| ID|                  Error Message                   |
404| -------- | -------------------------------------------- |
405| 201      | Permission denied.                           |
406| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
407| 8300001  | Invalid parameter value.                     |
408| 8300002  | Service connection failed.                   |
409| 8300003  | System internal error.                       |
410| 8300999  | Unknown error.                               |
411
412**Example**
413
414```ts
415import { data } from '@kit.TelephonyKit';
416import { BusinessError } from '@kit.BasicServicesKit';
417
418data.isCellularDataRoamingEnabled(0).then((contextData: boolean) => {
419    console.log(`isCellularDataRoamingEnabled success, promise: contextData->${JSON.stringify(contextData)}`);
420}).catch((err: BusinessError) => {
421    console.error(`isCellularDataRoamingEnabled fail, promise: err->${JSON.stringify(err)}`);
422});
423```
424
425## data.isCellularDataRoamingEnabledSync<sup>12+</sup>
426
427isCellularDataRoamingEnabledSync(slotId: number): boolean
428
429Checks whether roaming is enabled for the cellular data service. This API returns the result synchronously.
430
431**Required permission**: ohos.permission.GET_NETWORK_INFO
432
433**System capability**: SystemCapability.Telephony.CellularData
434
435**Parameters**
436
437| Name| Type  | Mandatory| Description                                    |
438| ------ | ------ | ---- | ---------------------------------------- |
439| slotId | number | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
440
441**Return value**
442
443| Type   | Description                                                        |
444| ------- | ------------------------------------------------------------ |
445| boolean | Whether roaming is enabled for the cellular data service.<br>**true**: Roaming is enabled for the cellular data service.<br>**false**: Roaming is disabled for the cellular data service.|
446
447**Error codes**
448
449For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
450
451| ID| Error Message                                                    |
452| -------- | ------------------------------------------------------------ |
453| 201      | Permission denied.                                           |
454| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; |
455| 8300001  | Invalid parameter value.                                     |
456| 8300002  | Operation failed. Cannot connect to service.                 |
457| 8300003  | System internal error.                                       |
458| 8300999  | Unknown error code.                                          |
459
460**Example**
461
462<!--code_no_check-->
463
464```ts
465import { data } from '@kit.TelephonyKit';
466
467try {
468    let isEnabled: boolean = data.isCellularDataRoamingEnabledSync(0);
469    console.log(`isCellularDataRoamingEnabledSync success : ${isEnabled}`);
470} catch (error) {
471    console.error(`isCellularDataRoamingEnabledSync fail : err->${JSON.stringify(error)}`);
472}
473```
474
475## data.getDefaultCellularDataSimId<sup>10+</sup>
476
477getDefaultCellularDataSimId(): number
478
479Obtains the default ID of the SIM card used for mobile data.
480
481**System capability**: SystemCapability.Telephony.CellularData
482
483**Return value**
484
485| Type             | Description                                                        |
486| ------ | -------------------------------------------------- |
487| number | Obtains the default ID of the SIM card used for mobile data.<br>The return value is bound to the SIM card and increases from 1.|
488
489**Example**
490
491```ts
492import { data } from '@kit.TelephonyKit';
493
494console.log("Result: "+ data.getDefaultCellularDataSimId());
495```
496
497## DataFlowType
498
499Defines the cellular data flow type.
500
501**System capability**: SystemCapability.Telephony.CellularData
502
503| Name                  | Value  | Description                                      |
504| ---------------------- | ---- | ------------------------------------------ |
505| DATA_FLOW_TYPE_NONE    | 0    | No uplink or downlink data is available.                  |
506| DATA_FLOW_TYPE_DOWN    | 1    | Only the downlink data is available.                        |
507| DATA_FLOW_TYPE_UP      | 2    | Only the uplink data is available.                        |
508| DATA_FLOW_TYPE_UP_DOWN | 3    | Both the uplink data and downlink data are available.                        |
509| DATA_FLOW_TYPE_DORMANT | 4    | No uplink or downlink data is available because the lower-layer link is in the dormant state.|
510
511## DataConnectState
512
513Describes the connection status of a cellular data link.
514
515**System capability**: SystemCapability.Telephony.CellularData
516
517| Name                   | Value  | Description                      |
518| ----------------------- | ---- | -------------------------- |
519| DATA_STATE_UNKNOWN      | -1   | The status of the cellular data link is unknown.    |
520| DATA_STATE_DISCONNECTED | 0    | The cellular data link is disconnected.    |
521| DATA_STATE_CONNECTING   | 1    | The cellular data link is being connected.|
522| DATA_STATE_CONNECTED    | 2    | The cellular data link is connected.  |
523| DATA_STATE_SUSPENDED    | 3    | The cellular data link is suspended.  |
524