1# @ohos.telephony.data (蜂窝数据)(系统接口)
2
3蜂窝数据提供了移动数据管理能力,包括设置默认移动数据的SIM卡,启用、禁用蜂窝数据服务和蜂窝数据漫游。
4
5>**说明:**
6>
7>本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9>当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.telephony.data (蜂窝数据)](js-apis-telephony-data.md)
10
11
12## 导入模块
13
14```ts
15import { data } from '@kit.TelephonyKit';
16```
17
18
19## data.setDefaultCellularDataSlotId
20
21setDefaultCellularDataSlotId(slotId: number, callback: AsyncCallback\<void\>): void
22
23设置默认移动数据的SIM卡,使用callback方式作为异步方法。
24
25**系统接口:** 此接口为系统接口。
26
27**需要权限**:ohos.permission.SET_TELEPHONY_STATE
28
29**系统能力**:SystemCapability.Telephony.CellularData
30
31**参数:**
32
33| 参数名   | 类型                  | 必填 | 说明                                                         |
34| -------- | --------------------- | ---- | ------------------------------------------------------------ |
35| slotId   | number                | 是   | SIM卡槽ID。<br/>- 0:卡槽1。<br/>- 1:卡槽2。 |
36| callback | AsyncCallback\<void\> | 是   | 以callback形式异步返回结果。                                                   |
37
38**错误码:**
39
40以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
41
42| 错误码ID |                 错误信息                     |
43| -------- | -------------------------------------------- |
44| 201      | Permission denied.                           |
45| 202      | Non-system applications use system APIs.     |
46| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
47| 8300001  | Invalid parameter value.                     |
48| 8300002  | Service connection failed.                   |
49| 8300003  | System internal error.                       |
50| 8300004  | No SIM card found.                           |
51| 8300999  | Unknown error.                               |
52| 8301001  | SIM card is not activated.                   |
53
54**示例:**
55
56```ts
57import { data } from '@kit.TelephonyKit';
58import { BusinessError } from '@kit.BasicServicesKit';
59
60data.setDefaultCellularDataSlotId(0, (err: BusinessError) => {
61    if(err){
62        console.error(`setDefaultCellularDataSlotId fail,callback: err->${JSON.stringify(err)}.`);
63    }else{
64        console.log(`setDefaultCellularDataSlotId success`);
65    }
66});
67```
68
69## data.setDefaultCellularDataSlotId
70
71setDefaultCellularDataSlotId(slotId: number): Promise\<void\>
72
73设置默认移动数据的SIM卡,使用Promise方式作为异步方法。
74
75**系统接口:** 此接口为系统接口。
76
77**需要权限**:ohos.permission.SET_TELEPHONY_STATE
78
79**系统能力**:SystemCapability.Telephony.CellularData
80
81**参数:**
82
83| 参数名 | 类型   | 必填 | 说明                                                         |
84| ------ | ------ | ---- | ------------------------------------------------------------ |
85| slotId | number | 是   | SIM卡槽ID。<br/>- 0:卡槽1。<br/>- 1:卡槽2。 |
86
87**返回值:**
88
89| 类型            | 说明                            |
90| --------------- | ------------------------------- |
91| Promise\<void\> | 以Promise形式异步返回设置结果。 |
92
93**错误码:**
94
95以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
96
97| 错误码ID |                 错误信息                     |
98| -------- | -------------------------------------------- |
99| 201      | Permission denied.                           |
100| 202      | Non-system applications use system APIs.     |
101| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                            |
102| 8300001  | Invalid parameter value.                     |
103| 8300002  | Service connection failed.                   |
104| 8300003  | System internal error.                       |
105| 8300004  | No SIM card found.                           |
106| 8300999  | Unknown error.                               |
107| 8301001  | SIM card is not activated.                   |
108
109**示例:**
110
111```ts
112import { data } from '@kit.TelephonyKit';
113import { BusinessError } from '@kit.BasicServicesKit';
114
115data.setDefaultCellularDataSlotId(0).then(() => {
116    console.log(`setDefaultCellularDataSlotId success.`);
117}).catch((err: BusinessError) => {
118    console.error(`setDefaultCellularDataSlotId fail, promise: err->${JSON.stringify(err)}`);
119});
120```
121
122
123## data.enableCellularData
124
125enableCellularData(callback: AsyncCallback\<void\>): void
126
127启用蜂窝数据服务,使用callback方式作为异步方法。
128
129**系统接口:** 此接口为系统接口。
130
131**需要权限**:ohos.permission.SET_TELEPHONY_STATE
132
133**系统能力**:SystemCapability.Telephony.CellularData
134
135**参数:**
136
137| 参数名   | 类型                  | 必填 | 说明       |
138| -------- | --------------------- | ---- | ---------- |
139| callback | AsyncCallback\<void\> | 是   | 以callback形式异步返回结果。 |
140
141**错误码:**
142
143以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
144
145| 错误码ID |                  错误信息                    |
146| -------- | -------------------------------------------- |
147| 201      | Permission denied.                           |
148| 202      | Non-system applications use system APIs.     |
149| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
150| 8300001  | Invalid parameter value.                     |
151| 8300002  | Service connection failed.                   |
152| 8300003  | System internal error.                       |
153| 8300999  | Unknown error.                               |
154
155**示例:**
156
157```ts
158import { data } from '@kit.TelephonyKit';
159import { BusinessError } from '@kit.BasicServicesKit';
160
161data.enableCellularData((err: BusinessError) => {
162    if(err){
163        console.error(`enableCellularData fail,callback: callback: err->${JSON.stringify(err)}`);
164    }else{
165        console.log(`enableCellularData success`);
166    }
167});
168```
169
170## data.enableCellularData
171
172enableCellularData(): Promise\<void\>
173
174启用蜂窝数据服务,使用Promise方式作为异步方法。
175
176**系统接口:** 此接口为系统接口。
177
178**需要权限**:ohos.permission.SET_TELEPHONY_STATE
179
180**系统能力**:SystemCapability.Telephony.CellularData
181
182**返回值:**
183
184| 类型            | 说明                    |
185| --------------- | ----------------------- |
186| Promise\<void\> | 以Promise形式返回结果。 |
187
188**错误码:**
189
190以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
191
192| 错误码ID |                  错误信息                    |
193| -------- | -------------------------------------------- |
194| 201      | Permission denied.                           |
195| 202      | Non-system applications use system APIs.     |
196| 8300002  | Service connection failed.                   |
197| 8300003  | System internal error.                       |
198| 8300999  | Unknown error.                               |
199
200**示例:**
201
202```ts
203import { data } from '@kit.TelephonyKit';
204import { BusinessError } from '@kit.BasicServicesKit';
205
206data.enableCellularData().then(() => {
207    console.log(`enableCellularData success.`);
208}).catch((err: BusinessError) => {
209    console.error(`enableCellularData fail, promise: err->${JSON.stringify(err)}`);
210});
211```
212
213## data.disableCellularData
214
215disableCellularData(callback: AsyncCallback\<void\>): void
216
217禁用蜂窝数据服务,使用callback方式作为异步方法。
218
219**系统接口:** 此接口为系统接口。
220
221**需要权限**:ohos.permission.SET_TELEPHONY_STATE
222
223**系统能力**:SystemCapability.Telephony.CellularData
224
225**参数:**
226
227| 参数名   | 类型                  | 必填 | 说明       |
228| -------- | --------------------- | ---- | ---------- |
229| callback | AsyncCallback\<void\> | 是   | 以callback形式异步返回结果。 |
230
231**错误码:**
232
233以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
234
235| 错误码ID |                  错误信息                    |
236| -------- | -------------------------------------------- |
237| 201      | Permission denied.                           |
238| 202      | Non-system applications use system APIs.     |
239| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
240| 8300001  | Invalid parameter value.                     |
241| 8300002  | Service connection failed.                   |
242| 8300003  | System internal error.                       |
243| 8300999  | Unknown error.                               |
244
245**示例:**
246
247```ts
248import { data } from '@kit.TelephonyKit';
249import { BusinessError } from '@kit.BasicServicesKit';
250
251data.disableCellularData((err: BusinessError) => {
252    if(err){
253        console.error(`disableCellularData fail,callback: callback: err->${JSON.stringify(err)}`);
254    }else{
255        console.log(`disableCellularData success`);
256    }
257});
258```
259
260## data.disableCellularData
261
262disableCellularData(): Promise\<void\>
263
264禁用蜂窝数据服务,使用Promise方式作为异步方法。
265
266**系统接口:** 此接口为系统接口。
267
268**需要权限**:ohos.permission.SET_TELEPHONY_STATE
269
270**系统能力**:SystemCapability.Telephony.CellularData
271
272**返回值:**
273
274| 类型            | 说明                        |
275| --------------- | --------------------------- |
276| Promise\<void\> | 以Promise形式返回禁用结果。 |
277
278**错误码:**
279
280以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
281
282| 错误码ID |                  错误信息                    |
283| -------- | -------------------------------------------- |
284| 201      | Permission denied.                           |
285| 202      | Non-system applications use system APIs.     |
286| 8300002  | Service connection failed.                   |
287| 8300003  | System internal error.                       |
288| 8300999  | Unknown error.                               |
289
290**示例:**
291
292```ts
293import { data } from '@kit.TelephonyKit';
294import { BusinessError } from '@kit.BasicServicesKit';
295
296data.disableCellularData().then(() => {
297    console.log(`disableCellularData success.`);
298}).catch((err: BusinessError) => {
299    console.error(`disableCellularData fail, promise: err->${JSON.stringify(err)}`);
300});
301```
302
303## data.enableCellularDataRoaming
304
305enableCellularDataRoaming(slotId: number, callback: AsyncCallback\<void\>): void
306
307启用蜂窝数据漫游,使用callback方式作为异步方法。
308
309**系统接口:** 此接口为系统接口。
310
311**需要权限**:ohos.permission.SET_TELEPHONY_STATE
312
313**系统能力**:SystemCapability.Telephony.CellularData
314
315**参数:**
316
317| 参数名   | 类型                  | 必填 | 说明                                     |
318| -------- | --------------------- | ---- | ---------------------------------------- |
319| slotId   | number                | 是   | 卡槽ID。<br />0:卡槽1。<br />1:卡槽2。 |
320| callback | AsyncCallback\<void\> | 是   | 以callback形式异步返回结果。                               |
321
322**错误码:**
323
324以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
325
326| 错误码ID |                  错误信息                    |
327| -------- | -------------------------------------------- |
328| 201      | Permission denied.                           |
329| 202      | Non-system applications use system APIs.     |
330| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
331| 8300001  | Invalid parameter value.                     |
332| 8300002  | Service connection failed.                   |
333| 8300003  | System internal error.                       |
334| 8300999  | Unknown error.                               |
335
336**示例:**
337
338```ts
339import { data } from '@kit.TelephonyKit';
340import { BusinessError } from '@kit.BasicServicesKit';
341
342data.enableCellularDataRoaming(0, (err: BusinessError) => {
343    if(err){
344        console.error(`enableCellularDataRoaming fail,callback: err->${JSON.stringify(err)}`);
345    }else{
346        console.log(`enableCellularDataRoaming success`);
347    }
348});
349```
350
351## data.enableCellularDataRoaming
352
353enableCellularDataRoaming(slotId: number): Promise\<void\>
354
355启用蜂窝数据漫游,使用Promise方式作为异步方法。
356
357**系统接口:** 此接口为系统接口。
358
359**需要权限**:ohos.permission.SET_TELEPHONY_STATE
360
361**系统能力**:SystemCapability.Telephony.CellularData
362
363**参数:**
364
365| 参数名 | 类型   | 必填 | 说明                                     |
366| ------ | ------ | ---- | ---------------------------------------- |
367| slotId | number | 是   | 卡槽ID。<br />0:卡槽1。<br />1:卡槽2。 |
368
369**返回值:**
370
371| 类型            | 说明                      |
372| --------------- | ------------------------- |
373| Promise\<void\> | 以Promise形式返回启用结果 |
374
375**错误码:**
376
377以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
378
379| 错误码ID |                  错误信息                    |
380| -------- | -------------------------------------------- |
381| 201      | Permission denied.                           |
382| 202      | Non-system applications use system APIs.     |
383| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
384| 8300001  | Invalid parameter value.                     |
385| 8300002  | Service connection failed.                   |
386| 8300003  | System internal error.                       |
387| 8300999  | Unknown error.                               |
388
389**示例:**
390
391```ts
392import { data } from '@kit.TelephonyKit';
393import { BusinessError } from '@kit.BasicServicesKit';
394
395data.enableCellularDataRoaming(0).then(() => {
396    console.log(`enableCellularDataRoaming success.`);
397}).catch((err: BusinessError) => {
398    console.error(`enableCellularDataRoaming fail, promise: err->${JSON.stringify(err)}`);
399});
400```
401
402## data.disableCellularDataRoaming
403
404disableCellularDataRoaming(slotId: number, callback: AsyncCallback\<void\>): void
405
406禁用蜂窝数据漫游,使用callback方式作为异步方法。
407
408**系统接口:** 此接口为系统接口。
409
410**需要权限**:ohos.permission.SET_TELEPHONY_STATE
411
412**系统能力**:SystemCapability.Telephony.CellularData
413
414**参数:**
415
416| 参数名   | 类型                  | 必填 | 说明                                     |
417| -------- | --------------------- | ---- | ---------------------------------------- |
418| slotId   | number                | 是   | 卡槽ID。<br />0:卡槽1。<br />1:卡槽2。 |
419| callback | AsyncCallback\<void\> | 是   | 以callback形式异步返回结果。                               |
420
421**错误码:**
422
423以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
424
425| 错误码ID |                  错误信息                    |
426| -------- | -------------------------------------------- |
427| 201      | Permission denied.                           |
428| 202      | Non-system applications use system APIs.     |
429| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
430| 8300001  | Invalid parameter value.                     |
431| 8300002  | Service connection failed.                   |
432| 8300003  | System internal error.                       |
433| 8300999  | Unknown error.                               |
434
435**示例:**
436
437```ts
438import { data } from '@kit.TelephonyKit';
439import { BusinessError } from '@kit.BasicServicesKit';
440
441data.disableCellularDataRoaming(0, (err: BusinessError) => {
442    if(err){
443        console.error(`disableCellularDataRoaming fail,callback: err->${JSON.stringify(err)}`);
444    }else{
445        console.log(`disableCellularDataRoaming success`);
446    }
447});
448```
449
450## data.disableCellularDataRoaming
451
452disableCellularDataRoaming(slotId: number): Promise\<void\>
453
454禁用蜂窝数据漫游,使用Promise方式作为异步方法。
455
456**系统接口:** 此接口为系统接口。
457
458**需要权限**:ohos.permission.SET_TELEPHONY_STATE
459
460**系统能力**:SystemCapability.Telephony.CellularData
461
462**参数:**
463
464| 参数名 | 类型   | 必填 | 说明                                     |
465| ------ | ------ | ---- | ---------------------------------------- |
466| slotId | number | 是   | 卡槽ID。<br />0:卡槽1。<br />1:卡槽2。 |
467
468**返回值:**
469
470| 类型            | 说明                      |
471| --------------- | ------------------------- |
472| Promise\<void\> | 以Promise形式返回禁用结果 |
473
474**错误码:**
475
476以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
477
478| 错误码ID |                  错误信息                    |
479| -------- | -------------------------------------------- |
480| 201      | Permission denied.                           |
481| 202      | Non-system applications use system APIs.     |
482| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
483| 8300001  | Invalid parameter value.                     |
484| 8300002  | Service connection failed.                   |
485| 8300003  | System internal error.                       |
486| 8300999  | Unknown error.                               |
487
488**示例:**
489
490```ts
491import { data } from '@kit.TelephonyKit';
492import { BusinessError } from '@kit.BasicServicesKit';
493
494data.disableCellularDataRoaming(0).then(() => {
495    console.log(`disableCellularDataRoaming success.`);
496}).catch((err: BusinessError) => {
497    console.error(`disableCellularDataRoaming fail, promise: err->${JSON.stringify(err)}`);
498});
499```
500