• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import sms from '@ohos.telephony.sms'
17import Logger from '../model/Logger'
18
19const TAG: string = `[SmsModel]`
20
21export default class SmsModel {
22  async createMessage() {
23    const specification = '3gpp'
24    const pdu = [0x08, 0x91] // 以数组的形式显示协议数据单元(PDU),类型为number
25    const shortMessage = await sms.createMessage(pdu, specification)
26    Logger.info(`${TAG}, createMessageCallback: shortMessage = ${JSON.stringify(shortMessage)}`)
27    return shortMessage
28  }
29
30  sendMessage(slotId, content, destinationHost, serviceCenter, destinationPort, handleSend, handleDelivery) {
31    Logger.info(`${TAG}, sendMessage start ${slotId} ${content} ${destinationHost} ${serviceCenter} ${destinationPort}`)
32    const options =
33      {
34        slotId: slotId,
35        content: content,
36        destinationHost: destinationHost,
37        serviceCenter: serviceCenter,
38        destinationPort: destinationPort,
39        sendCallback(err, data) {
40          Logger.info(`${TAG}, sendCallback: data = ${JSON.stringify(data)} err = ${JSON.stringify(err)}`)
41          handleSend(err, data)
42        },
43        deliveryCallback(err, data) {
44          Logger.info(`${TAG}, deliveryCallback: data = ${JSON.stringify(data)} err = ${JSON.stringify(err)}`)
45          handleDelivery(err, data)
46        }
47      }
48
49    sms.sendMessage(options)
50    Logger.info(`${TAG}, sendMessage end`)
51  }
52
53  async getDefaultSmsSlotId() {
54    const defaultSmsSlotId = await sms.getDefaultSmsSlotId()
55    Logger.info(`${TAG}, getDefaultSmsSlotId: defaultSmsSlotId = ${defaultSmsSlotId}`)
56    return defaultSmsSlotId
57  }
58
59  async setSmscAddr(slotId, smscAddr) {
60    const serviceCenter = await sms.setSmscAddr(slotId, smscAddr)
61    Logger.info(`${TAG}, setSmscAddr: serviceCenter = ${JSON.stringify(serviceCenter)}`)
62    return serviceCenter
63  }
64
65  async getSmscAddr(slotId) {
66    const serviceCenter = await sms.getSmscAddr(slotId)
67    Logger.info(`${TAG}, getSmscAddr: serviceCenter = ${JSON.stringify(serviceCenter)}`)
68    return serviceCenter
69  }
70}
71
72
73