1# 电话服务子系统<a name="ZH-CN_TOPIC_0000001162422291"></a> 2 3- [简介](#section104mcpsimp) 4- [目录](#section119mcpsimp) 5- [约束](#section123mcpsimp) 6- [使用说明](#section128mcpsimp) 7 - [获取当前蜂窝网络信号信息](#section1458213210369) 8 - [观察蜂窝网络状态变化](#section750135512369) 9 10- [相关仓](#section152mcpsimp) 11 12## 简介<a name="section104mcpsimp"></a> 13 14电话服务子系统,提供了一系列的API用于获取无线蜂窝网络和SIM卡相关的一些信息。应用可以通过调用API来获取当前注册网络名称、网络服务状态、信号强度以及SIM卡的相关信息。 15 16各个模块主要作用如下: 17 18- 核心服务模块:主要功能是初始化RIL管理、SIM卡和搜网模块。 19- 通话管理模块:主要功能是管理CS(Circuit Switch,电路交换)、IMS(IP Multimedia Subsystem,IP多媒体子系统)和OTT(over the top,OTT解决方案)三种类型的通话,申请通话所需要的音视频资源,处理多路通话时产生的各种冲突。 20- 蜂窝通话模块:主要功能是实现基于运营商网络的基础通话。 21- 短彩信模块:主要功能是短信收发和彩信编解码。 22- 状态注册模块:主要功能是提供电话服务子系统各种消息事件的订阅以及取消订阅的API。 23 24**图 1** 子系统架构图 25 26 27 28## 目录<a name="section119mcpsimp"></a> 29 30``` 31base/telephony/ 32├── core_service # 核心服务 33├── call_manager # 通话管理 34├── cellular_call # 蜂窝通话 35├── sms_mms # 短彩信 36└── state_registry # 状态注册 37``` 38 39## 约束<a name="section123mcpsimp"></a> 40 411. 目前开源的范围仅支持蜂窝通话(仅支持CS通话)和短信,不支持蜂窝数据上网,仅支持单SIM卡。 42 43## 使用说明<a name="section128mcpsimp"></a> 44 45### 获取当前蜂窝网络信号信息<a name="section1458213210369"></a> 46 471. 从@ohos.telephony.radio.d.ts中导入radio命名空间。 482. 可以通过callback或者Promise的方式调用getSignalInformation\(slotId: number\)方法。 493. 该接口为异步接口,结果会从callback中返回SignalInformation数组。 504. 遍历SignalInformation数组,获取不同制式(signalType)的信号强度(signalLevel)。 51 52 ``` 53 // 引入包名 54 import radio from "@ohos.telephony.radio"; 55 56 // 参数赋值 57 let slotId = 1; 58 59 // 调用接口【callback方式】 60 radio.getSignalInformation(slotId, (err, value) => { 61 if (err) { 62 // 接口调用失败,err非空 63 console.error(`failed to getSignalInformation because ${err.message}`); 64 return; 65 } 66 // 接口调用成功,err为空 67 for (let i = 0; i < value.length; i++) { 68 console.log(`success to getSignalInformation: type is ${value[i].signalType}, level is ${value[i].signalLevel}`); 69 } 70 }); 71 72 // 调用接口【Promise方式】 73 let promise = radio.getSignalInformation(slotId); 74 promise.then((value) => { 75 // 接口调用成功,此处可以实现成功场景分支代码。 76 for (let i = 0; i < value.length; i++) { 77 console.log(`success to getSignalInformation: type is ${value[i].signalType}, level is ${value[i].signalLevel}`); 78 } 79 }).catch((err) => { 80 // 接口调用失败,此处可以实现失败场景分支代码。 81 console.error(`failed to getSignalInformation because ${err.message}`); 82 }); 83 ``` 84 85 86### 观察蜂窝网络状态变化<a name="section750135512369"></a> 87 88**添加观察事件** 89 901. 从@ohos.telephony.observer.d.ts中导入observer命名空间。 912. 调用on\(type: 'networkStateChange'\)方法,传入卡槽id(slotId)和收到事件的回调处理函数(callback),其中slotId为可选参数。 923. 当网络状态发生变更时,调用者会收到回调。 93 94 ``` 95 // 引入包名 96 import observer from '@ohos.telephony.observer'; 97 98 // 开启订阅 99 observer.on('networkStateChange', {slotId: 1}, (err, value) => { 100 if (err) { 101 // 接口调用失败,err非空 102 console.error(`failed, because ${err.message}`); 103 return; 104 } 105 // 接口调用成功,err为空 106 console.log(`success on. network state is ` + value); 107 }); 108 ``` 109 110 111**停止观察** 112 1131. 从@ohos.telephony.observer.d.ts中导入observer命名空间。 1142. 调用off\(type: 'networkStateChange'\)方法,传入添加观察事件时的callback对象。 115 116 ``` 117 // 引入包名 118 import observer from '@ohos.telephony.observer'; 119 120 // 关闭订阅 121 observer.off('networkStateChange', (err, value) => { 122 if (err) { 123 // 接口调用失败,err非空 124 console.error(`failed, because ${err.message}`); 125 return; 126 } 127 // 接口调用成功,err为空 128 console.log(`success off`); 129 }); 130 ``` 131 132 133## 相关仓<a name="section152mcpsimp"></a> 134 135**电话服务子系统** 136 137[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README_zh.md) 138 139[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README_zh.md) 140 141[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README_zh.md) 142 143[telephony\_sms\_mms](https://gitee.com/openharmony/telephony_sms_mms/blob/master/README_zh.md) 144 145[telephony\_state\_registry](https://gitee.com/openharmony/telephony_state_registry/blob/master/README_zh.md) 146