Strange character when reading NFC tag - android

I am trying to read an NFC tag using Android. I'm a beekeeper and this is to ID my hives when I approach them. I have searched here but I am still having issues reading the tag. I want to read the text, but when it reads, there is a square-like character and characters displayed like " Ten" before the desired text.
Here is the code I'm using. I know that the payload bytes have to be correct and I have tried changing them but to no avail.
private static NdefMessage getTestMessage() {
byte[] mimeBytes = "application/com.android.cts.verifier.nfc"
.getBytes(Charset.forName("US-ASCII"));
byte[] id = new byte[] {1, 3, 3, 7};
byte[] payload = "CTS Verifier NDEF Push Tag".getBytes(Charset.forName("US-ASCII"));
return new NdefMessage(new NdefRecord[] {
new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, payload)
});
}
#Override
protected void onResume() {
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
mNfcAdapter.setNdefPushMessageCallback(this, this);
}
// sending message
#Override
public NdefMessage createNdefMessage(NfcEvent event) {
return getTestMessage();
}
private NdefMessage[] getNdefMessages(Intent intent) {
Parcelable[] rawMessages = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
}
return messages;
} else {
return null;
}
}
static String displayByteArray(byte[] bytes) {
String res="";
StringBuilder builder = new StringBuilder().append("");
for (int i = 0; i < bytes.length; i++) {
res+=(char)bytes[i];
}
return res;
}
// displaying message
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
NdefMessage[] messages = getNdefMessages(intent);
edtUser.setText(displayByteArray(messages[0].toByteArray()));
Toast.makeText(this, "NFC tag entered", Toast.LENGTH_LONG).show();
}

You are getting odd additional characters when displaying the message since you try to display the whole raw NDEF record as text string (using some odd decoding method):
NdefMessage[] messages = getNdefMessages(intent);
edtUser.setText(displayByteArray(messages[0].toByteArray()));
There are several problems with this. First of all, you would typically want to decode the text using the same encoding that you used to write the text. For instance, if you used
String text = "...";
byte[] bytes = text.getBytes(Charset.forName("US-ASCII"));
to get a byte array in US-ASCII encoding for a given text string, you would also want to use that same US-ASCII encoding to translate the bytes into a text string again:
byte[] bytes = ...;
String text = new String(bytes, "US-ASCII");
Second, you are interpreting the whole NDEF message as a text string. However, the text that you stored on the tag is typically only contained inside the payload of an NDEF record. In your case, the prefix "Ten" suggests that you used an NFC Forum Text record (type name "T") with the language indication "en" (for English). You would, therefore, want to search the NDEF message for the Text record:
for (NdefRecord r : messages[0].getRecords()) {
if (r.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
if (Arrays.equals(r.getType(), NdefRecord.RTD_TEXT)) {
Once you found the Text record, you can decode its text payload. The payload consists of a status byte, a language field and the actual text:
byte[] payloadBytes = ndefRecord.getPayload();
boolean isUTF8 = (payloadBytes[0] & 0x080) == 0; //status byte: bit 7 indicates encoding (0 = UTF-8, 1 = UTF-16)
int languageLength = payloadBytes[0] & 0x03F; //status byte: bits 5..0 indicate length of language code
int textLength = payloadBytes.length - 1 - languageLength;
String languageCode = new String(payloadBytes, 1, languageLength, "US-ASCII");
String payloadText = new String(payloadBytes, 1 + languageLength, textLength, isUTF8 ? "UTF-8" : "UTF-16");
edtUser.setText(payloadText);
}
}
}

Related

Cordova-plugin-sms too big SMS error

I´m using Cordova-plugin-sms and it is working fine.
I can send sms except I cant exceed the standart 140-70 characters for each sms.
I need to send 2-in-1 sms like default SMS app.
For the exceeded size sms, it gives a successCallback, but sms is not sent.
https://github.com/floatinghotpot/cordova-plugin-sms
var successCallback = function () {
msgSentUser(message);
};
var failureCallback = function (e) {
};
SMS.sendSMS(number, fullMsg, successCallback, failureCallback);
Thank you,
Looking at the source code for that plugin, you can see that it uses the SmsManager#sendTextMessage() method. This method only handles single-part messages, and it will fail silently if you pass it a message that exceeds the character limit for a single part in the alphabet you're using. You still get the successCallback, however, since no Exception is thrown, and the plugin itself uses no other method of confirmation. The solution is to alter the code to utilize the sendMultipartTextMessage() method.
In the original source, lines 206 through 212, inclusive, handle the message send, and are what we need to replace. That is, these lines:
PendingIntent sentIntent = PendingIntent.getBroadcast((Context)this.cordova.getActivity(),
(int)0, (Intent)new Intent("SENDING_SMS"), (int)0);
SmsManager sms = SmsManager.getDefault();
for (int i = 0; i < n; ++i) {
String address;
if ((address = addressList.optString(i)).length() <= 0) continue;
sms.sendTextMessage(address, null, text, sentIntent, (PendingIntent)null);
}
The following replacement block divides the message into the appropriate parts, and creates the necessary ArrayList of PendingIntents to pass to the sendMultipartTextMessage() method. Please note that if you are handling the SENDING_SMS broadcast, it will now fire once for each message part, instead of once per send, as it did for single-part messages.
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(text);
final int count = parts.size();
ArrayList<PendingIntent> sentPIs = new ArrayList<PendingIntent>(count);
int req = 0;
PendingIntent pi = null;
for (int i = 0; i < n; i++) {
String address;
if ((address = addressList.optString(i)).length() <= 0) continue;
sentPIs.clear();
for (int j = 0; j < count; j++) {
req = i * count + j;
pi = PendingIntent.getBroadcast((Context) this.cordova.getActivity(),
req, new Intent("SENDING_SMS"), 0);
sentPIs.add(pi);
}
sms.sendMultipartTextMessage(address, null, parts, sentPIs, null);
}
The incoming message handling in that plugin is not correct, and will result in multipart messages appearing as multiple, separate messages. Two code sections will need to be altered to fix this. The first is lines 350 to 354, inclusive:
for (int i = 0; i < pdus.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[])((byte[])pdus[i]));
JSONObject json = SMSPlugin.this.getJsonFromSmsMessage(sms);
SMSPlugin.this.onSMSArrive(json);
}
Which we change to:
JSONObject json = SMSPlugin.this.getJsonFromSmsMessage(pdus);
SMSPlugin.this.onSMSArrive(json);
Next, we need to alter the getJsonFromSmsMessage() method; lines 447 through 466, inclusive:
private JSONObject getJsonFromSmsMessage(SmsMessage sms) {
JSONObject json = new JSONObject();
try {
json.put( ADDRESS, sms.getOriginatingAddress() );
json.put( BODY, sms.getMessageBody() );
json.put( DATE_SENT, sms.getTimestampMillis() );
json.put( DATE, System.currentTimeMillis() );
json.put( READ, MESSAGE_IS_NOT_READ );
json.put( SEEN, MESSAGE_IS_NOT_SEEN );
json.put( STATUS, sms.getStatus() );
json.put( TYPE, MESSAGE_TYPE_INBOX );
json.put( SERVICE_CENTER, sms.getServiceCenterAddress());
} catch ( Exception e ) {
e.printStackTrace();
}
return json;
}
This method will now be as follows. Please note that the method's parameter type has changed, and the JSONObject's value for the BODY key has, as well.
private JSONObject getJsonFromSmsMessage(Object[] pdus) {
SmsMessage sms = null;
StringBuilder sb = new StringBuilder();
JSONObject json = new JSONObject();
for (int i = 0; i < pdus.length; i++) {
sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(sms.getMessageBody());
}
try {
json.put(ADDRESS, sms.getOriginatingAddress());
json.put(BODY, sb.toString());
json.put(DATE_SENT, sms.getTimestampMillis());
json.put(DATE, System.currentTimeMillis());
json.put(READ, MESSAGE_IS_NOT_READ);
json.put(SEEN, MESSAGE_IS_NOT_SEEN);
json.put(STATUS, sms.getStatus());
json.put(TYPE, MESSAGE_TYPE_INBOX);
json.put(SERVICE_CENTER, sms.getServiceCenterAddress());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
For the sms concat i used this: ( i use this because 1 to 1 chat)
var totalSms = "";
function timeToAdd(newSms) {
totalSms = totalSms + newSms;
if (totalSms == newSms) { // only waits the first time
window.setTimeout(
function () {
msgReceived(totalSms);
addConversationMessage(totalSms, "sender");
totalSms = "";
}, 1000);
}
}
it basicly waits 1 second after the first "onsmsarrive" event to concat all the sms received (since every sms takes >1s to send) it should work
It seems like the problem is in:
safesmsExport.sendSMS = function(address, text, successCallback, failureCallback) {
var numbers;
if( Object.prototype.toString.call( address ) === '[object Array]' ) {
numbers = address;
} else if(typeof address === 'string') {
numbers = [ address ];
} else {
if(typeof failureCallback === 'function') {
failureCallback("require address, phone number as string, or array of string");
}
return;
}
cordova.exec(successCallback, failureCallback, 'SMS', 'sendSMS', [numbers, text]);
};
This is not calling the function sendSMS from the smsPlugin.java. Stands alone on SMS send even if smsPlugin sendSMS is commented.
I chose to change plugin. I got this one to work sending big sms: https://github.com/cordova-sms/cordova-sms-plugin , the problem is that this one doesn't have the startwatch. im trying to add it ( I'm new on plugins and JS as you can see)

Write and read application record to NFC tag on Android

I'm trying the following:
Write a message to an NFC tag that contains a reference to my application as well as a short string with which I can identify the tag.
Read that message.
To speed up testing a bit at the beginning I've used the app Tagwriter (https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&hl=de) to write a tag with my needs: "Create plain text" and "Add launch application" in the next window.
Upon contact with the tag my phone will start up my application, it'll even read the identifying string correctly. However I also want it to write the tag from my own application instead of referring to another one.
I've tested several approaches, none of them worked. Either my application isn't started at all or it can't read the string. Can anyone help me?
public static boolean writeTag(String textToWrite, Tag tag)
{
Miscellaneous.logEvent("i", "NFC", "Attempting to write tag...", 2);
String packageName = Miscellaneous.getAnyContext().getPackageName();
NdefRecord appRecord = NdefRecord.createApplicationRecord(packageName);
// Record with actual data we care about
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
new String("application/" + packageName)
.getBytes(Charset.forName("US-ASCII")),
null, textToWrite.getBytes());
// Complete NDEF message with both records
NdefMessage completeMessageToWrite = new NdefMessage(new NdefRecord[] {textRecord, appRecord});
int size = completeMessageToWrite.toByteArray().length;
try
{
Ndef ndef = Ndef.get(tag);
if (ndef != null)
{
ndef.connect();
if (ndef.isWritable() && ndef.getMaxSize() > size)
{
ndef.writeNdefMessage(completeMessageToWrite);
Miscellaneous.logEvent("i", "NFC", "Done writing tag.", 2);
return true;
}
}
else
{
NdefFormatable format = NdefFormatable.get(tag);
if (format != null)
{
try
{
format.connect();
format.format(completeMessageToWrite);
Miscellaneous.logEvent("i", "NFC", "Done writing tag.", 2);
return true;
}
catch(IOException e)
{
Miscellaneous.logEvent("e", "NFC", "Error writing tag: " + Log.getStackTraceString(e), 2);
}
}
}
}
catch(Exception e)
{
Miscellaneous.logEvent("e", "NFC", "Error writing tag: " + Log.getStackTraceString(e), 2);
}
return false;
}
I'm sorry, could have been a bit more detailed. It appears I kind of solved my problem myself. I've been taking a bit of example code from this website and a bit from that website and...
That's why I had to first do some cleaning up before answering here. In that process I kind of found the mistake. The write-function now looks like this:
public static boolean writeTag(String textToWrite, Tag tag)
{
Miscellaneous.logEvent("i", "NFC", "Attempting to write tag...", 2);
String packageName = Miscellaneous.getAnyContext().getPackageName();
NdefRecord appRecord = NdefRecord.createApplicationRecord(packageName);
// Record with actual data we care about
byte[] textBytes = textToWrite.getBytes();
byte[] textPayload = new byte[textBytes.length + 3];
textPayload[0] = 0x02; // 0x02 = UTF8
textPayload[1] = 'e'; // Language = en
textPayload[2] = 'n';
System.arraycopy(textBytes, 0, textPayload, 3, textBytes.length);
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], textPayload);
// Complete NDEF message with both records
NdefMessage completeMessageToWrite = new NdefMessage(new NdefRecord[] {textRecord, appRecord});
[...]
}
It appears the "apprecord" was fine, but the text record was not.

Open browser on scan of NFC tag in Android

I am trying to develop a use case such that when any user having a NFC enabled smartphone taps a NFC tag, the browser should open with the URL contained in the tag.
At present I am using a Mifare Classic 1K tag that contains the URL http://www.google.com that I have written in my NFC tag.
Now when I tap/scan the tag from my Google Nexus 7 (2012) edition, the tag gets detected but the browser isn't showing up. Also on other devices including Samsung S3, S4 the tag isnt detected at all. Why is that so?
Here is my code that does the writing and reading of tag,
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//check for nfc
adapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] { tagDetected };
//write tag
write("http://www.google.com",tag);
}
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Log.i("App","Tag:" +tag);
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
}
private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
return recordNFC;
}
#Override
protected void onNewIntent(Intent intent){
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
new NdefReaderTask().execute(mytag);
Toast.makeText(this, this.getString(R.string.ok_detection) + mytag.toString(), Toast.LENGTH_LONG ).show();
}
}
private class NdefReaderTask extends AsyncTask<Tag, Void, String> {
#Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
return readText(ndefRecord);
} catch (UnsupportedEncodingException e) {
Log.e("App", "Unsupported Encoding", e);
}
}
}
return null;
}
private String readText(NdefRecord record) throws UnsupportedEncodingException {
/*
* See NFC forum specification for "Text Record Type Definition" at 3.2.1
*
* http://www.nfc-forum.org/specs/
*
* bit_7 defines encoding
* bit_6 reserved for future use, must be 0
* bit_5..0 length of IANA language code
*/
byte[] payload = record.getPayload();
// Get the Text Encoding
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
// Get the Language Code
int languageCodeLength = payload[0] & 0063;
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
// e.g. "en"
// Get the Text
return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
}
#Override
protected void onPostExecute(String result) {
if (result != null) {
Toast.makeText(getApplicationContext(), "Read content: " +result,Toast.LENGTH_LONG).show();
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(result);
}
}
}
Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="com.framentos.hellonfc.MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.NFC" />
Also, do I need to have an app always present in the device for the browser to be invoked. As NFC is a service, shouldn't every NFC device open the browser on tap of tag.
That is because you are writing a text record with the text "http://www.google.com" to the tag. NFC devices usually do not know how to interpret text on tags (though they could just display that text -- and some devices do exactly that).
What you want to do is create a record based on the NFC Forum's URI Record Type Definition. The createRecord method could look like this:
private NdefRecord createRecord(String uri) throws UnsupportedEncodingException {
byte[] uriBytes = uri.getBytes("UTF-8");
byte[] payload = new byte[1 + uriBytes.length];
// set prefix byte (see URI RTD for possibile values, we just use 0 indicating no prefix for now)
payload[0] = 0;
// copy uriBytes into payload
System.arraycopy(uriBytes, 0, payload, 1, uriBytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI,
null,
payload);
}
Or you could just use Android's built-in method:
record = NdefRecord.createUri("http://www.google.com");
Regarding the detection on other devices: Using a MIFARE Classic tag on the S3 should work. The S4 (and any other device based on Broadcom's NFC chipset) does not support MIFARE Classic at all, so you would need to switch to another tag technology to support those devices.

android client socket is not receiving data

I have a tcp server which is not in java also its a hardware device , I need to send and receve the data ,
I am connecting with server and sending some configuration data using following code
this.clientSocket=new Socket("198.168.1.17",9999);
this.os=new DataOutputStream(this.clientSocket.getOutputStream());
this.in=new DataInputStream(this.clientSocket.getInputStream());
System.out.println("Conncted");
char data[]={0x03,0x03,0x00};
byte b[]=new byte[data.length];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) data[i];
}
try {
os.write(b);
Device receives data correctly , now in my tcp client i am not able to receive data , though i write following code just after above code
int size =in.available();
byte data1[]=new byte[size];
// in.read(data1);
String str=new String(data1);
System.out.println("Reading data:"+str);
It only shows reading data , and string has not data
also i tried about code in thread
public void run(){
try{
while(true){
int size =in.available();
byte data[]=new byte[size];
in.read(data);
String str=new String(data);
System.out.println("Reading data:"+str);
}
in thread it only shows reading data ,
Please help how can i get data from the server also please note server is built in i can not change the server code
what available() method do in code "int size =in.available();"????????
Solution may be:
available() method return 0 so you are not able to receive data.
use this code for receiving data from socket. write this code before onCreate.
private EditText mInputMessageView;
private Emitter.Listener onNewMessage = new Emitter.Listener() {
#Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONException e) {
return;
}
// add the message to view
addMessage(username, message);
}
});
}
};

Base64 incompatible with 7-bit GSM encoding?

Since I am not able to compress a 256-byte long md5-like string within 160 characters, I decided to use Multi-part message in Android to divide the SMS into several parts and the receiver will listen the SMS then combine those messages.
I use the combination algorithm from here: Android - receiving long SMS (multipart)
And after a few testing I found that the SMS I sent was totally messed, though I encoded them with Base64 before sending.
Thinking the SMS is a 7-bit Encoding method I thought it might be the encoding problem.
But I have successfully sent Base 64 encoded message, but it was short and within 1 SMS.
My question is:
If it is a encoding problem, then why I can send a totally readable Base64 encoded messages within 160 characters, but cannot get a readable result when sending messages exceeding 160 characters?
I've attached my code here:
The string I intend to send:
static final String bigNum = "C196BA05AC29E1F9C3C72D56DFFC6154A033F1477AC88EC37F09BE6C5BB95F51C296DD20D1A28A067CCC4D4316A4BD1DCA55ED1066D438C35AEBAABF57E7DAE428782A95ECA1C143DB701FD48533A3C18F0FE23557EA7AE619ECACC7E0B51652A8776D02A425567DED36EABD90CA33A1E8D988F0BBB92D02D1D20290113BB562CE1FC856EEB7CDD92D33EEA6F410859B179E7E789A8F75F645FAE2E136D252BFFAFF89528945C1ABE705A38DBC2D364AADE99BE0D0AAD82E5320121496DC65B3930E38047294FF877831A16D5228418DE8AB275D7D75651CEFED65F78AFC3EA7FE4D79B35F62A0402A1117599ADAC7B269A59F353CF450E6982D3B1702D9CA83";
The Code
private void sendMsg (String phoneNum, String msg) {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(msg);
sms.sendMultipartTextMessage(phoneNum, null, parts, null, null);
}
public void onStart() {
super.onStart();
final String bigNum64 = Base64.encodeToString(bigNum.getBytes(), 0);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendMsg("5554", bigNum64);
textView.setText(bigNum64);
}
});
}
public void onResume() {
super.onResume();
Bundle receiver = getIntent().getExtras();
if (receiver != null) {
String msg = receiver.getString("SMS");
textView.setText("Received" + msg);
}
}
}
The SMS Receiver:
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
String body = "";
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
SmsMessage sms = messages[0];
try {
if (messages.length == 1 || sms.isReplace()) {
body = sms.getDisplayMessageBody();
}
else {
StringBuilder bodyText = new StringBuilder();
for (int i = 0; i < messages.length; i++) {
bodyText.append(messages[i].getMessageBody());
}
body = bodyText.toString();
}
}
catch (Exception e) {
}
Intent start = new Intent(context, SendLongSMSActivity.class);
start.putExtra("SMS", body);
start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(start);
}
Thanks for your help!
Related Post: Any way to compress a 256 bytes "MD5-like" string into 160 bytes or less?
Even if I give a rather simple long string, like
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
and no matter with or without Base64 encoding, the receiver cannot get the correct results.
UPDATE #2
When using real phone for testing, the receiver can receive correct message without base 64, it was due to the emulator garbled the text.
Question now closed.
Thanks for everyone who helps! And thanx #Dan
UPDATE:
The string "0123456789ABCDEF..." without base64, divided into 2 parts:
part [0] "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345678"
part [1] "9ABCDEF0123456789ABCDEF"
Here's the divided base64 string of the "0123456789ABCDEF...:
part[0] "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4\nOUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCQ0RFRjAx"
part[1] "\nMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlB\nQkNERUY=\n"
It appears that the sms divide is adding \n characters after every 77 characters and at the end, just strip those from your string and it will decode properly.

Categories

Resources