I am creating an app on android that needs to be able to allow android devices to send SMS data back and forth with SmsManager.sendDataMessage(). For some reason when I try to send data this way my sms receiver doesn't ever get anything. What am I missing?
Here is the code to send the data:
SmsManager sm = SmsManager.getDefault();
sm.sendDataMessage(toPhone, null, (short)8901, message.getBytes(), null, null);
Here is the code to my sms receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class DataReceiver extends BroadcastReceiver
{
private String myMessage;
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle == null) return;
Object[] objs = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[objs.length];
for (int i = 0; i < objs.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) objs[i]);
}
for (SmsMessage currMessage : messages)
{
if (!currMessage.isStatusReportMessage())
{
String messageBody = currMessage.getDisplayMessageBody();
byte[] messageBytes = currMessage.getPdu();
int x = 1 + messageBytes[0] + 19 + 7;
myMessage = new String(messageBytes, x, messageBytes.length - x);
System.out.printf(myMessage);
}
}
}
}
Here is what I have in my manifest for the receiver:
<receiver android:name=".DataReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:host="localhost" />
<data android:port="8901" />
</intent-filter>
</receiver>
And here are the permissions I am using:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />