Search code examples
javasmsgsmpdu

SMS PDU format - how to extract message part


How can I extract a message from SMS PDU?

I need to take a message from SMS PDU. When I use some online services, they work fine. For example, here - http://www.diafaan.com/sms-tutorials/gsm-modem-tutorial/online-sms-pdu-decoder/ - message from PDU 0791448720003023240DD0E474D81C0EBB010000111011315214000BE474D81C0EBB5DE3771B is diafaan.com.

I found some SMS PDU Java implementations to do PDU->Text conversion, but it seems they didn't work as I expect because I don't extract message part from whole PDU (in another words I don't cut the service information - From, SMSC...) - so how can I do this on Java? Or just an algorithm would be a great help too. Thanks!


Solution

  • Finally I used SMSLib library:

                //String resultMessage = ...
                Pdu pdu = new PduParser().parsePdu(resultMessage);
                byte[] bytes = pdu.getUserDataAsBytes();
                String decodedMessage;
                int dataCodingScheme = pdu.getDataCodingScheme();
                if (dataCodingScheme == PduUtils.DCS_ENCODING_7BIT) {
                    decodedMessage = PduUtils.decode7bitEncoding(null, bytes);
                } else if (dataCodingScheme == PduUtils.DCS_ENCODING_8BIT) {
                    decodedMessage = PduUtils.decode8bitEncoding(null, bytes);
                } else if (dataCodingScheme == PduUtils.DCS_ENCODING_UCS2) {
                    decodedMessage = PduUtils.decodeUcs2Encoding(null, bytes);
                } else {
                    log.error("Unknown DataCodingScheme!");
                    ...
                }