Alex T
Alex T

Reputation: 765

Android SMS un-readable if over 140 characters

I'm trying to receive SMS messages through a custom broadcastreceiver. Everything works great as long as the messages are shorter than 140 characters. Anything over 140 causes the messages to display as garbage on the screen.

I've looked at the three methods outlined below (note that this is not my exact code)

        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) { // for each message
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            //here are three things I've tried so far:

            String msg = msgs[i].getMessageBody(); //nope, msg contains garbage
                   msg = msgs[i].getDisplayMessageBody(); //nope, msg contains garbage
                   byte[] bdata = msgs[i].getUserData();    
                   for(int j=0; j < bdata.length; j++) {
                       str += Character.toString((char)bdata[j]);
                   }
                   //str contains garbage

Here's a screenshot: http://screencast.com/t/m1qjPWvxx

Has anyone come across this before?

Thank you, Alex

Upvotes: 0

Views: 175

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93559

The cap on a single message is 140 characters. They can go up to 160 characters if they use 7 bit ascii for each character instead of 8 bit. It they do that you can't just go byte by byte like that- you actually have to bitmask and shift to get the right values for each characters- they'd be stuffing 7 bit data into 8 bit bytes, meaning each 7 bytes will actually be 8 characters instead of 7. You'd see line noise similar to what you're getting in that case though.

Upvotes: 2

Related Questions