Creating PDUs for Android that Work with SmsMessage.createFromPdu() (GSM 3GPP)
This article will guide you through creating PDUs (Protocol Data Units) for SMS messages that can be used with the SmsMessage.createFromPdu()
method in Android applications, adhering to the GSM 3GPP standards.
Understanding PDUs
A PDU is a binary representation of an SMS message. It contains information like:
- SMS Service Center Address (SC Address): The address of the network’s SMS center.
- Originating Address (OA): The phone number of the sender.
- Destination Address (DA): The phone number of the recipient.
- Message Content: The actual text of the SMS message.
- Other Parameters: Information about the message type, validity period, and other optional features.
Structure of a PDU
PDUs are formatted according to GSM 3GPP standards. The structure is typically a concatenation of:
- SMSC Address: The address of the SMS center. This is usually a phone number but can be an alphanumeric string.
- Protocol Identifier: Identifies the type of message (e.g., SMS-DELIVER, SMS-SUBMIT).
- Message Reference: Used for tracking and referencing messages.
- Data Coding Scheme: Specifies the character encoding used (e.g., GSM 7-bit, Unicode).
- Validity Period: Specifies how long the message remains valid for delivery.
- Message Content: The actual text of the SMS message.
Creating PDUs in Android
You can create PDUs in your Android application using a combination of Java byte manipulation and the SmsMessage.createFromPdu()
method.
1. Define the SMS Service Center (SC Address)
You’ll need to know the SC address of your network. You can typically find this information in your device settings or by consulting your carrier’s documentation.
2. Create a PDU Buffer
Initialize a byte array to hold the PDU data. The size of this array will depend on the length of your message and the specific PDU structure you’re implementing.
3. Construct the PDU
Fill the PDU buffer with the necessary bytes according to the GSM 3GPP PDU format. Here’s a general outline:
byte[] pdu = new byte[pduSize]; // Fill in SMSC Address // Fill in Protocol Identifier // Fill in Message Reference // Fill in Data Coding Scheme // Fill in Validity Period // Fill in Message Content
4. Use SmsMessage.createFromPdu()
Use the SmsMessage.createFromPdu()
method to convert the constructed PDU byte array into an SmsMessage
object. You can then extract information like sender, recipient, and message content from the SmsMessage
object.
SmsMessage smsMessage = SmsMessage.createFromPdu(pdu);
Example: Sending a Simple SMS Message
This example demonstrates sending a simple SMS message using the SmsManager
class in Android.
import android.telephony.SmsManager; public class SendSMS { public static void sendSMS(String phoneNumber, String message) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNumber, null, message, null, null); } public static void main(String[] args) { String phoneNumber = "1234567890"; String message = "Hello from Android!"; sendSMS(phoneNumber, message); } }
Important Considerations
- PDU Encoding: Ensure you use the correct encoding (GSM 7-bit, Unicode) when building your PDU to ensure compatibility with the
SmsMessage.createFromPdu()
method. - SMSC Address: Always verify the correct SC address for your network.
- Permissions: Your Android application will require the “android.permission.SEND_SMS” permission to send SMS messages.
Conclusion
Creating PDUs for SMS messages requires understanding the GSM 3GPP standards and the specific PDU structure. With proper knowledge and implementation, you can build Android applications that efficiently create and send SMS messages.