README.md
1# State Registry<a name="EN-US_TOPIC_0000001152064139"></a>
2
3- [Introduction](#section117mcpsimp)
4- [Directory Structure](#section124mcpsimp)
5- [Constraints](#section128mcpsimp)
6- [Usage](#section134mcpsimp)
7 - [Available APIs](#section136mcpsimp)
8
9- [Usage Guidelines](#section163mcpsimp)
10 - [Parameters of C APIs](#section1099113151207)
11 - [Sample Code](#section1558565082915)
12
13- [Repositories Involved](#section206mcpsimp)
14
15## Introduction<a name="section117mcpsimp"></a>
16
17The state registry module provides APIs to register and deregister an observer that listens for various callback events of the telephony subsystem. Such events include but are not limited to the following: network status change, signal strength change, cell information change, cellular data connection status change, and call status change.
18
19**Figure 1** Architecture of the state registry module<a name="fig13267152558"></a>
20
21
22## Directory Structure<a name="section124mcpsimp"></a>
23
24```
25/base/telephony/state_registry # State registry service
26├─ BUILD.gn # Build script (gn)
27├─ README.md # Readme
28├─ interfaces # JS APIs
29├─ service
30│ ├─ include # Header files
31│ └─ src # Source files
32├─ sa_profile # SA profile
33├─ ohos.build # Build code
34└─ test # Test code
35```
36
37## Constraints<a name="section128mcpsimp"></a>
38
39- Programming language: JavaScript
40- Software constraints: this service needs to work with the telephony core service \(core\_service\).
41- Hardware constraints: the accommodating device must be equipped with a modem and a SIM card capable of independent cellular communication.
42- The API for registering an observer for the SIM card status takes effect only when SIM cards are in position. If SIM cards are removed, no callback events will be received. Your application can call the **getSimState** API to check whether SIM cards are in position.
43
44## Usage<a name="section134mcpsimp"></a>
45
46### Available APIs<a name="section136mcpsimp"></a>
47
48**Table 1** Registration APIs
49
50<a name="table165976561598"></a>
51
52| API | Description |
53| ------------------------------------------------------------ | ------------------------------ |
54| function on(type: String, options: { slotId?: number }, callback: AsyncCallback\<T\>): void; | Registers an observer. |
55| function off(type: String, callback?: AsyncCallback\<T\>): void; | Deregisters an observer. |
56| function once(type: String, options: { slotId?: number }, callback: AsyncCallback\<T\>): void; | Registers a one-time observer. |
57
58## Usage Guidelines<a name="section163mcpsimp"></a>
59
60### Parameters of C APIs<a name="section1099113151207"></a>
61
62Different subscription events are distinguished by the **type** parameter. The following table lists the related **type** parameters.
63
64**Table 2** Description of type parameters
65
66<a name="table1234838197"></a>
67
68| Parameter | Description | Required Permission |
69| --------------------------------- | ------------------------------------------------------------ | -------------------------------- |
70| networkStateChange | Network status change event | ohos.permission.GET_NETWORK_INFO |
71| signalInfoChange | Signal change event | None |
72| cellularDataConnectionStateChange | Cellular data connection status change event | None |
73| cellularDataFlowChange | Cellular data flow change event | None |
74| callStateChange | Call status change event, in which the value of **phoneNumber** is empty if the user does not have the required permission. | ohos.permission.READ_CALL_LOG |
75
76### Sample Code<a name="section1558565082915"></a>
77
78The function of registering an observer for call status change events is used as an example. The process is as follows:
79
801. Call the **on** method or **once** method with the **type** parameter specified to register an observer for different types of events.
812. Check whether the registration is successful. If **err** is empty in the received callback, the registration is successful. If **err** is not empty, the registration has failed. Obtain the required data from **value** if the registration is successful.
823. Call the **off** method to deregister the observer. After the observer is deregistered, no callback will be received.
83
84 ```
85 // Import the observer package.
86 import observer from '@ohos.telephony.observer';
87
88 // Registers an observer.
89 observer.on('callStateChange', {slotId: 1}, (err, value) => {
90 if (err) {
91 // If the API call failed, err is not empty.
92 console.error(`failed, because ${err.message}`);
93 return;
94 }
95 // If the API call succeeded, err is empty.
96 console.log(`success on. number is ` + value.number + ", state is " + value.state);
97 });
98
99 // Register a one-time observer.
100 observer.once('callStateChange', {slotId: 1}, (err, value) => {
101 if (err) {
102 // If the API call failed, err is not empty.
103 console.error(`failed, because ${err.message}`);
104 return;
105 }
106 // If the API call succeeded, err is empty.
107 console.log(`success once. number is ` + value.number + ", state is " + value.state);
108 });
109
110 // Deregister the observer.
111 observer.off('callStateChange', (err, value) => {
112 if (err) {
113 // If the API call failed, err is not empty.
114 console.error(`failed, because ${err.message}`);
115 return;
116 }
117 // If the API call succeeded, err is empty.
118 console.log(`success off`);
119 });
120 ```
121
122
123## Repositories Involved<a name="section206mcpsimp"></a>
124
125[Telephony](https://gitee.com/openharmony/docs/blob/master/en/readme/telephony.md)
126
127**telephony_state_registry**
128
129[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README.md)
130
131[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README.md)
132
133[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README.md)
README_zh.md
1# 状态注册<a name="ZH-CN_TOPIC_0000001152064139"></a>
2
3- [简介](#section117mcpsimp)
4- [目录](#section124mcpsimp)
5- [约束](#section128mcpsimp)
6- [说明](#section134mcpsimp)
7 - [接口说明](#section136mcpsimp)
8
9- [使用说明](#section163mcpsimp)
10 - [订阅接口参数说明](#section1099113151207)
11 - [接口调用代码示例](#section1558565082915)
12
13- [相关仓](#section206mcpsimp)
14
15## 简介<a name="section117mcpsimp"></a>
16
17状态注册主要负责提供电话服务子系统各种消息事件的订阅以及取消订阅的API。事件类型包括网络状态变化、信号强度变化、小区信息变化、蜂窝数据连接状态变化、通话状态变化等等。
18
19**图 1** 状态注册架构图<a name="fig13267152558"></a>
20
21
22## 目录<a name="section124mcpsimp"></a>
23
24```
25/base/telephony/state_registry # 状态注册转发服务
26├─ BUILD.gn # 编译gn脚本
27├─ README.md # Readme文档
28├─ frameworks # js,native文件
29├─ interfaces # API,innerkits文件
30├─ service
31│ ├─ include # 头文件
32│ └─ src # 源文件
33├─ sa_profile # sa文件
34├─ ohos.build # 编译build
35└─ test # 测试相关
36```
37
38## 约束<a name="section128mcpsimp"></a>
39
40- 开发语言:JavaScript。
41- 软件约束:需要与以下服务配合使用:Telephony核心服务(core\_service)。
42- 硬件约束:需要搭载的设备支持以下硬件:可以进行独立蜂窝通信的Modem以及SIM卡。
43- 使用场景:注册获取SIM卡状态接口仅针对有SIM卡在位场景生效,若用户拔出SIM卡,则接收不到回调事件。应用可通过调用getSimState接口来确定当前卡槽是否有卡在位。
44
45## 说明<a name="section134mcpsimp"></a>
46
47### 接口说明<a name="section136mcpsimp"></a>
48
49**表 1** 注册接口
50
51<a name="table165976561598"></a>
52
53| 接口定义 | **接口描述** |
54| ------------------------------------------------------------ | ------------ |
55| function on(type: String, options: { slotId?: number }, callback: AsyncCallback\<T\>): void; | 开启订阅 |
56| function off(type: String, callback?: AsyncCallback\<T\>): void; | 关闭订阅 |
57| function once(type: String, options: { slotId?: number }, callback: AsyncCallback\<T\>): void; | 一次性订阅 |
58
59## 使用说明<a name="section163mcpsimp"></a>
60
61### 订阅接口参数说明<a name="section1099113151207"></a>
62
63不同订阅事件通过type进行区分,type列表如下:
64
65**表 2** type参数说明
66
67<a name="table1234838197"></a>
68
69| type参数 | 说明 | 所需权限 |
70| --------------------------------- | ----------------------------------------------------- | -------------------------------- |
71| networkStateChange | 网络状态变化事件 | ohos.permission.GET_NETWORK_INFO |
72| signalInfoChange | 信号变化事件 | 无 |
73| cellularDataConnectionStateChange | 蜂窝数据连接状态事件 | 无 |
74| cellularDataFlowChange | 蜂窝数据流变化事件 | 无 |
75| callStateChange | 通话状态变化事件 | ohos.permission.READ_CALL_LOG |
76| simStateChange | SIM卡状态变化事件 | ohos.permission.READ_SIM_STATE |
77
78### 接口调用代码示例<a name="section1558565082915"></a>
79
80以订阅通话状态变化事件为例,主要步骤和代码如下:
81
821. 不同的事件指定不同的type,传入参数,调用on方法(开启订阅)或once方法(一次性订阅)。
832. 收到回调后,若err为空,则订阅成功,否则订阅失败。订阅成功则可以从value中获取数据。
843. 可以通过off方法取消订阅。取消订阅之后,不会再收到任何回调。
85
86 ```
87 // 引入包名
88 import observer from '@ohos.telephony.observer';
89
90 // 开启订阅
91 observer.on('callStateChange', {slotId: 1}, (err, value) => {
92 if (err) {
93 // 接口调用失败,err非空
94 console.error(`failed, because ${err.message}`);
95 return;
96 }
97 // 接口调用成功,err为空
98 console.log(`success on. number is ` + value.number + ", state is " + value.state);
99 });
100
101 // 只订阅一次
102 observer.once('callStateChange', {slotId: 1}, (err, value) => {
103 if (err) {
104 // 接口调用失败,err非空
105 console.error(`failed, because ${err.message}`);
106 return;
107 }
108 // 接口调用成功,err为空
109 console.log(`success once. number is ` + value.number + ", state is " + value.state);
110 });
111
112 // 关闭订阅
113 observer.off('callStateChange', (err, value) => {
114 if (err) {
115 // 接口调用失败,err非空
116 console.error(`failed, because ${err.message}`);
117 return;
118 }
119 // 接口调用成功,err为空
120 console.log(`success off`);
121 });
122 ```
123
124
125## 相关仓<a name="section206mcpsimp"></a>
126
127[电话服务子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E7%94%B5%E8%AF%9D%E6%9C%8D%E5%8A%A1%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
128
129**telephony_state_registry**
130
131[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README_zh.md)
132
133[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README_zh.md)
134
135[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README_zh.md)