openharmony 鸿蒙 js-apis-telephony-data

2025-06-12 浏览 (1)

@ohos.telephony.data (Cellular Data)

The 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.

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 '@kit.TelephonyKit';

data.getDefaultCellularDataSlotId

getDefaultCellularDataSlotId(callback: AsyncCallback<number>): void

Obtains the default slot of the SIM card used for mobile data. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the result.
- 0: card slot 1.
- 1: card slot 2

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

data.getDefaultCellularDataSlotId((err: BusinessError, contextData: number) => {
    if(err){
        console.error(`getDefaultCellularDataSlotId fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
    }else{
        console.log(`getDefaultCellularDataSlotId success`);
    }
});

data.getDefaultCellularDataSlotId

getDefaultCellularDataSlotId(): Promise<number>

Obtains the default slot of the SIM card used for mobile data. This API uses a promise to return the result.

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<number>Promise used to return the result.
- 0: card slot 1.
- 1: card slot 2

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

data.getDefaultCellularDataSlotIdSync9+

getDefaultCellularDataSlotIdSync(): number

Card slot ID.

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
numberCard slot ID.
- 0: card slot 1.
- 1: card slot 2

Example

import { data } from '@kit.TelephonyKit';

console.log("Result: "+ data.getDefaultCellularDataSlotIdSync())

data.getCellularDataFlowType

getCellularDataFlowType(callback: AsyncCallback<DataFlowType>): void

Obtains the cellular data flow type, which can be uplink or downlink. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<DataFlowType>YesCallback used to return the result.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

data.getCellularDataFlowType((err: BusinessError, contextData: data.DataFlowType) => {
    if(err){
        console.error(`getCellularDataFlowType fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
    }else{
        console.log(`getCellularDataFlowType success`);
    }
});

data.getCellularDataFlowType

getCellularDataFlowType(): Promise<DataFlowType>

Obtains the cellular data flow type, which can be uplink or downlink. This API uses a promise to return the result.

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<DataFlowType>Promise used to return the result.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

data.getCellularDataState

getCellularDataState(callback: AsyncCallback<DataConnectState>): void

Obtains the connection status of the packet switched (PS) domain. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<DataConnectState>YesCallback used to return the result.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

data.getCellularDataState((err: BusinessError, contextData: data.DataConnectState) => {
    if(err){
        console.error(`getCellularDataState fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
    }else{
        console.log(`getCellularDataState success`);
    }
});

data.getCellularDataState

getCellularDataState(): Promise<DataConnectState>

Obtains the connection status of the PS domain. This API uses a promise to return the result.

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<DataConnectState>Promise used to return the result.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

data.isCellularDataEnabled

isCellularDataEnabled(callback: AsyncCallback<boolean>): void

Checks whether the cellular data service is enabled. This API uses an asynchronous callback to return the result.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result.
true: The cellular data service is enabled.
false: The cellular data service is disabled.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

data.isCellularDataEnabled((err: BusinessError, contextData: boolean) => {
    if(err){
        console.error(`isCellularDataEnabled fail,callback: callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
    }else{
        console.log(`isCellularDataEnabled success`);
    }
});

data.isCellularDataEnabled

isCellularDataEnabled(): Promise<boolean>

Checks whether the cellular data service is enabled. This API uses a promise to return the result.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<boolean>Promise used to return the result.
true: The cellular data service is enabled.
false: The cellular data service is disabled.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

data.isCellularDataEnabledSync12+

isCellularDataEnabledSync(): boolean

Checks whether the cellular data service is enabled. This API returns the result synchronously.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
booleanWhether the cellular data service is enabled.
true: The cellular data service is enabled.
false: The cellular data service is disabled.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { data } from '@kit.TelephonyKit';

try {
    let isEnabled: boolean = data.isCellularDataEnabledSync();
    console.log(`isCellularDataEnabledSync success : ${isEnabled}`);
} catch (error) {
    console.error(`isCellularDataEnabledSync fail : err->${JSON.stringify(error)}`);  
}

data.isCellularDataRoamingEnabled

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

Checks whether roaming is enabled for the cellular data service. This API uses an asynchronous callback to return the result.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
slotIdnumberYesCard slot ID.
- 0: card slot 1.
- 1: card slot 2
callbackAsyncCallback<boolean>YesCallback used to return the result.
true: Roaming is enabled for the cellular data service.
false: Roaming is disabled for the cellular data service.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

data.isCellularDataRoamingEnabled(0, (err: BusinessError, contextData: boolean) => {
    if(err){
        console.error(`isCellularDataRoamingEnabled fail,callback: err->${JSON.stringify(err)}, contextData->${JSON.stringify(contextData)}`);
    }else{
        console.log(`isCellularDataRoamingEnabled success`);
    }
});

data.isCellularDataRoamingEnabled

isCellularDataRoamingEnabled(slotId: number): Promise<boolean>

Checks whether roaming is enabled for the cellular data service. This API uses a promise to return the result.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
slotIdnumberYesCard slot ID.
- 0: card slot 1.
- 1: card slot 2

Return value

TypeDescription
Promise<boolean>Promise used to return the result.
true: Roaming is enabled for the cellular data service.
false: Roaming is disabled for the cellular data service.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { data } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

data.isCellularDataRoamingEnabledSync12+

isCellularDataRoamingEnabledSync(slotId: number): boolean

Checks whether roaming is enabled for the cellular data service. This API returns the result synchronously.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Telephony.CellularData

Parameters

NameTypeMandatoryDescription
slotIdnumberYesCard slot ID.
- 0: card slot 1.
- 1: card slot 2

Return value

TypeDescription
booleanWhether roaming is enabled for the cellular data service.
true: Roaming is enabled for the cellular data service.
false: Roaming is disabled for the cellular data service.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { data } from '@kit.TelephonyKit';

try {
    let isEnabled: boolean = data.isCellularDataRoamingEnabledSync(0);
    console.log(`isCellularDataRoamingEnabledSync success : ${isEnabled}`);
} catch (error) {
    console.error(`isCellularDataRoamingEnabledSync fail : err->${JSON.stringify(error)}`);  
}

data.getDefaultCellularDataSimId10+

getDefaultCellularDataSimId(): number

Obtains the default ID of the SIM card used for mobile data.

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
numberObtains the default ID of the SIM card used for mobile data.
The return value is bound to the SIM card and increases from 1.

Example

import { data } from '@kit.TelephonyKit';

console.log("Result: "+ data.getDefaultCellularDataSimId());

data.queryAllApns16+

queryAllApns(): Promise<Array<ApnInfo>>

Obtains the access point name (APN) of the default SIM card used for mobile data.

Required permissions: ohos.permission.MANAGE_APN_SETTING

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<Array<ApnInfo>>APN list of the default SIM card used for mobile data.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.

Example

import { data } from '@kit.TelephonyKit';

data.queryAllApns().then((data: Array<data.ApnInfo>) => {
    console.info(`queryAllApns success, promise: data->${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
    console.error(`queryAllApns failed, promise: err->${JSON.stringify(err)}`);
});

data.queryApnIds16+

queryApnIds(apnInfo: ApnInfo): Promise<Array<number>>

Obtains the APN ID corresponding to the specified ApnInfo.

Required permissions: ohos.permission.MANAGE_APN_SETTING

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<Array<number>>List of APN IDs corresponding to the specified ApnInfo.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.

Example

import { data } from '@kit.TelephonyKit';

let apnInfo: data.ApnInfo;
apnInfo = {
  apnName: "CMNET",
  apn: "cmnet",
  mcc: "460",
  mnc: "07",
};

data.queryApnIds(apnInfo).then((data: Array<number>) => {
    console.info(`queryApnIds success, promise: data->${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
    console.error(`queryApnIds failed, promise: err->${JSON.stringify(err)}`);
});

data.setPreferredApn16+

setPreferredApn(apnId: number): Promise<boolean>

Sets the APN indicated by the specified APN ID as the preferred APN.

NOTE

If the input APN ID is invalid, the default preferred APN configured by the carrier is used.

Required permissions: ohos.permission.MANAGE_APN_SETTING

System capability: SystemCapability.Telephony.CellularData

Return value

TypeDescription
Promise<boolean>Promise used to return the result, which is false if no SIM card is inserted.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.

Example

import { data } from '@kit.TelephonyKit';

let apnId: number = 0; // apnId is a valid value returned by queryApnIds. If an invalid APN ID is passed to setPreferredApn, the default preferred APN configured by the carrier is used.
data.setPreferredApn(apnId).then((data: boolean) => {
    console.info(`setPreferredApn success, promise: data->${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
    console.error(`setPreferredApn failed, promise: err->${JSON.stringify(err)}`);
});

DataFlowType

Defines the cellular data flow type.

System capability: SystemCapability.Telephony.CellularData

NameValueDescription
DATA_FLOW_TYPE_NONE0No uplink or downlink data is available.
DATA_FLOW_TYPE_DOWN1Only the downlink data is available.
DATA_FLOW_TYPE_UP2Only the uplink data is available.
DATA_FLOW_TYPE_UP_DOWN3Both the uplink data and downlink data are available.
DATA_FLOW_TYPE_DORMANT4No uplink or downlink data is available because the lower-layer link is in the dormant state.

DataConnectState

Describes the connection status of a cellular data link.

System capability: SystemCapability.Telephony.CellularData

NameValueDescription
DATA_STATE_UNKNOWN-1The status of the cellular data link is unknown.
DATA_STATE_DISCONNECTED0The cellular data link is disconnected.
DATA_STATE_CONNECTING1The cellular data link is being connected.
DATA_STATE_CONNECTED2The cellular data link is connected.
DATA_STATE_SUSPENDED3The cellular data link is suspended.

ApnInfo16+

Defines the APN information.

System capability: SystemCapability.Telephony.CellularData

NameTypeRead-OnlyOptionalDescription
apnNamestringNoNoAPN name.
apnstringNoNoAPN.
mccstringNoNoMobile country code (MCC) of the SIM card.
mncstringNoNoMobile network code (MNC) of the SIM card.
userstringNoYesUser name.
typestringNoYesAPN type.
proxystringNoYesProxy address.
mmsproxystringNoYesMultimedia messaging service (MMS) proxy.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Telephony Kit

harmony 鸿蒙Telephony_NetworkState

harmony 鸿蒙Telephony Error Codes

harmony 鸿蒙js-apis-call-sys

harmony 鸿蒙@ohos.telephony.call (Call)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management) (System API)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management)

harmony 鸿蒙@ohos.telephony.observer (Observer) (System API)

harmony 鸿蒙@ohos.telephony.observer (Observer)

harmony 鸿蒙@ohos.telephony.radio (Radio) (System API)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/Y51u5A