1e41f4b71Sopenharmony_ci# @ohos.telephony.sms (短信服务) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci短信服务提供了管理短信的一些基础能力,包括创建、发送短信,获取发送短信的默认SIM卡槽ID,以及检查当前设备是否具备短信发送和接收能力等。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci>**说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci>本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 导入模块 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## sms.createMessage 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_cicreateMessage\(pdu: Array<number>, specification: string, callback: AsyncCallback\<ShortMessage\>\): void 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci根据协议数据单元(PDU)和指定的短信协议创建短信实例。使用callback异步回调。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**参数:** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 26e41f4b71Sopenharmony_ci| ------------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 27e41f4b71Sopenharmony_ci| pdu | Array<number> | 是 | 协议数据单元,从收到的信息中获取。 | 28e41f4b71Sopenharmony_ci| specification | string | 是 | 短信协议类型。<br/>- 3gpp:表示GSM/UMTS/LTE SMS<br/>- 3gpp2:表示CDMA SMS | 29e41f4b71Sopenharmony_ci| callback | AsyncCallback<[ShortMessage](#shortmessage)> | 是 | 获取短信实例的回调函数。 | 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**错误码:** 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 36e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 37e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 38e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 39e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 40e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 41e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**示例:** 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci```ts 46e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 47e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ciconst specification: string = '3gpp'; 50e41f4b71Sopenharmony_ci// 以数组的形式显示协议数据单元(PDU),类型为number。 51e41f4b71Sopenharmony_ciconst pdu: Array<number> = [0x01, 0x00, 0x05, 0x81, 0x01, 0x80, 0xF6, 0x00, 0x00, 0x05, 0xE8, 0x32, 0x9B, 0xFD, 0x06]; 52e41f4b71Sopenharmony_cisms.createMessage(pdu, specification, (err: BusinessError, data: sms.ShortMessage) => { 53e41f4b71Sopenharmony_ci console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 54e41f4b71Sopenharmony_ci}); 55e41f4b71Sopenharmony_ci``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci## sms.createMessage 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_cicreateMessage\(pdu: Array<number>, specification: string\): Promise\<ShortMessage\> 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci根据协议数据单元(PDU)和指定的短信协议创建短信实例。使用Promise异步回调。 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**参数:** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 69e41f4b71Sopenharmony_ci| ------------- | ------------------- | ---- | ------------------------------------------------------------ | 70e41f4b71Sopenharmony_ci| pdu | Array<number> | 是 | 协议数据单元,从收到的信息中获取。 | 71e41f4b71Sopenharmony_ci| specification | string | 是 | 短信协议类型。<br/>- 3gpp:表示GSM/UMTS/LTE SMS<br/>- 3gpp2:表示CDMA SMS | 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**返回值:** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci| 类型 | 说明 | 76e41f4b71Sopenharmony_ci| -------------------------------------------- | --------------------------------- | 77e41f4b71Sopenharmony_ci| Promise<[ShortMessage](#shortmessage)> | 以Promise形式返回创建的短信实例。 | 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**错误码:** 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 84e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 85e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 86e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 87e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 88e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 89e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**示例:** 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci```ts 94e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 95e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciconst specification: string = '3gpp'; 98e41f4b71Sopenharmony_ci// 以数组的形式显示协议数据单元(PDU),类型为number。 99e41f4b71Sopenharmony_ciconst pdu: Array<number> = [0x01, 0x00, 0x05, 0x81, 0x01, 0x80, 0xF6, 0x00, 0x00, 0x05, 0xE8, 0x32, 0x9B, 0xFD, 0x06]; 100e41f4b71Sopenharmony_cisms.createMessage(pdu, specification).then((data: sms.ShortMessage) => { 101e41f4b71Sopenharmony_ci console.log(`createMessage success, promise: data->${JSON.stringify(data)}`); 102e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 103e41f4b71Sopenharmony_ci console.error(`createMessage failed, promise: err->${JSON.stringify(err)}`); 104e41f4b71Sopenharmony_ci}); 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci## sms.sendMessage<sup>(deprecated)</sup> 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_cisendMessage\(options: SendMessageOptions\): void 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci发送短信。 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci> **说明:** 114e41f4b71Sopenharmony_ci> 115e41f4b71Sopenharmony_ci> 从 API version 6开始支持,从API version 10开始废弃。建议使用[sendShortMessage](#smssendshortmessage10)替代。 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.SEND_MESSAGES(该权限仅系统应用可申请) 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci**参数:** 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 124e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 125e41f4b71Sopenharmony_ci| options | [SendMessageOptions](#sendmessageoptions) | 是 | 发送短信的参数和回调,参考[SendMessageOptions](#sendmessageoptions)。 | 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**错误码:** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 132e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 133e41f4b71Sopenharmony_ci| 201 | Permission denied. | 134e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 135e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 136e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 137e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 138e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci**示例:** 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci```ts 143e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 144e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_cilet sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => { 147e41f4b71Sopenharmony_ci console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 148e41f4b71Sopenharmony_ci}; 149e41f4b71Sopenharmony_cilet deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => { 150e41f4b71Sopenharmony_ci console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 151e41f4b71Sopenharmony_ci}; 152e41f4b71Sopenharmony_cilet options: sms.SendMessageOptions = { 153e41f4b71Sopenharmony_ci slotId: 0, 154e41f4b71Sopenharmony_ci content: '短信内容', 155e41f4b71Sopenharmony_ci destinationHost: '+861xxxxxxxxxx', 156e41f4b71Sopenharmony_ci serviceCenter: '+861xxxxxxxxxx', 157e41f4b71Sopenharmony_ci destinationPort: 1000, 158e41f4b71Sopenharmony_ci sendCallback: sendCallback, 159e41f4b71Sopenharmony_ci deliveryCallback: deliveryCallback 160e41f4b71Sopenharmony_ci}; 161e41f4b71Sopenharmony_cisms.sendMessage(options); 162e41f4b71Sopenharmony_ci``` 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci## sms.sendShortMessage<sup>10+</sup> 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_cisendShortMessage\(options: SendMessageOptions, callback: AsyncCallback<void>\): void 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci发送短信。使用callback异步回调。 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.SEND_MESSAGES(该权限仅系统应用可申请) 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**参数:** 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 177e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ---------------------------------------- | 178e41f4b71Sopenharmony_ci| options | [SendMessageOptions](#sendmessageoptions) | 是 | 发送短信的参数和回调,参考[SendMessageOptions](#sendmessageoptions)。 | 179e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 发送短信的回调函数。 | 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**错误码:** 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 186e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 187e41f4b71Sopenharmony_ci| 201 | Permission denied. | 188e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 189e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 190e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 191e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 192e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci**示例:** 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci```ts 197e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 198e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_cilet sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => { 201e41f4b71Sopenharmony_ci console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 202e41f4b71Sopenharmony_ci}; 203e41f4b71Sopenharmony_cilet deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => { 204e41f4b71Sopenharmony_ci console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 205e41f4b71Sopenharmony_ci}; 206e41f4b71Sopenharmony_cilet options: sms.SendMessageOptions = { 207e41f4b71Sopenharmony_ci slotId: 0, 208e41f4b71Sopenharmony_ci content: '短信内容', 209e41f4b71Sopenharmony_ci destinationHost: '+861xxxxxxxxxx', 210e41f4b71Sopenharmony_ci serviceCenter: '+861xxxxxxxxxx', 211e41f4b71Sopenharmony_ci destinationPort: 1000, 212e41f4b71Sopenharmony_ci sendCallback: sendCallback, 213e41f4b71Sopenharmony_ci deliveryCallback: deliveryCallback 214e41f4b71Sopenharmony_ci}; 215e41f4b71Sopenharmony_cisms.sendShortMessage(options, (err: BusinessError) => { 216e41f4b71Sopenharmony_ci console.log(`callback: err->${JSON.stringify(err)}`); 217e41f4b71Sopenharmony_ci}); 218e41f4b71Sopenharmony_ci``` 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci## sms.sendShortMessage<sup>10+</sup> 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_cisendShortMessage\(options: SendMessageOptions\): Promise<void> 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci发送短信。使用Promise异步回调。 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.SEND_MESSAGES(该权限仅系统应用可申请) 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci**参数:** 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 233e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ---------------------------------------- | 234e41f4b71Sopenharmony_ci| options | [SendMessageOptions](#sendmessageoptions) | 是 | 发送短信的参数和回调,参考[SendMessageOptions](#sendmessageoptions)。 | 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci**返回值:** 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci| 类型 | 说明 | 239e41f4b71Sopenharmony_ci| --------------- | ------------------------------------------------------------ | 240e41f4b71Sopenharmony_ci| Promise<void> | 以Promise形式返回发送短信的结果。 | 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**错误码:** 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 247e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 248e41f4b71Sopenharmony_ci| 201 | Permission denied. | 249e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 250e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 251e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 252e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 253e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**示例:** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci```ts 258e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 259e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_cilet sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => { 262e41f4b71Sopenharmony_ci console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 263e41f4b71Sopenharmony_ci}; 264e41f4b71Sopenharmony_cilet deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => { 265e41f4b71Sopenharmony_ci console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 266e41f4b71Sopenharmony_ci}; 267e41f4b71Sopenharmony_cilet options: sms.SendMessageOptions = { 268e41f4b71Sopenharmony_ci slotId: 0, 269e41f4b71Sopenharmony_ci content: '短信内容', 270e41f4b71Sopenharmony_ci destinationHost: '+861xxxxxxxxxx', 271e41f4b71Sopenharmony_ci serviceCenter: '+861xxxxxxxxxx', 272e41f4b71Sopenharmony_ci destinationPort: 1000, 273e41f4b71Sopenharmony_ci sendCallback: sendCallback, 274e41f4b71Sopenharmony_ci deliveryCallback: deliveryCallback 275e41f4b71Sopenharmony_ci}; 276e41f4b71Sopenharmony_cilet promise = sms.sendShortMessage(options); 277e41f4b71Sopenharmony_cipromise.then(() => { 278e41f4b71Sopenharmony_ci console.log(`sendShortMessage success`); 279e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 280e41f4b71Sopenharmony_ci console.error(`sendShortMessage failed, promise: err->${JSON.stringify(err)}`); 281e41f4b71Sopenharmony_ci}); 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci``` 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci## sms.getDefaultSmsSlotId<sup>7+</sup> 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_cigetDefaultSmsSlotId\(callback: AsyncCallback<number>\): void 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci获取发送短信的默认SIM卡槽ID。使用callback异步回调。 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci**参数:** 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 297e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ---------------------------------------- | 298e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | 是 | 获取发送短信的默认SIM卡槽ID的回调函数。<br/>- 0:卡槽1<br/>- 1:卡槽2 | 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**示例:** 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci```ts 303e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 304e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_cisms.getDefaultSmsSlotId((err: BusinessError, data: number) => { 307e41f4b71Sopenharmony_ci console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 308e41f4b71Sopenharmony_ci}); 309e41f4b71Sopenharmony_ci``` 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci## sms.getDefaultSmsSlotId<sup>7+</sup> 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_cigetDefaultSmsSlotId\(\): Promise<number> 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci获取发送短信的默认SIM卡槽ID。使用Promise异步回调。 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci**返回值:** 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci| 类型 | 说明 | 323e41f4b71Sopenharmony_ci| --------------- | ------------------------------------------------------------ | 324e41f4b71Sopenharmony_ci| Promise<number> | 以Promise形式返回发送短信的默认SIM卡:<br/>- 0:卡槽1<br/>- 1:卡槽2 | 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci**示例:** 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci```ts 329e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 330e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_cisms.getDefaultSmsSlotId().then((data: number) => { 333e41f4b71Sopenharmony_ci console.log(`getDefaultSmsSlotId success, promise: data->${JSON.stringify(data)}`); 334e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 335e41f4b71Sopenharmony_ci console.error(`getDefaultSmsSlotId failed, promise: err->${JSON.stringify(err)}`); 336e41f4b71Sopenharmony_ci}); 337e41f4b71Sopenharmony_ci``` 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci## sms.hasSmsCapability<sup>7+</sup> 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_cihasSmsCapability\(\): boolean 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci检查当前设备是否具备短信发送和接收能力,该方法是同步方法。 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**返回值:** 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci| 类型 | 说明 | 350e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 351e41f4b71Sopenharmony_ci| boolean | - true:设备具备短信发送和接收能力。<br/>- false:设备不具备短信发送和接收能力。 | 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci```ts 354e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_cilet result = sms.hasSmsCapability(); 357e41f4b71Sopenharmony_ciconsole.log(`hasSmsCapability: ${JSON.stringify(result)}`); 358e41f4b71Sopenharmony_ci``` 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci## sms.getDefaultSmsSimId<sup>10+</sup> 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_cigetDefaultSmsSimId\(callback: AsyncCallback<number>\): void 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci获取发送短信的默认SIM卡ID。使用callback异步回调。 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci**参数:** 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 371e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ---------------------------------------- | 372e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | 是 | 获取默认短信SIM的SIM ID的回调函数。<br/>与SIM卡绑定,从1开始递增。<br/>无卡时返回值为-1。 | 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci**错误码:** 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 379e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 380e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 381e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 382e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 383e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 384e41f4b71Sopenharmony_ci| 8300004 | Do not have sim card. | 385e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 386e41f4b71Sopenharmony_ci| 8301001 | SIM card is not activated. | 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci**示例:** 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci```ts 391e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 392e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_cisms.getDefaultSmsSimId((err: BusinessError, data: number) => { 395e41f4b71Sopenharmony_ci console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 396e41f4b71Sopenharmony_ci}); 397e41f4b71Sopenharmony_ci``` 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci## sms.getDefaultSmsSimId<sup>10+</sup> 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_cigetDefaultSmsSimId\(\): Promise<number> 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci获取发送短信的默认SIM卡ID。使用Promise异步回调。 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci**返回值:** 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci| 类型 | 说明 | 411e41f4b71Sopenharmony_ci| --------------- | ------------------------------------------------------------ | 412e41f4b71Sopenharmony_ci| Promise<number> | 以Promise形式返回发送短信的默认SIM卡ID:<br/>与SIM卡绑定,从1开始递增。<br/>无卡时返回值为-1。 | 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci**错误码:** 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 419e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 420e41f4b71Sopenharmony_ci| 8300001 | Invalid parameter value. | 421e41f4b71Sopenharmony_ci| 8300002 | Operation failed. Cannot connect to service. | 422e41f4b71Sopenharmony_ci| 8300003 | System internal error. | 423e41f4b71Sopenharmony_ci| 8300004 | Do not have sim card. | 424e41f4b71Sopenharmony_ci| 8300999 | Unknown error code. | 425e41f4b71Sopenharmony_ci| 8301001 | SIM card is not activated. | 426e41f4b71Sopenharmony_ci 427e41f4b71Sopenharmony_ci**示例:** 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci```ts 430e41f4b71Sopenharmony_ciimport { sms } from '@kit.TelephonyKit'; 431e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_cilet promise = sms.getDefaultSmsSimId(); 434e41f4b71Sopenharmony_cipromise.then((data: number) => { 435e41f4b71Sopenharmony_ci console.log(`getDefaultSmsSimId success, promise: data->${JSON.stringify(data)}`); 436e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 437e41f4b71Sopenharmony_ci console.error(`getDefaultSmsSimId failed, promise: err->${JSON.stringify(err)}`); 438e41f4b71Sopenharmony_ci}); 439e41f4b71Sopenharmony_ci``` 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci## ShortMessage 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci短信实例。 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 449e41f4b71Sopenharmony_ci| ------------------------ | --------------------------------------- | ---- | ------------------------------------------------------------ | 450e41f4b71Sopenharmony_ci| hasReplyPath | boolean | 是 | 收到的短信是否包含“TP-Reply-Path”,默认为false。<br/>“TP-Reply-Path”:设备根据发送SMS消息的短消息中心进行回复。 | 451e41f4b71Sopenharmony_ci| isReplaceMessage | boolean | 是 | 收到的短信是否为“替换短信”,默认为false。<br/>“替换短信”有关详细信息,参见 [“3GPP TS 23.040 9.2.3.9”](https://www.3gpp.org/ftp/specs/archive/23_series/23.040)。 | 452e41f4b71Sopenharmony_ci| isSmsStatusReportMessage | boolean | 是 | 当前消息是否为“短信状态报告”,默认为false。<br/>“短信状态报告”是一种特定格式的短信,被用来从Service Center到Mobile Station传送状态报告。| 453e41f4b71Sopenharmony_ci| messageClass | [ShortMessageClass](#shortmessageclass) | 是 | 短信类型。 | 454e41f4b71Sopenharmony_ci| pdu | Array<number> | 是 | SMS消息中的协议数据单元 (PDU)。 | 455e41f4b71Sopenharmony_ci| protocolId | number | 是 | 发送短信时使用的协议标识。 | 456e41f4b71Sopenharmony_ci| scAddress | string | 是 | 短消息服务中心(SMSC)地址。 | 457e41f4b71Sopenharmony_ci| scTimestamp | number | 是 | SMSC时间戳。 | 458e41f4b71Sopenharmony_ci| status | number | 是 | SMS-STATUS-REPORT消息中的短信状态指示短信服务中心(SMSC)发送的短信状态。 | 459e41f4b71Sopenharmony_ci| visibleMessageBody | string | 是 | 短信正文。 | 460e41f4b71Sopenharmony_ci| visibleRawAddress | string | 是 | 发送者地址。 | 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci## ShortMessageClass 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci短信类型。 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 470e41f4b71Sopenharmony_ci| ---------------- | ---- | ---------------------------------------- | 471e41f4b71Sopenharmony_ci| UNKNOWN | 0 | 未知类型。 | 472e41f4b71Sopenharmony_ci| INSTANT_MESSAGE | 1 | 即时消息,收到后立即显示。 | 473e41f4b71Sopenharmony_ci| OPTIONAL_MESSAGE | 2 | 存储在设备或SIM卡上的短信。 | 474e41f4b71Sopenharmony_ci| SIM_MESSAGE | 3 | 包含SIM卡信息的短信,需要存储在SIM卡中。 | 475e41f4b71Sopenharmony_ci| FORWARD_MESSAGE | 4 | 要转发到另一台设备的短信。 | 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ci## SendMessageOptions 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci发送短信的参数和回调。根据SendMessageOptions中的可选参数content的值判断短信类型。 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 485e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 486e41f4b71Sopenharmony_ci| slotId | number | 是 | 用于发送短信的SIM卡槽ID:<br/>- 0:卡槽1<br/>- 1:卡槽2 | 487e41f4b71Sopenharmony_ci| destinationHost | string | 是 | 短信的发送地址。 | 488e41f4b71Sopenharmony_ci| content | string \| Array<number> | 是 | 如果内容是字符串,则这是一条文本短信。如果内容是字节数组,则这是一条数据短信。 | 489e41f4b71Sopenharmony_ci| serviceCenter | string | 否 | 短信中心地址。默认使用SIM卡中的短信中心地址。 | 490e41f4b71Sopenharmony_ci| destinationPort | number | 否 | 如果发送数据消息,destinationPort 是必需的。否则是可选的。 | 491e41f4b71Sopenharmony_ci| sendCallback | AsyncCallback<[ISendShortMessageCallback](#isendshortmessagecallback)> | 否 | 短信发送结果回调,返回短信发送的结果,参考[ISendShortMessageCallback](#isendshortmessagecallback)。发送数据短信时,此项必填。| 492e41f4b71Sopenharmony_ci| deliveryCallback | AsyncCallback<[IDeliveryShortMessageCallback](#ideliveryshortmessagecallback)> | 否 | 短信送达结果回调,返回短信递送报告,参考[IDeliveryShortMessageCallback](#ideliveryshortmessagecallback)。发送数据短信时,此项必填。| 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci## ISendShortMessageCallback 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci回调实例。返回短信发送结果、存储已发送短信的URI和是否为长短信的最后一部分。 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 503e41f4b71Sopenharmony_ci| ---------- | ------------------------------- | ---- | ----------------------------------------------------------------------------------------- | 504e41f4b71Sopenharmony_ci| isLastPart | boolean | 是 | 指定这是否是长短信的最后一部分。true表示这是长短信的最后一部分,false表示不是。默认为false。 | 505e41f4b71Sopenharmony_ci| result | [SendSmsResult](#sendsmsresult) | 是 | 短信发送结果。 | 506e41f4b71Sopenharmony_ci| url | string | 是 | 存储发送短信的URI。 | 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci## IDeliveryShortMessageCallback 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci回调实例,返回短信送达报告。 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 516e41f4b71Sopenharmony_ci| ---- | ------------------- | ---- | -------------- | 517e41f4b71Sopenharmony_ci| pdu | Array<number> | 是 | 短信送达报告。 | 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci## SendSmsResult 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci短信发送结果。 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.SmsMms 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 527e41f4b71Sopenharmony_ci| ------------------------------------ | ---- | ------------------------------------------------------ | 528e41f4b71Sopenharmony_ci| SEND_SMS_SUCCESS | 0 | 发送短信成功。 | 529e41f4b71Sopenharmony_ci| SEND_SMS_FAILURE_UNKNOWN | 1 | 发送短信失败,原因未知。 | 530e41f4b71Sopenharmony_ci| SEND_SMS_FAILURE_RADIO_OFF | 2 | 发送短信失败,原因为调制解调器关机。 | 531e41f4b71Sopenharmony_ci| SEND_SMS_FAILURE_SERVICE_UNAVAILABLE | 3 | 发送短信失败,原因为网络不可用、不支持发送或接收短信。 | 532