1# Cellular Data<a name="EN-US_TOPIC_0000001105538940"></a> 2 3- [Introduction](#section117mcpsimp) 4- [Directory Structure](#section121mcpsimp) 5- [Constraints](#section125mcpsimp) 6- [Available APIs](#section131mcpsimp) 7- [Usage Guidelines](#section160mcpsimp) 8 - [Checking the Cellular Data Status](#section192mcpsimp) 9 - [Obtaining the Cellular Data Status](#section213mcpsimp) 10 - [Check if cellular mobile data service is enabled](#section234mcpsimp) 11 - [Check if cellular data roaming is enabled](#section255mcpsimp) 12 13- [Repositories Involved](#section234mcpsimp) 14 15## Introduction<a name="section117mcpsimp"></a> 16 17The cellular data module is a tailorable component of the Telephony subsystem. It depends on the telephony core service \(core\_service\) and RIL Adapter \(ril\_adapter\). The module provides functions such as cellular data activation, cellular data fault detection and rectification, cellular data status management, cellular data switch management, cellular data roaming management, APN management, and network management and interaction. 18 19**Figure 1** Architecture of the cellular data module<a name="fig332493822512"></a> 20 21 22## Directory Structure<a name="section121mcpsimp"></a> 23 24``` 25base/telephony/cellular_data/ 26├── figures 27├── frameworks 28│ ├── js 29│ │ └── napi 30│ │ ├── include # js head files 31│ │ └── src # js source files 32│ └── native 33├── interfaces # externally exposed interface 34│ ├── innerkits 35│ └── kits 36│ └── js 37│ └── declaration # external JS API interfaces 38├── sa_profile # SA profiles 39├── services 40│ ├── include # head files 41│ │ ├── apn_manager 42│ │ ├── common 43│ │ ├── state_machine 44│ │ └── utils 45│ └── src # source files 46│ ├── apn_manager 47│ ├── state_machine 48│ └── utils 49└── test 50 └── unit_test # unit test code 51 52``` 53 54## Constraints<a name="section125mcpsimp"></a> 55 56- Programming language: JavaScript 57- In terms of software, this module needs to work with the telephony core service \(core\_service\) and RIL Adapter \(call\_manger\). 58- In terms of hardware, the accommodating device must be equipped with a modem and a SIM card capable of independent cellular communication. 59 60## Available APIs<a name="section131mcpsimp"></a> 61 62**Table 1** External APIs provided by the cellular data module 63 64<a name="table133mcpsimp"></a> 65 66| API | Description | Required Permission | 67| ------------------------------------------------------------ | ------------------------------------------------------------ | -------------------------------- | 68| function isCellularDataEnabled(callback: AsyncCallback\<boolean>): void; | Checks whether the cellular data is enabled | ohos.permission.GET_NETWORK_INFO | 69| function getCellularDataState(callback: AsyncCallback\<DataConnectState>): void; | Obtains the cellular data status. | ohos.permission.GET_NETWORK_INFO | 70| function isCellularDataEnabledSync(): boolean; | Checks if cellular mobile data service is enabled. | ohos.permission.GET_NETWORK_INFO | 71| function isCellularDataRoamingEnabledSync(slotId: number): boolean; | Checks if cellular data roaming is enabled(The parameter slotId is the SIM card id, 0 represents card one, and 1 represents card two). | ohos.permission.GET_NETWORK_INFO | 72 73 74## Usage Guidelines<a name="section160mcpsimp"></a> 75 76### Checking the Cellular Data Status<a name="section192mcpsimp"></a> 77 781. Call the **IsCellularDataEnabled** method in callback or Promise mode to check whether the cellular data is enabled. 792. This method works in asynchronous mode. The execution result is returned through the callback. 80 81 ``` 82 import data from "@ohos.telephony.data"; 83 84 // Call the API in callback mode. 85 data.isCellularDataEnabled((err, value) => { 86 if (err) { 87 // If the API call failed, err is not empty. 88 console.error(`failed to isCellularDataEnabled because ${err.message}`); 89 return; 90 } 91 // If the API call succeeded, err is empty. 92 console.log(`success to isCellularDataEnabled: ${value}`); 93 }); 94 95 // Call the API in Promise mode. 96 let promise = data.isCellularDataEnabled(); 97 promise.then((value) => { 98 // The API call succeeded. 99 console.log(`success to isCellularDataEnabled: ${value}`); 100 }).catch((err) => { 101 // The API call failed. 102 console.error(`failed to isCellularDataEnabled because ${err.message}`); 103 }); 104 ``` 105 106### Check if cellular mobile data service is enabled<a name="section234mcpsimp"></a> 107 1081. You can determine if cellular data services are enabled by calling isCellularDataEnabledSync. 109 1102. This interface is synchronous, and the relevant execution results will be returned from isCellularDataEnabledSync. 111 112 ``` 113 import data from "@ohos.telephony.data"; 114 115 try { 116 // Call the interface [Sync method] 117 let isEnabled: boolean = data.isCellularDataEnabledSync(); 118 // Call the interface successfully 119 console.log(`isCellularDataEnabledSync success : ${isEnabled}`); 120 } catch (error) { 121 // Call the interface failed 122 console.log(`isCellularDataEnabledSync failed`); 123 } 124 ``` 125 126### Check if cellular data roaming is enabled<a name="section255mcpsimp"></a> 127 1281. You can determine if cellular data roaming services are enabled by calling isCellularDataRoamingEnabledSync. 129 1302. This interface is synchronous, and the relevant execution results will be returned from isCellularDataRoamingEnabledSync. 131 132 ``` 133 import data from "@ohos.telephony.data"; 134 135 try { 136 // Call the interface [Sync method] 137 let isEnabled: boolean = data.isCellularDataRoamingEnabledSync(0); 138 // Call the interface successfully 139 console.log(`isCellularDataRoamingEnabledSync success : ${isEnabled}`); 140 } catch (error) { 141 // Call the interface failed 142 console.log(`isCellularDataRoamingEnabledSync failed`); 143 } 144 ``` 145 146### Obtaining the Cellular Data Status<a name="section213mcpsimp"></a> 147 148**Table 2** Description of DataConnectState enum values 149 150<a name="table21531410101919"></a> 151 152| Name | ValueDescription | | 153| ----------------------- | ---------------- | ------------ | 154| DATA_STATE_UNKNOWN | -1 | Unknown | 155| DATA_STATE_DISCONNECTED | 0 | Disconnected | 156| DATA_STATE_CONNECTING | 1 | Connecting | 157| DATA_STATE_CONNECTED | 2 | Connected | 158| DATA_STATE_SUSPENDED | 3 | Suspended | 159 160 1611. Call the **getCellularDataState** method in callback or Promise mode to obtain the cellular data status. 1622. This method works in asynchronous mode. The execution result is returned through the callback. 163 164 ``` 165 import data from "@ohos.telephony.data"; 166 167 // Call the API in callback mode. 168 data.getCellularDataState((err, value) => { 169 if (err) { 170 // If the API call failed, err is not empty. 171 console.error(`failed to getCellularDataState because ${err.message}`); 172 return; 173 } 174 // If the API call succeeded, err is empty. 175 console.log(`success to getCellularDataState: ${value}`); 176 }); 177 178 // Call the API in Promise mode. 179 let promise = data.getCellularDataState(); 180 promise.then((value) => { 181 // The API call succeeded. 182 console.log(`success to getCellularDataState: ${value}`); 183 }).catch((err) => { 184 // The API call failed. 185 console.error(`failed to getCellularDataState because ${err.message}`); 186 }); 187 ``` 188 189 190## Repositories Involved<a name="section234mcpsimp"></a> 191 192Telephony 193 194telephony_cellular_data 195 196telephony_core_service 197 198telephony_ril_adapter 199