js-apis-observer.md 40.1 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
import observer from '@ohos.telephony.observer';
S
shawn_he 已提交
14 15
```

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
**Error codes**

| 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 已提交
44 45
**Example**

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


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

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

S
shawn_he 已提交
57
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 已提交
58 59 60 61 62 63 64

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

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

**Parameters**

S
shawn_he 已提交
65
| Name| Type  | Mandatory| Description                                  |
S
shawn_he 已提交
66
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
67
| type     | string                                    | Yes  | Network status change event.                 |
S
shawn_he 已提交
68
| slotId | number | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
S
shawn_he 已提交
69
| 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 已提交
70

S
shawn_he 已提交
71 72 73 74 75 76 77 78 79 80 81
**Error codes**

| 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 已提交
82 83
**Example**

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


S
shawn_he 已提交
91
## observer.off('networkStateChange')
S
shawn_he 已提交
92 93 94

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

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

>**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 已提交
105 106 107 108
| 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 已提交
109

S
shawn_he 已提交
110 111 112 113 114 115 116 117
| 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 已提交
118 119
**Example**

S
shawn_he 已提交
120
```js
S
shawn_he 已提交
121 122 123 124 125 126 127 128 129
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 已提交
130
## observer.on('signalInfoChange')
S
shawn_he 已提交
131 132 133

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

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

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

**Parameters**

S
shawn_he 已提交
140
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
141
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
142
| type     | string                                                       | Yes  | Signal information change event.                                            |
S
shawn_he 已提交
143
| 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 已提交
144

S
shawn_he 已提交
145 146 147 148 149 150 151 152 153 154 155
**Error codes**

| 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 已提交
156 157
**Example**

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


S
shawn_he 已提交
165
## observer.on('signalInfoChange')
S
shawn_he 已提交
166 167 168

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

S
shawn_he 已提交
169
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 已提交
170 171 172 173 174

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

**Parameters**

S
shawn_he 已提交
175
| Name| Type  | Mandatory| Description                                  |
S
shawn_he 已提交
176
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
177 178
| 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 已提交
179
| 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 已提交
180

S
shawn_he 已提交
181 182 183 184 185 186 187 188 189 190 191
**Error codes**

| 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 已提交
192 193
**Example**

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


S
shawn_he 已提交
201
## observer.off('signalInfoChange')
S
shawn_he 已提交
202 203 204

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

S
shawn_he 已提交
205
Unregisters the observer for signal status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
206 207 208 209 210 211 212 213 214

>**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 已提交
215
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
216
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
217
| type     | string                                                       | Yes  | Signal information change event.                                            |
S
shawn_he 已提交
218
| 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 已提交
219

S
shawn_he 已提交
220 221 222 223 224 225 226 227 228 229
**Error codes**

| 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 已提交
230 231
**Example**

S
shawn_he 已提交
232
```js
S
shawn_he 已提交
233 234 235 236 237 238 239 240 241
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 已提交
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## observer.on('cellInfoChange')<sup>8+</sup>

on\(type: \'cellInfoChange\', callback: Callback<CellInformation\>\): void;

Registers an observer for cell information change events. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

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

**Parameters**

| Name  | Type                                                     | Mandatory| Description                                                        |
| -------- | --------------------------------------------------------- | ---- |------------------------------------------------------------|
| type     | string                                                    | Yes  | Cell information change event. This field has a fixed value of **cellInfoChange**.                                                  |
| callback | Callback\<[CellInformation](js-apis-radio.md#cellinformation8)\> | Yes  | Callback used to return the result.|

**Error codes**

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

**Example**

```js
observer.on('cellInfoChange', data => {
    console.log("on cellInfoChange, data:" + JSON.stringify(data));
});
```


## observer.on('cellInfoChange')<sup>8+</sup>

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

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.

**System API**: This is a system API.

**Required permissions**: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

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

**Parameters**

| Name| Type                                              | Mandatory| Description                                                        |
| ------ |--------------------------------------------------| ---- |------------------------------------------------------------|
| type     | string                                           | Yes  | Cell information change event. This field has a fixed value of **cellInfoChange**.                                                  |
| slotId | number                                           | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                             |
| callback | Callback\<[CellInformation](js-apis-radio.md#cellinformation8)\> | Yes  | Callback used to return the result.|

**Error codes**

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

**Example**

```js
observer.on('cellInfoChange', {slotId: 0}, data => {
    console.log("on cellInfoChange, data:" + JSON.stringify(data));
});
```


## observer.off('cellInfoChange')<sup>8+</sup>

off\(type: \'cellInfoChange\', callback?: Callback<CellInformation\>\): void;

Unregisters the observer for cell information 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 API**: This is a system API.

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

**Parameters**

| Name  | Type                                                     | Mandatory| Description                                                        |
| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                    | Yes  | Cell information change event. This field has a fixed value of **cellInfoChange**.                                           |
| callback | Callback\<[CellInformation](js-apis-radio.md#cellinformation8)\> | No  | Callback used to return the result.|

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

**Example**

```js
let callback = data => {
    console.log("on cellInfoChange, data:" + JSON.stringify(data));
}
observer.on('cellInfoChange', 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('cellInfoChange', callback);
observer.off('cellInfoChange');
```
S
shawn_he 已提交
361

S
shawn_he 已提交
362
## observer.on('callStateChange')
S
shawn_he 已提交
363

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

S
shawn_he 已提交
366
Registers an observer for call status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
367 368 369 370 371

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

**Parameters**

S
shawn_he 已提交
372
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
373
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
374 375
| 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 已提交
376

S
shawn_he 已提交
377 378 379 380 381 382 383 384 385 386
**Error codes**

| 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 已提交
387 388
**Example**

S
shawn_he 已提交
389
```js
S
shawn_he 已提交
390
observer.on('callStateChange', value => {
S
shawn_he 已提交
391 392 393 394 395
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
});
```


S
shawn_he 已提交
396
## observer.on('callStateChange')
S
shawn_he 已提交
397

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

S
shawn_he 已提交
400
Registers an observer for call status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
401 402 403 404 405

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

**Parameters**

S
shawn_he 已提交
406
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
407
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
408
| type     | string                                                       | Yes  | Call status change event.                                            |
S
shawn_he 已提交
409
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
410
| 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 已提交
411

S
shawn_he 已提交
412 413 414 415 416 417 418 419 420 421
**Error codes**

| 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 已提交
422 423
**Example**

S
shawn_he 已提交
424
```js
S
shawn_he 已提交
425
observer.on('callStateChange', {slotId: 0}, value => {
S
shawn_he 已提交
426 427 428 429 430
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
});
```


S
shawn_he 已提交
431
## observer.off('callStateChange')
S
shawn_he 已提交
432

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

S
shawn_he 已提交
435
Unregisters the observer for call status change events. This API uses an asynchronous callback to return the execution result.
S
shawn_he 已提交
436 437 438 439 440 441 442 443 444

>**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 已提交
445
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
446
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
447 448
| 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 已提交
449

S
shawn_he 已提交
450 451 452 453 454 455 456 457 458 459
**Error codes**

| 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 已提交
460 461
**Example**

S
shawn_he 已提交
462
```js
S
shawn_he 已提交
463 464 465 466 467 468 469 470
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 已提交
471 472 473 474 475 476


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

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

S
shawn_he 已提交
477
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 已提交
478 479 480 481 482 483 484

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
485
| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
S
shawn_he 已提交
486
| 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 已提交
487

S
shawn_he 已提交
488 489 490 491 492 493 494 495 496 497
**Error codes**

| 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 已提交
498 499
**Example**

S
shawn_he 已提交
500
```js
S
shawn_he 已提交
501
observer.on('cellularDataConnectionStateChange', value => {
S
shawn_he 已提交
502 503 504 505 506 507 508 509 510
    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 已提交
511
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 已提交
512 513 514 515 516 517 518

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
519 520
| 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 已提交
521
| 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 已提交
522

S
shawn_he 已提交
523 524 525 526 527 528 529 530 531 532
**Error codes**

| 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 已提交
533 534
**Example**

S
shawn_he 已提交
535
```js
S
shawn_he 已提交
536
observer.on('cellularDataConnectionStateChange', {slotId: 0}, value => {
S
shawn_he 已提交
537 538 539 540 541 542 543 544 545
    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 已提交
546
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 已提交
547 548 549 550 551 552 553 554 555 556 557

>**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 已提交
558
| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
S
shawn_he 已提交
559
| 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 已提交
560

S
shawn_he 已提交
561 562 563 564 565 566 567 568 569 570
**Error codes**

| 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 已提交
571 572
**Example**

S
shawn_he 已提交
573
```js
S
shawn_he 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
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 已提交
599 600 601 602 603 604 605 606 607 608
**Error codes**

| 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 已提交
609 610
**Example**

S
shawn_he 已提交
611
```js
S
shawn_he 已提交
612
observer.on('cellularDataFlowChange', data => {
S
shawn_he 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    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 已提交
631
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
632 633
| 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 已提交
634 635 636 637 638 639 640 641 642 643
**Error codes**

| 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 已提交
644 645
**Example**

S
shawn_he 已提交
646
```js
S
shawn_he 已提交
647
observer.on('cellularDataFlowChange', {slotId: 0}, data => {
S
shawn_he 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    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 已提交
672 673 674 675 676 677 678 679 680 681
**Error codes**

| 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 已提交
682 683
**Example**

S
shawn_he 已提交
684
```js
S
shawn_he 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
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 已提交
710 711 712 713 714 715 716 717 718 719
**Error codes**

| 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 已提交
720 721
**Example**

S
shawn_he 已提交
722
```js
S
shawn_he 已提交
723
observer.on('simStateChange', data => {
S
shawn_he 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    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 已提交
742
| slotId   | number                                                       | Yes  | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
743 744
| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|

S
shawn_he 已提交
745 746 747 748 749 750 751 752 753 754
**Error codes**

| 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 已提交
755 756
**Example**

S
shawn_he 已提交
757
```js
S
shawn_he 已提交
758
observer.on('simStateChange', {slotId: 0}, data => {
S
shawn_he 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    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 已提交
783 784 785 786 787 788 789 790 791 792
**Error codes**

| 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 已提交
793 794
**Example**

S
shawn_he 已提交
795
```js
S
shawn_he 已提交
796 797 798 799 800 801 802 803 804 805 806 807
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 已提交
808
Enumerates SIM card lock types.
S
shawn_he 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

**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 已提交
835 836
|     Name           |                 Type               | Mandatory| Description                                                     |
| ------------------- | ----------------------------------- | ---- | --------------------------------------------------------  |
S
shawn_he 已提交
837 838
| type                | [CardType](js-apis-sim.md#cardtype7) | Yes  | SIM card type.|
| state               | [SimState](js-apis-sim.md#simstate) | Yes  | SIM card state.|
S
shawn_he 已提交
839
| reason<sup>8+</sup> | [LockReason](#lockreason8)          | Yes  | SIM card lock type.                                            |