js-apis-observer.md 36.7 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.telephony.observer (Observer)
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The **observer** module provides event subscription management functions. You can register or unregister an observer that listens for the following events: network status change, signal status change, call status change, cellular data connection status, uplink and downlink data flow status of cellular data services, and SIM status change.
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6 7 8 9 10 11
>**NOTE**
>
>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.


## Modules to Import

S
shawn_he 已提交
12
```
S
shawn_he 已提交
13 14 15
import observer from '@ohos.telephony.observer'
```

S
shawn_he 已提交
16
## observer.on('networkStateChange')
S
shawn_he 已提交
17 18 19

on\(type: \'networkStateChange\', callback: Callback<NetworkState\>\): void;

S
shawn_he 已提交
20
Registers an observer for network status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
21 22 23 24 25 26 27

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

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
28 29 30 31
| Name  | Type                                                     | Mandatory| Description                                                        |
| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                    | Yes  | Network status change event.                                            |
| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | Yes  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
S
shawn_he 已提交
32

S
shawn_he 已提交
33 34 35 36 37 38 39 40 41 42 43 44
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 201      | Permission denied.                           |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
45 46
**Example**

S
shawn_he 已提交
47
```js
S
shawn_he 已提交
48 49 50 51 52 53
observer.on('networkStateChange', data =>{ 
    console.log("on networkStateChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
54
## observer.on('networkStateChange')
S
shawn_he 已提交
55 56 57

on\(type: \'networkStateChange\', options: { slotId: number }, callback: Callback<NetworkState\>\): void;

S
shawn_he 已提交
58
Registers an observer for network status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
59 60 61 62 63 64 65

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

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
66
| Name| Type  | Mandatory| Description                                  |
S
shawn_he 已提交
67
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
68
| type     | string                                    | Yes  | Network status change event.                 |
S
shawn_he 已提交
69
| slotId | number | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
S
shawn_he 已提交
70
| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | Yes  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
S
shawn_he 已提交
71

S
shawn_he 已提交
72 73 74 75 76 77 78 79 80 81 82 83
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 201      | Permission denied.                           |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
84 85
**Example**

S
shawn_he 已提交
86
```js
S
shawn_he 已提交
87 88 89 90 91 92
observer.on('networkStateChange', {slotId: 0}, data =>{ 
    console.log("on networkStateChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
93
## observer.off('networkStateChange')
S
shawn_he 已提交
94 95 96

off\(type: \'networkStateChange\', callback?: Callback<NetworkState\>\): void;

S
shawn_he 已提交
97
Unregisters the observer for network status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
98 99 100 101 102 103 104 105 106

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
107 108 109 110
| Name  | Type                                                     | Mandatory| Description                                                        |
| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                    | Yes  | Network status change event.                                            |
| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | No  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
S
shawn_he 已提交
111

S
shawn_he 已提交
112 113 114 115 116 117 118 119
| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
120 121
**Example**

S
shawn_he 已提交
122
```js
S
shawn_he 已提交
123 124 125 126 127 128 129 130 131
let callback = data => {
    console.log("on networkStateChange, data:" + JSON.stringify(data));
}
observer.on('networkStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('networkStateChange', callback);
observer.off('networkStateChange');
```

S
shawn_he 已提交
132
## observer.on('signalInfoChange')
S
shawn_he 已提交
133 134 135

on\(type: \'signalInfoChange\', callback: Callback<Array<SignalInformation\>\>): void;

S
shawn_he 已提交
136
Registers an observer for signal status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
137 138 139 140 141

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
142
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
143
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
144
| type     | string                                                       | Yes  | Signal information change event.                                            |
S
shawn_he 已提交
145
| callback | Callback<Array<[SignalInformation](js-apis-radio.md#signalinformation)\>\> | Yes  | Callback used to return the result. For details, see [SignalInformation](js-apis-radio.md#signalinformation).|
S
shawn_he 已提交
146

S
shawn_he 已提交
147 148 149 150 151 152 153 154 155 156 157 158
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 201      | Permission denied.                           |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
159 160
**Example**

S
shawn_he 已提交
161
```js
S
shawn_he 已提交
162 163 164 165 166 167
observer.on('signalInfoChange', data =>{ 
    console.log("on signalInfoChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
168
## observer.on('signalInfoChange')
S
shawn_he 已提交
169 170 171

on\(type: \'signalInfoChange\', options: { slotId: number }, callback: Callback<Array<SignalInformation\>\>): void;

S
shawn_he 已提交
172
Registers an observer for signal status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
173 174 175 176 177

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
178
| Name| Type  | Mandatory| Description                                  |
S
shawn_he 已提交
179
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
180 181
| type     | string                                    | Yes  | Signal information change event.               |
| slotId | number | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
S
shawn_he 已提交
182
| callback | Callback<Array<[SignalInformation](js-apis-radio.md#signalinformation)\>\> | Yes  | Callback used to return the result. For details, see [SignalInformation](js-apis-radio.md#signalinformation).|
S
shawn_he 已提交
183

S
shawn_he 已提交
184 185 186 187 188 189 190 191 192 193 194 195
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 201      | Permission denied.                           |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
196 197
**Example**

S
shawn_he 已提交
198
```js
S
shawn_he 已提交
199 200 201 202 203 204
observer.on('signalInfoChange', {slotId: 0}, data =>{ 
    console.log("on signalInfoChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
205
## observer.off('signalInfoChange')
S
shawn_he 已提交
206 207 208

off\(type: \'signalInfoChange\', callback?: Callback<Array<SignalInformation\>\>): void;

S
shawn_he 已提交
209
Unregisters the observer for signal status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
210 211 212 213 214 215 216 217 218

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
219
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
220
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
221
| type     | string                                                       | Yes  | Signal information change event.                                            |
S
shawn_he 已提交
222
| callback | Callback<Array<[SignalInformation](js-apis-radio.md#signalinformation)\>\> | No  | Callback used to return the result. For details, see [SignalInformation](js-apis-radio.md#signalinformation).|
S
shawn_he 已提交
223

S
shawn_he 已提交
224 225 226 227 228 229 230 231 232 233 234
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
235 236
**Example**

S
shawn_he 已提交
237
```js
S
shawn_he 已提交
238 239 240 241 242 243 244 245 246 247
let callback = data => {
    console.log("on signalInfoChange, data:" + JSON.stringify(data));
}
observer.on('signalInfoChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('signalInfoChange', callback);
observer.off('signalInfoChange');
```


S
shawn_he 已提交
248
## observer.on('callStateChange')
S
shawn_he 已提交
249

S
shawn_he 已提交
250
on(type: 'callStateChange', callback: Callback\<{ state: CallState, number: string }\>): void;
S
shawn_he 已提交
251

S
shawn_he 已提交
252
Registers an observer for call status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
253 254 255 256 257

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
258
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
259
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
260 261
| type     | string                                                       | Yes  | Call status change event.                                            |
| callback | Callback\<{ state: [CallState](js-apis-call.md#callstate), number: string }\> | Yes  | Callback function. For details, see [CallState](js-apis-call.md#callstate) in call.<br>**number**: phone number.|
S
shawn_he 已提交
262

S
shawn_he 已提交
263 264 265 266 267 268 269 270 271 272 273
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
274 275
**Example**

S
shawn_he 已提交
276
```js
S
shawn_he 已提交
277 278 279 280 281 282
observer.on('callStateChange', value =>{ 
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
});
```


S
shawn_he 已提交
283
## observer.on('callStateChange')
S
shawn_he 已提交
284

S
shawn_he 已提交
285
on(type: 'callStateChange', options: { slotId: number }, callback: Callback<{ state:CallState, number: string }>): void;
S
shawn_he 已提交
286

S
shawn_he 已提交
287
Registers an observer for call status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
288 289 290 291 292

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
293
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
294
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
295
| type     | string                                                       | Yes  | Call status change event.                                            |
S
shawn_he 已提交
296
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
297
| callback | Callback\<{ state: [CallState](js-apis-call.md#callstate), number: string }\> | Yes  | Callback function. For details, see [CallState](js-apis-call.md#callstate) in call.<br>**number**: phone number.|
S
shawn_he 已提交
298

S
shawn_he 已提交
299 300 301 302 303 304 305 306 307 308 309
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
310 311
**Example**

S
shawn_he 已提交
312
```js
S
shawn_he 已提交
313 314 315 316 317 318
observer.on('callStateChange', {slotId: 0}, value =>{ 
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
});
```


S
shawn_he 已提交
319
## observer.off('callStateChange')
S
shawn_he 已提交
320

S
shawn_he 已提交
321
off(type: 'callStateChange', callback?: Callback<{ state: CallState, number: string }>): void;
S
shawn_he 已提交
322

S
shawn_he 已提交
323
Unregisters the observer for call status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
324 325 326 327 328 329 330 331 332

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
333
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
334
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
335 336
| type     | string                                                       | Yes  | Call status change event.                                            |
| callback | Callback\<{ state: [CallState](js-apis-call.md#callstate), number: string }\> | No  | Callback function. For details, see [CallState](js-apis-call.md#callstate) in call.<br>**number**: phone number.|
S
shawn_he 已提交
337

S
shawn_he 已提交
338 339 340 341 342 343 344 345 346 347 348
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
349 350
**Example**

S
shawn_he 已提交
351
```js
S
shawn_he 已提交
352 353 354 355 356 357 358 359
let callback = value => {
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
}
observer.on('callStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('callStateChange', callback);
observer.off('callStateChange');
```
S
shawn_he 已提交
360 361 362 363 364 365


## observer.on('cellularDataConnectionStateChange')<sup>7+</sup>

on\(type: 'cellularDataConnectionStateChange', callback: Callback\<{ state: DataConnectState, network: RatType}\>\): void;

S
shawn_he 已提交
366
Registers an observer for connection status change events of the cellular data link. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
367 368 369 370 371 372 373

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
374
| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
S
shawn_he 已提交
375
| callback | Callback\<{ state: [DataConnectState](js-apis-telephony-data.md#dataconnectstate), network: [RatType](js-apis-radio.md#radiotechnology) }\> | Yes  | Callback used to return the result. For details, see [DataConnectState](js-apis-telephony-data.md#dataconnectstate) and [RadioTechnology](js-apis-radio.md#radiotechnology).|
S
shawn_he 已提交
376

S
shawn_he 已提交
377 378 379 380 381 382 383 384 385 386 387
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
388 389
**Example**

S
shawn_he 已提交
390
```js
S
shawn_he 已提交
391 392 393 394 395 396 397 398 399 400
observer.on('cellularDataConnectionStateChange', value =>{
    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
});
```


## observer.on('cellularDataConnectionStateChange')<sup>7+</sup>

on\(type: 'cellularDataConnectionStateChange', options: { slotId: number }, callback: Callback\<{ state: DataConnectState, network: RatType }\>\): void;

S
shawn_he 已提交
401
Registers an observer for connection status change events of the cellular data link over the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
402 403 404 405 406 407 408

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
409 410
| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
411
| callback | Callback\<{ state: [DataConnectState](js-apis-telephony-data.md#dataconnectstate), network: [RatType](js-apis-radio.md#radiotechnology) }\> | Yes  | Callback used to return the result. For details, see [DataConnectState](js-apis-telephony-data.md#dataconnectstate) and [RadioTechnology](js-apis-radio.md#radiotechnology).|
S
shawn_he 已提交
412

S
shawn_he 已提交
413 414 415 416 417 418 419 420 421 422 423
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
424 425
**Example**

S
shawn_he 已提交
426
```js
S
shawn_he 已提交
427 428 429 430 431 432 433 434 435 436
observer.on('cellularDataConnectionStateChange', {slotId: 0}, value =>{
    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
});
```


## observer.off('cellularDataConnectionStateChange')<sup>7+</sup>

off\(type: 'cellularDataConnectionStateChange',  callback?: Callback\<{ state: DataConnectState, network: RatType}\>\): void;

S
shawn_he 已提交
437
Unregisters the observer for connection status change events of the cellular data link. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
438 439 440 441 442 443 444 445 446 447 448

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
449
| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
S
shawn_he 已提交
450
| callback | Callback\<{ state: [DataConnectState](js-apis-telephony-data.md#dataconnectstate), network: [RatType](js-apis-radio.md#radiotechnology) }\> | No  | Callback used to return the result. For details, see [DataConnectState](js-apis-telephony-data.md#dataconnectstate) and [RadioTechnology](js-apis-radio.md#radiotechnology).|
S
shawn_he 已提交
451

S
shawn_he 已提交
452 453 454 455 456 457 458 459 460 461 462
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
463 464
**Example**

S
shawn_he 已提交
465
```js
S
shawn_he 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
let callback = value => {
    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
}
observer.on('cellularDataConnectionStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('cellularDataConnectionStateChange', callback);
observer.off('cellularDataConnectionStateChange');
```


## observer.on('cellularDataFlowChange')<sup>7+</sup>

on\(type: 'cellularDataFlowChange', callback: Callback\<DataFlowType\>\): void;

Registers an observer for the uplink and downlink data flow status change events of the cellular data service. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                      |
| callback | Callback\<[DataFlowType](js-apis-telephony-data.md#dataflowtype)\> | Yes  | Callback used to return the result. For details, see [DataFlowType](js-apis-telephony-data.md#dataflowtype).|

S
shawn_he 已提交
491 492 493 494 495 496 497 498 499 500 501
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
502 503
**Example**

S
shawn_he 已提交
504
```js
S
shawn_he 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
observer.on('cellularDataFlowChange', data =>{
    console.log("on networkStateChange, data:" + JSON.stringify(data));
});
```


## observer.on('cellularDataFlowChange')<sup>7+</sup>

on\(type: 'cellularDataFlowChange', options: { slotId: number },  callback: Callback\<DataFlowType\>\): void;

Registers an observer for the uplink and downlink data flow status change events of the cellular data service on the SIM card in the specified slot. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                          |
S
shawn_he 已提交
524
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
525 526
| callback | Callback\<[DataFlowType](js-apis-telephony-data.md#dataflowtype)\> | Yes  | Callback used to return the result. For details, see [DataFlowType](js-apis-telephony-data.md#dataflowtype).|

S
shawn_he 已提交
527 528 529 530 531 532 533 534 535 536 537
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
538 539
**Example**

S
shawn_he 已提交
540
```js
S
shawn_he 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
observer.on('cellularDataFlowChange', {slotId: 0}, data =>{
    console.log("on cellularDataFlowChange, data:" + JSON.stringify(data));
});
```


## observer.off('cellularDataFlowChange')<sup>7+</sup>

off\(type: 'cellularDataFlowChange', callback?: Callback\<DataFlowType\>\): void;

Unregisters the observer for the uplink and downlink data flow status change events of the cellular data service. This API uses an asynchronous callback to return the result.

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                          |
| callback | Callback\<[DataFlowType](js-apis-telephony-data.md#dataflowtype)\> | No  | Callback used to return the result. For details, see [DataFlowType](js-apis-telephony-data.md#dataflowtype).|

S
shawn_he 已提交
566 567 568 569 570 571 572 573 574 575 576
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                  Error Message                   |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
577 578
**Example**

S
shawn_he 已提交
579
```js
S
shawn_he 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
let callback = data => {
    console.log("on cellularDataFlowChange, data:" + JSON.stringify(data));
}
observer.on('cellularDataFlowChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('cellularDataFlowChange', callback);
observer.off('cellularDataFlowChange');
```


## observer.on('simStateChange')<sup>7+</sup>

on\(type: 'simStateChange', callback: Callback\<SimStateData\>\): void;

Registers an observer for SIM card status change events. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | SIM card status change event.                                    |
| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|

S
shawn_he 已提交
605 606 607 608 609 610 611 612 613 614 615
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                 Error Message                    |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
616 617
**Example**

S
shawn_he 已提交
618
```js
S
shawn_he 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
observer.on('simStateChange', data =>{
    console.log("on simStateChange, data:" + JSON.stringify(data));
});
```


## observer.on('simStateChange')<sup>7+</sup>

on\(type: 'simStateChange', options: { slotId: number }, callback: Callback\<SimStateData\>\): void;

Registers an observer for status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | SIM card status change event.                                    |
S
shawn_he 已提交
638
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
639 640
| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|

S
shawn_he 已提交
641 642 643 644 645 646 647 648 649 650 651
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                 Error Message                    |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
652 653
**Example**

S
shawn_he 已提交
654
```js
S
shawn_he 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
observer.on('simStateChange', {slotId: 0}, data =>{
    console.log("on simStateChange, data:" + JSON.stringify(data));
});
```


## observer.off('simStateChange')<sup>7+</sup>

off\(type: 'simStateChange', callback?: Callback\<SimStateData\>\): void;

Unregisters the observer for SIM card status change events. This API uses an asynchronous callback to return the result.

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | SIM card status change event.                                            |
| callback | Callback\<[SimStateData](#simstatedata7)\> | No  | Callback used to return the result.|

S
shawn_he 已提交
680 681 682 683 684 685 686 687 688 689 690
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).

| ID|                 Error Message                    |
| -------- | -------------------------------------------- |
| 401      | Parameter error.                             |
| 8300001  | Invalid parameter value.                     |
| 8300002  | Operation failed. Cannot connect to service. |
| 8300003  | System internal error.                       |
| 8300999  | Unknown error code.                          |

S
shawn_he 已提交
691 692
**Example**

S
shawn_he 已提交
693
```js
S
shawn_he 已提交
694 695 696 697 698 699 700 701 702 703 704 705
let callback = data => {
    console.log("on simStateChange, data:" + JSON.stringify(data));
}
observer.on('simStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('simStateChange', callback);
observer.off('simStateChange');
```


## LockReason<sup>8+</sup>

S
shawn_he 已提交
706
Enumerates SIM card lock types.
S
shawn_he 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

**System capability**: SystemCapability.Telephony.StateRegistry

| Name       | Value  | Description             |
| ----------- | ---- | ----------------- |
| SIM_NONE    | 0    | No lock.           |
| SIM_PIN     | 1    | PIN lock.          |
| SIM_PUK     | 2    | PUK lock.          |
| SIM_PN_PIN  | 3    | Network PIN lock.      |
| SIM_PN_PUK  | 4    | Network PUK lock.      |
| SIM_PU_PIN  | 5    | Subnet PIN lock.      |
| SIM_PU_PUK  | 6    | Subnet PUK lock.      |
| SIM_PP_PIN  | 7    | Service provider PIN lock.|
| SIM_PP_PUK  | 8    | Service provider PUK lock.|
| SIM_PC_PIN  | 9    | Organization PIN lock.      |
| SIM_PC_PUK  | 10   | Organization PUK lock.      |
| SIM_SIM_PIN | 11   | SIM PIN lock.      |
| SIM_SIM_PUK | 12   | SIM PUK lock.      |


## SimStateData<sup>7+</sup>

Enumerates SIM card types and states.

**System capability**: SystemCapability.Telephony.StateRegistry

S
shawn_he 已提交
733 734 735 736 737
|     Name           |                 Type               | Mandatory| Description                                                     |
| ------------------- | ----------------------------------- | ---- | --------------------------------------------------------  |
| type                | [CardType](js-apis-sim.md#cardtype) | Yes  | SIM card type. For details, see [CardType](js-apis-sim.md#cardtype).|
| state               | [SimState](js-apis-sim.md#simstate) | Yes  | SIM card status. For details, see [SimState](js-apis-sim.md#simstate).|
| reason<sup>8+</sup> | [LockReason](#lockreason8)          | Yes  | SIM card lock type.                                            |