0

Right now I can receive sms with the next code:

override fun onReceive(c: Context, i: Intent) {
        val bundle = intent.extras
        if (bundle != null && intent.action == SMS_RECEIVED) {
            var currentMessage: SmsMessage
            var msgs: Array<SmsMessage>? = null
            var pdusObj: Array<Any>? = null
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent)
                currentMessage = msgs!![0]
            } else {
                pdusObj = bundle.get("pdus") as Array<Any>
                currentMessage = SmsMessage.createFromPdu(pdusObj[0] as ByteArray)
            }
            mPhoneNumb = currentMessage.displayOriginatingAddress
}

I am wondered if there any way I can make some kind of reverse: create an intent(with my phone number and text message) for incoming sms emulation.

Is there any way to do this?

UPDATE:

thanks to MikeM find this solution:Create PDU for Android that works with SmsMessage.createFromPdu() (GSM 3gpp)

but I still can't send fake sms to my broadcast receiver, for this time I get crash with next error:

SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED from pid=14860, uid=10621

but i already has that permission! here is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.test.test">

    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.VIBRATE"/>

<application
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver android:name=".receiver.SmsReceiver">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
</application>
</manifest>

and here is my class wich must create and send fake sms to my receiver:

public class SendTestSms {

    public void createFakeSms(Context context, String sender,
                                      String body) {
        byte[] pdu = null;
        byte[] scBytes = PhoneNumberUtils
                .networkPortionToCalledPartyBCD("0000000000");
        byte[] senderBytes = PhoneNumberUtils
                .networkPortionToCalledPartyBCD(sender);
        int lsmcs = scBytes.length;
        byte[] dateBytes = new byte[7];
        Calendar calendar = new GregorianCalendar();
        dateBytes[0] = reverseByte((byte) (calendar.get(Calendar.YEAR)));
        dateBytes[1] = reverseByte((byte) (calendar.get(Calendar.MONTH) + 1));
        dateBytes[2] = reverseByte((byte) (calendar.get(Calendar.DAY_OF_MONTH)));
        dateBytes[3] = reverseByte((byte) (calendar.get(Calendar.HOUR_OF_DAY)));
        dateBytes[4] = reverseByte((byte) (calendar.get(Calendar.MINUTE)));
        dateBytes[5] = reverseByte((byte) (calendar.get(Calendar.SECOND)));
        dateBytes[6] = reverseByte((byte) ((calendar.get(Calendar.ZONE_OFFSET) + calendar
                .get(Calendar.DST_OFFSET)) / (60 * 1000 * 15)));
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            bo.write(lsmcs);
            bo.write(scBytes);
            bo.write(0x04);
            bo.write((byte) sender.length());
            bo.write(senderBytes);
            bo.write(0x00);
            bo.write(0x00); // encoding: 0 for default 7bit
            bo.write(dateBytes);
            try {
                String sReflectedClassName = "com.android.internal.telephony.GsmAlphabet";
                Class cReflectedNFCExtras = Class.forName(sReflectedClassName);
                Method stringToGsm7BitPacked = cReflectedNFCExtras.getMethod(
                        "stringToGsm7BitPacked", new Class[] { String.class });
                stringToGsm7BitPacked.setAccessible(true);
                byte[] bodybytes = (byte[]) stringToGsm7BitPacked.invoke(null,
                        body);
                bo.write(bodybytes);
            } catch (Exception e) {
                e.printStackTrace();
            }

            pdu = bo.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(context, SmsReceiver.class);
        intent.setClassName("com.android.mms",
                "com.android.mms.transaction.SmsReceiverService");
        intent.setAction("android.provider.Telephony.SMS_RECEIVED");
        intent.putExtra("pdus", new Object[] { pdu });
        intent.putExtra("format", "3gpp");
        context.sendBroadcast(intent);
    }

    private static byte reverseByte(byte b) {
        return (byte) ((b & 0xF0) >> 4 | (b & 0x0F) << 4);
    }
}

any ideas why my permission doesn't matter for sending broadcast?

Community
  • 1
  • 1
Andriy Antonov
  • 1,360
  • 2
  • 15
  • 29
  • You can, but you would only be able to send it to your own Receiver. – Mike M. Feb 02 '17 at 12:47
  • this is exactly what i need to do :) – Andriy Antonov Feb 02 '17 at 13:11
  • [This post](http://stackoverflow.com/a/12338541) has an example of creating the necessary PDUs, but you'd have to replace the `Intent` and `startService()` call there with an `Intent` targeting your Receiver, and a `sendBroadcast()` call. There are other ways of testing your Receiver, though, if that's all you're trying to do. On an emulator, you can [use telnet](http://stackoverflow.com/a/4325836), or [send from one to another](http://stackoverflow.com/a/6425565), or [use a built-in tool](http://stackoverflow.com/a/38897485). – Mike M. Feb 02 '17 at 13:36
  • Full disclosure: I've never tried any of these things. I have unlimited messaging, so I just send myself SMS messages for testing. – Mike M. Feb 02 '17 at 13:36
  • thanks! i will try the first one, cos i need to test receiving sms from mobile (without emulator). – Andriy Antonov Feb 02 '17 at 14:26
  • @MikeM, please take a look on my updated question. will be glad any ideas and suggestion! – Andriy Antonov Feb 04 '17 at 19:14
  • Yeah, you can't broadcast the `SMS_RECEIVED` action; you can only get it. That's why you can only target your own Receiver. You'd have to remove the `setAction()` call, or change the action to something else. And remove `setClassName()`, too. It's not really a great test, since the only thing you're really testing is whether you can create and decode PDUs. That's kinda why I mentioned that first thing. :-) – Mike M. Feb 04 '17 at 20:17

0 Answers0