创建PDU为Android与SmsMessage.createFromPdu()(GSM 3GPP)的工作原理 [英] Create PDU for Android that works with SmsMessage.createFromPdu() (GSM 3gpp)

查看:635
本文介绍了创建PDU为Android与SmsMessage.createFromPdu()(GSM 3GPP)的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:(注:所选择的答案产生一个GSM(3GPP)PDU)的 CDMA(3GPP2 )请参阅这里

Goal: (NOTE: The answer selected generates a GSM (3gpp) PDU) for CDMA (3gpp2) please refer here

要创建一个可以传递到 SmsMessage.createFromPdu一个PDU(byte []的PDU)。 我在广播的意图,以我的一个 BroadcastReciever 监听短信。

To create a PDU that can be passed into SmsMessage.createFromPdu(byte[] pdu). I'm "Broadcasting an Intent" to one of my BroadcastReciever that listens for SMS messages.

BroadcastReciever

使用 android.provider.Telephony.SMS_RECEIVED 真正的的短信的

使用自定义意图过滤器对这些新的应用程序短信的动作的。

Using a custom intent-filter action for these new "application SMS's".

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

    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) {
            //the currentMessage.getDisplayOriginatingAddress() 
            //or .getDisplayMessageBody() is null if I Broadcast a fake sms
            Log.i("BB", "address:"+currentMessage.getDisplayOriginatingAddress()+" message:"+currentMessage.getDisplayMessageBody());
    ...

所以,我希望我的 BroadcastReciever ,以便能够处理两种类型的邮件,无需增加额外code

So I want my BroadcastReciever to be able to handle both types of messages without adding extra code

(是的,我知道我可以有一个不同的 BroadcastReciever 为不同的意图过滤器的行动,但我想为把这事办成,因为我知道这是可以做到的,我固执)

(yes I know I can have a different BroadcastReciever for the different intent-filter action but I would like to actually pull this off as I know it can be done, I'm stubborn)

研究:

我一直在做研究的所有昼/夜。我试着写我自己的,即使我很可怕的数学和转换,创造一个合适的算法。我看过了上的PDU 堆栈的话题,和的创建PDU的Andr​​oid 但链接的答案打破。我甚至看了看 com.google.android.mms.pdu 源$ C ​​$ C

I've been doing research all day/night. I've tried writing my own even though I'm very terrible with the math and conversions and creating a suitable algorithm. I've looked over Stack topics on PDUs, and Create PDU Android but the link is broken in the answer. I even Looked at com.google.android.mms.pdu source code

到目前为止,我只能够创建一个PDU没有的起始地址的使用一些code从的 http://www.wrankl.de/JavaPC/SMSTools.html

so far I've only been able to create a PDU without a "originating address" using some code from http://www.wrankl.de/JavaPC/SMSTools.html

PDU:

目标:555消息:的HelloWorld

destination: 555 message: helloworld

"1100038155f50000aa0ae8329bfdbebfe56c32"

这显然是无效的......

Which obviously isn't valid...

便笺:

我不想做任何事情,除了与当地使用的PDU计划,我不想硬codeD PDU在我的code,因为我不重用的PDU。

I don't plan on doing anything with the PDU besides local use, I do not want hard coded PDU's in my code because I'm not reusing the PDU.

如果有什么我可以添加到code我使用了的起始地址添加的,将工作。或者有没有人有信息上的图书馆,我不知道呢?

If there is anything I can add to the code I'm using to add in a "originating address", that will work. Or does anyone have info on a Library I'm not aware of?

感谢

更新:

尝试

byte[] by =(byte[])(SmsMessage.getSubmitPdu("12345", "1234", "hello", false).encodedMessage);

这给了我以下(十六进制再presentation)

which gives me the following (in hex representation)

"0000100200000000000000000000000004010203040000000e000320ec400107102e8cbb366f00"

我以前不工作

did't work

推荐答案

也许这个片段并没有很多的细节领域,如你想,但我的简单的目的就可以调用通知像另一个短信。

Maybe this snippet doesn't have many detail fields like you want but for my simple purpose it can invoke notification like another sms.

    private static 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) {
        }

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

    Intent intent = new Intent();
    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.startService(intent);
}

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

希望你能找到一些有用的东西。

Hope you will find something useful

更新:

 public static final SmsMessage[] getMessagesFromIntent(
                Intent intent) {
            Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
            byte[][] pduObjs = new byte[messages.length][];

            for (int i = 0; i < messages.length; i++) {
                pduObjs[i] = (byte[]) messages[i];
            }
            byte[][] pdus = new byte[pduObjs.length][];
            int pduCount = pdus.length;
            SmsMessage[] msgs = new SmsMessage[pduCount];
            for (int i = 0; i < pduCount; i++) {
                pdus[i] = pduObjs[i];
                msgs[i] = SmsMessage.createFromPdu(pdus[i]);
            }
            return msgs;
        }

这篇关于创建PDU为Android与SmsMessage.createFromPdu()(GSM 3GPP)的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆