js-apis-telephony-data.md 10.9 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
# Cellular Data

>**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 '@ohos.telephony.data';
```

## data.getDefaultCellularDataSlotId<a name=data.getDefaultCellularDataSlotId-callback></a>

getDefaultCellularDataSlotId(callback: AsyncCallback\<number\>): void 

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

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | ------------------------------------------ |
| callback | AsyncCallback\<number\> | Yes| Callback used to return the result. <br /> **0**: slot 1 <br/>**1**: slot 2|

**Example**

```
data.getDefaultCellularDataSlotId((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

## data.getDefaultCellularDataSlotId<a name=data.getDefaultCellularDataSlotId-promise></a>

getDefaultCellularDataSlotId(): Promise\<number\> 

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

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Return value**

| Type| Description|
| ----------------- | ------------------------------------------------------------ |
| Promise\<number\> | Promise used to return the result, wherein: <br />**0**: slot 1 <br/>**1**: slot 2|

**Example**

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

## data.getCellularDataFlowType<a name=data.getCellularDataFlowType-callback></a>

getCellularDataFlowType(callback: AsyncCallback\<DataFlowType\>): void

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

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ---------------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<[DataFlowType](#DataFlowType)\> | Yes| Callback used to return the result.|

**Example**

```
data.getCellularDataFlowType((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

## data.getCellularDataFlowType<a name=data.getCellularDataFlowType-promise></a>

getCellularDataFlowType(): Promise\<DataFlowType\>

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

**System capability**: SystemCapability.Telephony.CellularData

**Return value**

| Type| Description|
| ---------------------------------------- | ----------------------------------------------- |
| Promise\<[DataFlowType](#DataFlowType)\> | Promise used to return the result. |

**Example**

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

## data.getCellularDataState<a name=data.getCellularDataState-callback></a>

getCellularDataState(callback: AsyncCallback\<DataConnectState\>): void

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

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ------------------------------------------------------ | ---- | ---------- |
| callback | AsyncCallback\<[DataConnectState](#DataConnectState)\> | Yes| Callback used to return the result.|

**Example**

```
data.getCellularDataState((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

## data.getCellularDataState<a name=data.getCellularDataState-promise></a>

getCellularDataState(): Promise\<DataConnectState\>

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

**System capability**: SystemCapability.Telephony.CellularData

**Return value**

| Type| Description|
| ------------------------------------------------ | ------------------------------------- |
| Promise\<[DataConnectState](#DataConnectState)\> | Promise used to return the result.|

**Example**

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

## data.isCellularDataEnabled<a name=data.isCellularDataEnabled-callback></a>

isCellularDataEnabled(callback: AsyncCallback\<boolean\>): void

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

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
| 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.|

**Example**

```
data.isCellularDataEnabled((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

## data.isCellularDataEnabled<a name=data.isCellularDataEnabled-promise></a>

isCellularDataEnabled(): Promise\<boolean\>

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

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Return value**

| Type| Description|
| ------------------ | ------------------------------------------------------------ |
| Promise\<boolean\> | Promise used to return the result, wherein: <br />**true**: The cellular data service is enabled. <br />**false**: The cellular data service is disabled.|

**Example**

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

## data.isCellularDataRoamingEnabled<a name=data.isCellularDataRoamingEnabled-callback></a>

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

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

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
| slotId   | number                   | Yes| Card slot ID. The options are as follows: <br />**0**: slot 1 <br />**1**: slot 2|
| 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.|

**Example**

```
data.isCellularDataRoamingEnabled(0,(err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```

## data.isCellularDataRoamingEnabled<a name=data.isCellularDataRoamingEnabled-promise></a>

isCellularDataRoamingEnabled(slotId: number): Promise\<boolean\>

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

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.CellularData

**Parameters**

| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | ---------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br />**0**: slot 1 <br/>**1**: slot 2|

**Return value**

| Type| Description|
| ------------------ | ------------------------------------------------------------ |
| Promise\<boolean\> | Promise used to return the result, wherein: <br />**true**: Roaming is enabled for the cellular data service. <br />**false**: Roaming is disabled for the cellular data service.|

**Example**

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

## DataFlowType<a name=DataFlowType></a>

 Defines the cellular data flow type.

| Name| Value| Description|
| ------ | ---------------------- | ------------------------------------------------------------ |
| 0      | DATA_FLOW_TYPE_NONE    | No uplink or downlink data is available. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 1      | DATA_FLOW_TYPE_DOWN    | Only the downlink data is available. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 2      | DATA_FLOW_TYPE_UP      | Only the uplink data is available. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 3      | DATA_FLOW_TYPE_UP_DOWN | Both uplink data and downlink data are available. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 4      | DATA_FLOW_TYPE_DORMANT | No uplink or downlink data is available because the lower-layer link is in the dormant state. <br />**System capability**: SystemCapability.Telephony.CellularData|

## DataConnectState<a name=DataConnectState></a>

 Describes the connection status of a cellular data link.

| Name| Value| Description|
| ------ | ----------------------- | ------------------------------------------------------------ |
| -1     | DATA_STATE_UNKNOWN| The status of the cellular data link is unknown. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 0      | DATA_STATE_DISCONNECTED | The cellular data link is disconnected. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 1      | DATA_STATE_CONNECTING   | The cellular data link is being connected. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 2      | DATA_STATE_CONNECTED    | The cellular data link is connected. <br />**System capability**: SystemCapability.Telephony.CellularData|
| 3      | DATA_STATE_SUSPENDED    | The cellular data link is suspended. <br />**System capability**: SystemCapability.Telephony.CellularData|