openharmony 鸿蒙 js-apis-sms

2025-06-12 浏览 (1)

@ohos.telephony.sms (SMS)

The sms module provides basic SMS management functions. With the APIs provided by this module, you can create and send SMS messages, and obtain the ID of the default SIM card used to send and receive SMS messages, and check whether the current device can send and receive SMS messages.

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

import { sms } from '@kit.TelephonyKit';

sms.createMessage

createMessage(pdu: Array<number>, specification: string, callback: AsyncCallback<ShortMessage>): void

Creates an SMS instance based on the protocol data unit (PDU) and specified SMS protocol. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
pduArray<number>YesProtocol data unit, which is obtained from the received SMS message.
specificationstringYesSMS protocol type.
- 3gpp: GSM/UMTS/LTE SMS
- 3gpp2: CDMA SMS
callbackAsyncCallback<ShortMessage>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { sms } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

const specification: string = '3gpp';
// Display PDUs in array format. The type is number.
const pdu: Array<number> = [0x01, 0x00, 0x05, 0x81, 0x01, 0x80, 0xF6, 0x00, 0x00, 0x05, 0xE8, 0x32, 0x9B, 0xFD, 0x06];
sms.createMessage(pdu, specification, (err: BusinessError, data: sms.ShortMessage) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

sms.createMessage

createMessage(pdu: Array<number>, specification: string): Promise<ShortMessage>

Creates an SMS instance based on the protocol data unit (PDU) and specified SMS protocol. This API uses a promise to return the result.

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
pduArray<number>YesProtocol data unit, which is obtained from the received SMS message.
specificationstringYesSMS protocol type.
- 3gpp: GSM/UMTS/LTE SMS
- 3gpp2: CDMA SMS

Return value

TypeDescription
Promise<ShortMessage>Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { sms } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

const specification: string = '3gpp';
// Display PDUs in array format. The type is number.
const pdu: Array<number> = [0x01, 0x00, 0x05, 0x81, 0x01, 0x80, 0xF6, 0x00, 0x00, 0x05, 0xE8, 0x32, 0x9B, 0xFD, 0x06];
sms.createMessage(pdu, specification).then((data: sms.ShortMessage) => {
    console.log(`createMessage success, promise: data->${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
    console.error(`createMessage failed, promise: err->${JSON.stringify(err)}`);
});

sms.sendMessage(deprecated)

sendMessage(options: SendMessageOptions): void

Sends an SMS message.

NOTE

This API is supported since API version 6 and deprecated since API version 10. You are advised to use sendShortMessage.

Required permissions: ohos.permission.SEND_MESSAGES (available only for system applications)

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
optionsSendMessageOptionsYesOptions (including the callback) for sending SMS messages. For details, see SendMessageOptions.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { sms } from '@kit.TelephonyKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => {
    console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 
};
let deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => {
    console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 
};
let options: sms.SendMessageOptions = {
    slotId: 0,
    content: 'SMS message content';
    destinationHost: '+861xxxxxxxxxx',
    serviceCenter: '+861xxxxxxxxxx',
    destinationPort: 1000,
    sendCallback: sendCallback,
    deliveryCallback: deliveryCallback
};
sms.sendMessage(options);

sms.sendShortMessage10+

sendShortMessage(options: SendMessageOptions, callback: AsyncCallback<void>): void

Sends an SMS message. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.SEND_MESSAGES (available only for system applications)

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
optionsSendMessageOptionsYesOptions (including the callback) for sending SMS messages. For details, see SendMessageOptions.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { sms } from '@kit.TelephonyKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => {
    console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
};
let deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => {
    console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
};
let options: sms.SendMessageOptions = {
    slotId: 0,
    content: 'SMS message content';
    destinationHost: '+861xxxxxxxxxx',
    serviceCenter: '+861xxxxxxxxxx',
    destinationPort: 1000,
    sendCallback: sendCallback,
    deliveryCallback: deliveryCallback
};
sms.sendShortMessage(options, (err: BusinessError) => {
    console.log(`callback: err->${JSON.stringify(err)}`);
});

sms.sendShortMessage10+

sendShortMessage(options: SendMessageOptions): Promise<void>

Sends an SMS message. This API uses a promise to return the result.

Required permissions: ohos.permission.SEND_MESSAGES (available only for system applications)

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
optionsSendMessageOptionsYesOptions (including the callback) for sending SMS messages. For details, see SendMessageOptions.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300999Unknown error code.

Example

import { sms } from '@kit.TelephonyKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

let sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => {
    console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
};
let deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => {
    console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
};
let options: sms.SendMessageOptions = {
    slotId: 0,
    content: 'SMS message content';
    destinationHost: '+861xxxxxxxxxx',
    serviceCenter: '+861xxxxxxxxxx',
    destinationPort: 1000,
    sendCallback: sendCallback,
    deliveryCallback: deliveryCallback
};
let promise = sms.sendShortMessage(options);
promise.then(() => {
    console.log(`sendShortMessage success`);
}).catch((err: BusinessError) => {
    console.error(`sendShortMessage failed, promise: err->${JSON.stringify(err)}`);
});

sms.getDefaultSmsSlotId7+

getDefaultSmsSlotId(callback: AsyncCallback<number>): void

Obtains the default slot ID of the SIM card used to send SMS messages. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the result.
- 0: card slot 1.
- 1: card slot 2

Example

import { sms } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

sms.getDefaultSmsSlotId((err: BusinessError, data: number) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

sms.getDefaultSmsSlotId7+

getDefaultSmsSlotId(): Promise<number>

Obtains the default slot ID of the SIM card used to send SMS messages. This API uses a promise to return the result.

System capability: SystemCapability.Telephony.SmsMms

Return value

TypeDescription
Promise<number>Promise used to return the result.
- 0: card slot 1.
- 1: card slot 2

Example

import { sms } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

sms.getDefaultSmsSlotId().then((data: number) => {
    console.log(`getDefaultSmsSlotId success, promise: data->${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
    console.error(`getDefaultSmsSlotId failed, promise: err->${JSON.stringify(err)}`);
});

sms.hasSmsCapability7+

hasSmsCapability(): boolean

Checks whether the current device can send and receive SMS messages. This API works in synchronous mode.

System capability: SystemCapability.Telephony.SmsMms

Return value

TypeDescription
boolean- true: The device can send and receive SMS messages.
- false: The device cannot send or receive SMS messages.
import { sms } from '@kit.TelephonyKit';

let result = sms.hasSmsCapability(); 
console.log(`hasSmsCapability: ${JSON.stringify(result)}`);

sms.getDefaultSmsSimId10+

getDefaultSmsSimId(callback: AsyncCallback<number>): void

Obtains the default ID of the SIM card used to send SMS messages. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Telephony.SmsMms

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the result.
The return value is bound to the SIM card and increases from 1.
The return value is -1 if no SIM card is detected.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300004Do not have sim card.
8300999Unknown error code.
8301001SIM card is not activated.

Example

import { sms } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

sms.getDefaultSmsSimId((err: BusinessError, data: number) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

sms.getDefaultSmsSimId10+

getDefaultSmsSimId(): Promise<number>

Obtains the default ID of the SIM card used to send SMS messages. This API uses a promise to return the result.

System capability: SystemCapability.Telephony.SmsMms

Return value

TypeDescription
Promise<number>Promise used to return the result.
The return value is bound to the SIM card and increases from 1.
The return value is -1 if no SIM card is detected.

Error codes

For details about the error codes, seeohos.telephony (Telephony) Error Codes.

IDError Message
8300001Invalid parameter value.
8300002Operation failed. Cannot connect to service.
8300003System internal error.
8300004Do not have sim card.
8300999Unknown error code.
8301001SIM card is not activated.

Example

import { sms } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';

let promise = sms.getDefaultSmsSimId();
promise.then((data: number) => {
    console.log(`getDefaultSmsSimId success, promise: data->${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
    console.error(`getDefaultSmsSimId failed, promise: err->${JSON.stringify(err)}`);
});

ShortMessage

Defines an SMS message instance.

System capability: SystemCapability.Telephony.SmsMms

NameTypeMandatoryDescription
hasReplyPathbooleanYesWhether the received SMS contains TP-Reply-Path. The default value is false.
TP-Reply-Path: The device returns a response based on the SMSC that sends the SMS message.
isReplaceMessagebooleanYesWhether the received SMS message is a replace short message. The default value is false.
For details, see 3GPP TS 23.040 9.2.3.9.
isSmsStatusReportMessagebooleanYesWhether the received SMS message is an SMS delivery report. The default value is false.
SMS delivery report: a message sent from the SMSC to show the current status of the SMS message you delivered.
messageClassShortMessageClassYesEnumerates SMS message types.
pduArray<number>YesPDU in the SMS message.
protocolIdnumberYesProtocol identifier used for delivering the SMS message.
scAddressstringYesSMSC address.
scTimestampnumberYesSMSC timestamp.
statusnumberYesSMS message status sent by the SMSC in the SMS-STATUS-REPORT message.
visibleMessageBodystringYesSMS message body.
visibleRawAddressstringYesSender address.

ShortMessageClass

Enumerates SMS message types.

System capability: SystemCapability.Telephony.SmsMms

NameValueDescription
UNKNOWN0Unknown type.
INSTANT_MESSAGE1Instant message, which is displayed immediately after being received.
OPTIONAL_MESSAGE2Message stored in the device or SIM card.
SIM_MESSAGE3Message containing SIM card information, which is to be stored in the SIM card.
FORWARD_MESSAGE4Message to be forwarded to another device.

SendMessageOptions

Provides the options (including callbacks) for sending SMS messages. For example, you can specify the SMS message type by the optional parameter content.

System capability: SystemCapability.Telephony.SmsMms

NameTypeMandatoryDescription
slotIdnumberYesSlot ID of the SIM card used for sending SMS messages.
- 0: card slot 1.
- 1: card slot 2
destinationHoststringYesDestination address of the SMS message.
contentstring |Array<number>YesSMS message type. If the content is composed of character strings, the SMS message is a text message. If the content is composed of byte arrays, the SMS message is a data message.
serviceCenterstringNoSMSC address. By default, the SMSC address in the SIM card is used.
destinationPortnumberNoDestination port of the SMS message. This field is mandatory only for a data message. Otherwise, it is optional.
sendCallbackAsyncCallback<ISendShortMessageCallback>NoCallback used to return the SMS message sending result. For details, see ISendShortMessageCallback. This parameter is mandatory for sending an SMS message.
deliveryCallbackAsyncCallback<IDeliveryShortMessageCallback>NoCallback used to return the SMS message delivery report. For details, see IDeliveryShortMessageCallback. This parameter is mandatory for sending an SMS message.

ISendShortMessageCallback

Provides the callback for the SMS message sending result. It consists of three parts: SMS message sending result, URI for storing the sent SMS message, and whether the SMS message is the last part of a long SMS message.

System capability: SystemCapability.Telephony.SmsMms

NameTypeMandatoryDescription
isLastPartbooleanYesWhether this SMS message is the last part of a long SMS message. The value true indicates that this SMS message is the last part of a long SMS message, and value false indicates the opposite. The default value is false.
resultSendSmsResultYesSMS message sending result.
urlstringYesURI for storing the sent SMS message.

IDeliveryShortMessageCallback

Provides the callback for the SMS message delivery report.

System capability: SystemCapability.Telephony.SmsMms

NameTypeMandatoryDescription
pduArray<number>YesSMS message delivery report.

SendSmsResult

Enumerates SMS message sending results.

System capability: SystemCapability.Telephony.SmsMms

NameValueDescription
SEND_SMS_SUCCESS0The SMS message is sent successfully.
SEND_SMS_FAILURE_UNKNOWN1Failed to send the SMS message due to an unknown reason.
SEND_SMS_FAILURE_RADIO_OFF2Failed to send the SMS message because the modem is shut down.
SEND_SMS_FAILURE_SERVICE_UNAVAILABLE3Failed to send the SMS message because the network is unavailable or SMS message sending or receiving is not supported.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Telephony Kit

harmony 鸿蒙Telephony_NetworkState

harmony 鸿蒙Telephony Error Codes

harmony 鸿蒙js-apis-call-sys

harmony 鸿蒙@ohos.telephony.call (Call)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management) (System API)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management)

harmony 鸿蒙@ohos.telephony.observer (Observer) (System API)

harmony 鸿蒙@ohos.telephony.observer (Observer)

harmony 鸿蒙@ohos.telephony.radio (Radio) (System API)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/JeeL8E