How to Send and Receive Data Sms Messages

How to send and receive data SMS messages

I know this is 1 year old at time of my response, but I thought it could still help someone.

Receiving:

Bundle bundle = intent.getExtras(); 

String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++){
recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);

try {
data = recMsg.getUserData();
} catch (Exception e){

}
if (data!=null){
for(int index=0; index<data.length; ++index)
{
recMsgString += Character.toString((char)data[index]);
}
}

fromAddress = recMsg.getOriginatingAddress();
}

Setting up Receiver in Manifest:

<receiver android:name=".SMSReceiver"> 
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>

Sending:

String messageText = "message!"; 
short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);

Android: How to send and receive data through SMS messages

The correct way to do this would be by using sendDataMessage from SmsManager class.
Here's a little code (SMSSender):

SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendDataMessage(phoneNumber, null,
(short) myApplicationPort, messageString.getBytes(), sentIntent, deliveryIntent);

Here's another little code (SMSReceiver):

    Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];

// getting SMS information from PDU
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}

for (SmsMessage currentMessage : messages) {
if (!currentMessage.isStatusReportMessage()) {

String messageBody = currentMessage.getDisplayMessageBody();

byte[] messageByteArray = currentMessage.getPdu();

// skipping PDU header, keeping only message body
int x = 1 + messageByteArray[0] + 19 + 7;

// I'm not sure about this last line, as I'm not converting the bytes back to string, so test it out
String realMessage = new String(messageByteArray, x, messageByteArray.length-x);

Here's what you should add to your AndroidManifest.xml:

 <receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:host="localhost" />
<data android:port="12345" /><!-- this number should be the same as the `myApplicationPort` from above!!! -->
</intent-filter>
</receiver>

How to post data to a sms server (if there is no internet) and then send back from the Sms server to the web server?

Twilio developer evangelist here.

You can do this using Twilio in an Android application. You would do it by using Android APIs to send an SMS from the app using the device's SIM card (I believe this is possible, though I'm not an Android developer, I have seen it done before). You would send that SMS to a Twilio number that you had bought with your account.

When the Twilio number receives an incoming SMS, Twilio will take all the data about the SMS and package it up as an HTTP request that it sends to a URL you define. That URL should point at your web application and when your application receives the incoming HTTP request can read the data and store it in your database.

To understand more about the SMS to HTTP request flow, I recommend you follow the tutorial on receiving and responding to SMS messages.

send and receive SMS from server to webapp

I did some searching and found a similar service called SMSified, that seems to be priced more reasonably. There is a node module that hasn't been updated in 3 years, but that might give you a good starting point.

Another one that is explicitly free during development/testing is Tropo. This one has a more-recently-updated node module.

I haven't used either of these myself, but hopefully one of them should be what you're looking for.

Another option is to use an SMS gateway provided by the carriers. Typically you can send an email to [number]@[gateway-address]. For example, to send a text to 123-456-7890 on AT&T's network, you would simply send an email to 1234567890@txt.att.net. Take a look here for a list of common carrier's gateway addresses.

If you already have email integrated with your app, this might be very easy to implement. The only downside is that it would require users to specify their carrier before you can send them an SMS message.

within my app to send and receive text thru android sms api

Yes, you can. By using the SmsManager you can send the SMS from your own app. You can also set up listeners to intercept received messages and display them to the user. All this has been done by a lot of app developers.

Send and receive SMS inside my own application only, not in the native Message application in android using native SMS API

Here is what I had implemented and its working like exactly what i wanted.

After entering the phone number and Text message call this method .

private static final int MAX_SMS_MESSAGE_LENGTH = 160;
private static final int SMS_PORT = 8901;
private static final String SMS_DELIVERED = "SMS_DELIVERED";
private static final String SMS_SENT = "SMS_SENT";

private void sendSms(String phonenumber,String message) {

SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);

byte[] data = new byte[message.length()];

for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
{
data[index] = (byte)message.charAt(index);
}

manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);

}

private BroadcastReceiver sendreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String info = "Send information: ";

switch(getResultCode())
{
case Activity.RESULT_OK: info += "send successful"; break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: info += "send failed, generic failure"; break;
case SmsManager.RESULT_ERROR_NO_SERVICE: info += "send failed, no service"; break;
case SmsManager.RESULT_ERROR_NULL_PDU: info += "send failed, null pdu"; break;
case SmsManager.RESULT_ERROR_RADIO_OFF: info += "send failed, radio is off"; break;
}

Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();

}
};

private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String info = "Delivery information: ";

switch(getResultCode())
{
case Activity.RESULT_OK: info += "delivered"; break;
case Activity.RESULT_CANCELED: info += "not delivered"; break;
}

Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};

Your Receiver for messages should look like :

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class MySMSReceiver extends BroadcastReceiver {

String action,from,message;

@Override
public void onReceive(Context context, Intent intent) {

action=intent.getAction();

Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;

if(null != bundle)
{
String info = "Binary SMS from ";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];

byte[] data = null;

for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
info += msgs[i].getOriginatingAddress();
info += "\n*****BINARY MESSAGE*****\n";
from= msgs[i].getOriginatingAddress();
data = msgs[i].getUserData();

for(int index=0; index<data.length; ++index) {
info += Character.toString((char)data[index]);
message += Character.toString((char)data[index]);
}
}

}

Intent showMessage=new Intent(context, AlertMessage.class);
showMessage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
showMessage.putExtra("from", from);
showMessage.putExtra("message", message);
context.startActivity(showMessage);

}

}

I have created a simple activity AlertMessage.java to show received message.

The way I registered my broadcast receiver in Manifest :

 <receiver android:name=".MySMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>

The port mentioned here must be the same which we specified in method sendSMS() for sending the message.

UPDATE :

Github Repository of working project

https://github.com/pyus-13/MySMSSender



Related Topics



Leave a reply



Submit