• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.telephony.data (Cellular Data) (System API)
2
3The **data** module provides basic mobile data management functions. You can obtain and set 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> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.telephony.data (Cellular Data)](js-apis-telephony-data.md).
9
10
11## Modules to Import
12
13```ts
14import data from '@ohos.telephony.data';
15```
16
17
18## data.setDefaultCellularDataSlotId
19
20setDefaultCellularDataSlotId(slotId: number, callback: AsyncCallback\<void\>): void
21
22Sets the default slot of the SIM card used for mobile data. This API uses an asynchronous callback to return the result.
23
24**System API**: This is a system API.
25
26**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
27
28**System capability**: SystemCapability.Telephony.CellularData
29
30**Parameters**
31
32| Name  | Type                 | Mandatory| Description                                                        |
33| -------- | --------------------- | ---- | ------------------------------------------------------------ |
34| slotId   | number                | Yes  | SIM card slot ID. <br>- **0**: card slot 1.<br>- **1**: card slot 2.<br>- **-1**: Clears the default configuration.|
35| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.                                                  |
36
37**Error codes**
38
39For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
40
41| ID|                 Error Message                    |
42| -------- | -------------------------------------------- |
43| 201      | Permission denied.                           |
44| 202      | Non-system applications use system APIs.     |
45| 401      | Parameter error.                             |
46| 8300001  | Invalid parameter value.                     |
47| 8300002  | Operation failed. Cannot connect to service. |
48| 8300003  | System internal error.                       |
49| 8300004  | Do not have sim card.                        |
50| 8300999  | Unknown error code.                          |
51| 8301001  | SIM card is not activated.                   |
52
53**Example**
54
55```ts
56import data from '@ohos.telephony.data';
57import { BusinessError } from '@ohos.base';
58
59data.setDefaultCellularDataSlotId(0, (err: BusinessError) => {
60    if(err){
61        console.error(`setDefaultCellularDataSlotId fail,callback: err->${JSON.stringify(err)}.`);
62    }else{
63        console.log(`setDefaultCellularDataSlotId success`);
64    }
65});
66```
67
68## data.setDefaultCellularDataSlotId
69
70setDefaultCellularDataSlotId(slotId: number): Promise\<void\>
71
72Sets the default slot of the SIM card used for mobile data. This API uses a promise to return the result.
73
74**System API**: This is a system API.
75
76**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
77
78**System capability**: SystemCapability.Telephony.CellularData
79
80**Parameters**
81
82| Name| Type  | Mandatory| Description                                                        |
83| ------ | ------ | ---- | ------------------------------------------------------------ |
84| slotId | number | Yes  | SIM card slot ID. <br>- **0**: card slot 1.<br>- **1**: card slot 2.<br>- **-1**: Clears the default configuration.|
85
86**Return value**
87
88| Type           | Description                           |
89| --------------- | ------------------------------- |
90| Promise\<void\> | Promise used to return the result.|
91
92**Error codes**
93
94For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
95
96| ID|                 Error Message                    |
97| -------- | -------------------------------------------- |
98| 201      | Permission denied.                           |
99| 202      | Non-system applications use system APIs.     |
100| 401      | Parameter error.                             |
101| 8300001  | Invalid parameter value.                     |
102| 8300002  | Operation failed. Cannot connect to service. |
103| 8300003  | System internal error.                       |
104| 8300004  | Do not have sim card.                        |
105| 8300999  | Unknown error code.                          |
106| 8301001  | SIM card is not activated.                   |
107
108**Example**
109
110```ts
111import data from '@ohos.telephony.data';
112import { BusinessError } from '@ohos.base';
113
114data.setDefaultCellularDataSlotId(0).then(() => {
115    console.log(`setDefaultCellularDataSlotId success.`);
116}).catch((err: BusinessError) => {
117    console.error(`setDefaultCellularDataSlotId fail, promise: err->${JSON.stringify(err)}`);
118});
119```
120
121
122## data.enableCellularData
123
124enableCellularData(callback: AsyncCallback\<void\>): void
125
126Enables the cellular data service. This API uses an asynchronous callback to return the result.
127
128**System API**: This is a system API.
129
130**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
131
132**System capability**: SystemCapability.Telephony.CellularData
133
134**Parameters**
135
136| Name  | Type                 | Mandatory| Description      |
137| -------- | --------------------- | ---- | ---------- |
138| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|
139
140**Error codes**
141
142For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
143
144| ID|                  Error Message                   |
145| -------- | -------------------------------------------- |
146| 201      | Permission denied.                           |
147| 202      | Non-system applications use system APIs.     |
148| 401      | Parameter error.                             |
149| 8300001  | Invalid parameter value.                     |
150| 8300002  | Operation failed. Cannot connect to service. |
151| 8300003  | System internal error.                       |
152| 8300999  | Unknown error code.                          |
153
154**Example**
155
156```ts
157import data from '@ohos.telephony.data';
158import { BusinessError } from '@ohos.base';
159
160data.enableCellularData((err: BusinessError) => {
161    if(err){
162        console.error(`enableCellularData fail,callback: callback: err->${JSON.stringify(err)}`);
163    }else{
164        console.log(`enableCellularData success`);
165    }
166});
167```
168
169## data.enableCellularData
170
171enableCellularData(): Promise\<void\>
172
173Enables the cellular data service. This API uses a promise to return the result.
174
175**System API**: This is a system API.
176
177**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
178
179**System capability**: SystemCapability.Telephony.CellularData
180
181**Return value**
182
183| Type           | Description                   |
184| --------------- | ----------------------- |
185| Promise\<void\> | Promise used to return the result.|
186
187**Error codes**
188
189For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
190
191| ID|                  Error Message                   |
192| -------- | -------------------------------------------- |
193| 201      | Permission denied.                           |
194| 202      | Non-system applications use system APIs.     |
195| 8300002  | Operation failed. Cannot connect to service. |
196| 8300003  | System internal error.                       |
197| 8300999  | Unknown error code.                          |
198
199**Example**
200
201```ts
202import data from '@ohos.telephony.data';
203import { BusinessError } from '@ohos.base';
204
205data.enableCellularData().then(() => {
206    console.log(`enableCellularData success.`);
207}).catch((err: BusinessError) => {
208    console.error(`enableCellularData fail, promise: err->${JSON.stringify(err)}`);
209});
210```
211
212## data.disableCellularData
213
214disableCellularData(callback: AsyncCallback\<void\>): void
215
216Disables the cellular data service. This API uses an asynchronous callback to return the result.
217
218**System API**: This is a system API.
219
220**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
221
222**System capability**: SystemCapability.Telephony.CellularData
223
224**Parameters**
225
226| Name  | Type                 | Mandatory| Description      |
227| -------- | --------------------- | ---- | ---------- |
228| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|
229
230**Error codes**
231
232For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
233
234| ID|                  Error Message                   |
235| -------- | -------------------------------------------- |
236| 201      | Permission denied.                           |
237| 202      | Non-system applications use system APIs.     |
238| 401      | Parameter error.                             |
239| 8300001  | Invalid parameter value.                     |
240| 8300002  | Operation failed. Cannot connect to service. |
241| 8300003  | System internal error.                       |
242| 8300999  | Unknown error code.                          |
243
244**Example**
245
246```ts
247import data from '@ohos.telephony.data';
248import { BusinessError } from '@ohos.base';
249
250data.disableCellularData((err: BusinessError) => {
251    if(err){
252        console.error(`disableCellularData fail,callback: callback: err->${JSON.stringify(err)}`);
253    }else{
254        console.log(`disableCellularData success`);
255    }
256});
257```
258
259## data.disableCellularData
260
261disableCellularData(): Promise\<void\>
262
263Disables the cellular data service. This API uses a promise to return the result.
264
265**System API**: This is a system API.
266
267**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
268
269**System capability**: SystemCapability.Telephony.CellularData
270
271**Return value**
272
273| Type           | Description                       |
274| --------------- | --------------------------- |
275| Promise\<void\> | Promise used to return the result.|
276
277**Error codes**
278
279For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
280
281| ID|                  Error Message                   |
282| -------- | -------------------------------------------- |
283| 201      | Permission denied.                           |
284| 202      | Non-system applications use system APIs.     |
285| 8300002  | Operation failed. Cannot connect to service. |
286| 8300003  | System internal error.                       |
287| 8300999  | Unknown error code.                          |
288
289**Example**
290
291```ts
292import data from '@ohos.telephony.data';
293import { BusinessError } from '@ohos.base';
294
295data.disableCellularData().then(() => {
296    console.log(`disableCellularData success.`);
297}).catch((err: BusinessError) => {
298    console.error(`disableCellularData fail, promise: err->${JSON.stringify(err)}`);
299});
300```
301
302## data.enableCellularDataRoaming
303
304enableCellularDataRoaming(slotId: number, callback: AsyncCallback\<void\>): void
305
306Enables the cellular data roaming service. This API uses an asynchronous callback to return the result.
307
308**System API**: This is a system API.
309
310**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
311
312**System capability**: SystemCapability.Telephony.CellularData
313
314**Parameters**
315
316| Name  | Type                 | Mandatory| Description                                    |
317| -------- | --------------------- | ---- | ---------------------------------------- |
318| slotId   | number                | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
319| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.                              |
320
321**Error codes**
322
323For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
324
325| ID|                  Error Message                   |
326| -------- | -------------------------------------------- |
327| 201      | Permission denied.                           |
328| 202      | Non-system applications use system APIs.     |
329| 401      | Parameter error.                             |
330| 8300001  | Invalid parameter value.                     |
331| 8300002  | Operation failed. Cannot connect to service. |
332| 8300003  | System internal error.                       |
333| 8300999  | Unknown error code.                          |
334
335**Example**
336
337```ts
338import data from '@ohos.telephony.data';
339import { BusinessError } from '@ohos.base';
340
341data.enableCellularDataRoaming(0, (err: BusinessError) => {
342    if(err){
343        console.error(`enableCellularDataRoaming fail,callback: err->${JSON.stringify(err)}`);
344    }else{
345        console.log(`enableCellularDataRoaming success`);
346    }
347});
348```
349
350## data.enableCellularDataRoaming
351
352enableCellularDataRoaming(slotId: number): Promise\<void\>
353
354Enables the cellular data roaming service. This API uses a promise to return the result.
355
356**System API**: This is a system API.
357
358**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
359
360**System capability**: SystemCapability.Telephony.CellularData
361
362**Parameters**
363
364| Name| Type  | Mandatory| Description                                    |
365| ------ | ------ | ---- | ---------------------------------------- |
366| slotId | number | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
367
368**Return value**
369
370| Type           | Description                     |
371| --------------- | ------------------------- |
372| Promise\<void\> | Promise used to return the result.|
373
374**Error codes**
375
376For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
377
378| ID|                  Error Message                   |
379| -------- | -------------------------------------------- |
380| 201      | Permission denied.                           |
381| 202      | Non-system applications use system APIs.     |
382| 401      | Parameter error.                             |
383| 8300001  | Invalid parameter value.                     |
384| 8300002  | Operation failed. Cannot connect to service. |
385| 8300003  | System internal error.                       |
386| 8300999  | Unknown error code.                          |
387
388**Example**
389
390```ts
391import data from '@ohos.telephony.data';
392import { BusinessError } from '@ohos.base';
393
394data.enableCellularDataRoaming(0).then(() => {
395    console.log(`enableCellularDataRoaming success.`);
396}).catch((err: BusinessError) => {
397    console.error(`enableCellularDataRoaming fail, promise: err->${JSON.stringify(err)}`);
398});
399```
400
401## data.disableCellularDataRoaming
402
403disableCellularDataRoaming(slotId: number, callback: AsyncCallback\<void\>): void
404
405Disables the cellular data roaming service. This API uses an asynchronous callback to return the result.
406
407**System API**: This is a system API.
408
409**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
410
411**System capability**: SystemCapability.Telephony.CellularData
412
413**Parameters**
414
415| Name  | Type                 | Mandatory| Description                                    |
416| -------- | --------------------- | ---- | ---------------------------------------- |
417| slotId   | number                | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
418| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.                              |
419
420**Error codes**
421
422For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
423
424| ID|                  Error Message                   |
425| -------- | -------------------------------------------- |
426| 201      | Permission denied.                           |
427| 202      | Non-system applications use system APIs.     |
428| 401      | Parameter error.                             |
429| 8300001  | Invalid parameter value.                     |
430| 8300002  | Operation failed. Cannot connect to service. |
431| 8300003  | System internal error.                       |
432| 8300999  | Unknown error code.                          |
433
434**Example**
435
436```ts
437import data from '@ohos.telephony.data';
438import { BusinessError } from '@ohos.base';
439
440data.disableCellularDataRoaming(0, (err: BusinessError) => {
441    if(err){
442        console.error(`disableCellularDataRoaming fail,callback: err->${JSON.stringify(err)}`);
443    }else{
444        console.log(`disableCellularDataRoaming success`);
445    }
446});
447```
448
449## data.disableCellularDataRoaming
450
451disableCellularDataRoaming(slotId: number): Promise\<void\>
452
453Disables the cellular data roaming service. This API uses a promise to return the result.
454
455**System API**: This is a system API.
456
457**Required permissions**: ohos.permission.SET_TELEPHONY_STATE
458
459**System capability**: SystemCapability.Telephony.CellularData
460
461**Parameters**
462
463| Name| Type  | Mandatory| Description                                    |
464| ------ | ------ | ---- | ---------------------------------------- |
465| slotId | number | Yes  | Card slot ID.<br>**0**: card slot 1.<br>**1**: card slot 2.|
466
467**Return value**
468
469| Type           | Description                     |
470| --------------- | ------------------------- |
471| Promise\<void\> | Promise used to return the result.|
472
473**Error codes**
474
475For details about the error codes, see[ohos.telephony (Telephony) Error Codes](errorcode-telephony.md).
476
477| ID|                  Error Message                   |
478| -------- | -------------------------------------------- |
479| 201      | Permission denied.                           |
480| 202      | Non-system applications use system APIs.     |
481| 401      | Parameter error.                             |
482| 8300001  | Invalid parameter value.                     |
483| 8300002  | Operation failed. Cannot connect to service. |
484| 8300003  | System internal error.                       |
485| 8300999  | Unknown error code.                          |
486
487**Example**
488
489```ts
490import data from '@ohos.telephony.data';
491import { BusinessError } from '@ohos.base';
492
493data.disableCellularDataRoaming(0).then(() => {
494    console.log(`disableCellularDataRoaming success.`);
495}).catch((err: BusinessError) => {
496    console.error(`disableCellularDataRoaming fail, promise: err->${JSON.stringify(err)}`);
497});
498```
499