diff options
4 files changed, 46 insertions, 6 deletions
diff --git a/src/android/bluetooth/client/map/BluetoothMapBmessage.java b/src/android/bluetooth/client/map/BluetoothMapBmessage.java index 84e4c75..e06b033 100644 --- a/src/android/bluetooth/client/map/BluetoothMapBmessage.java +++ b/src/android/bluetooth/client/map/BluetoothMapBmessage.java @@ -159,6 +159,7 @@ public class BluetoothMapBmessage { json.put("status", mBmsgStatus); json.put("type", mBmsgType); json.put("folder", mBmsgFolder); + json.put("charset", mBbodyCharset); json.put("message", mMessage); } catch (JSONException e) { // do nothing diff --git a/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java b/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java index ef75e7b..d4da25c 100644 --- a/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java +++ b/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java @@ -33,11 +33,13 @@ import android.bluetooth.client.map.utils.BmsgTokenizer.Property; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.text.ParseException; class BluetoothMapBmessageParser { private final static String TAG = "BluetoothMapBmessageParser"; + private final boolean DBG = false; private final static String CRLF = "\r\n"; @@ -75,6 +77,10 @@ class BluetoothMapBmessageParser { static public BluetoothMapBmessage createBmessage(String str) { BluetoothMapBmessageParser p = new BluetoothMapBmessageParser(); + if (DBG) { + Log.d(TAG, "actual wired contents: " + str); + } + try { p.parse(str); } catch (IOException e) { @@ -291,6 +297,17 @@ class BluetoothMapBmessageParser { } while (!prop.equals(BEGIN_MSG)); /* + * check that the charset is always set to UTF-8. We expect only text transfer (in lieu with + * the MAPv12 specifying only RFC2822 (text only) for MMS/EMAIL and SMS do not support + * non-text content. If the charset is not set to UTF-8, it is safe to set the message as + * empty. We force the getMessage (see BluetoothMasClient) to only call getMessage with + * UTF-8 as the MCE is not obliged to support native charset. + */ + if (!mBmsg.mBbodyCharset.equals("UTF-8")) { + Log.e(TAG, "The charset was not set to charset UTF-8: " + mBmsg.mBbodyCharset); + } + + /* * <bmessage-body-content>::={ "BEGIN:MSG"<CRLF> 'message'<CRLF> * "END:MSG"<CRLF> } */ @@ -314,7 +331,11 @@ class BluetoothMapBmessageParser { if (prop != null) { if (prop.equals(END_MSG)) { - mBmsg.mMessage = new String(data, 0, messageLen); + if (mBmsg.mBbodyCharset.equals("UTF-8")) { + mBmsg.mMessage = new String(data, 0, messageLen, StandardCharsets.UTF_8); + } else { + mBmsg.mMessage = null; + } } else { /* Handle possible exception for incorrect LENGTH value * from MSE while parsing GET Message response */ @@ -346,7 +367,11 @@ class BluetoothMapBmessageParser { throw expected(END_MSG); } - mBmsg.mMessage = remng.substring(0, messageLen); + if (mBmsg.mBbodyCharset.equals("UTF-8")) { + mBmsg.mMessage = remng.substring(0, messageLen); + } else { + mBmsg.mMessage = null; + } } prop = mParser.next(); diff --git a/src/android/bluetooth/client/map/BluetoothMasClient.java b/src/android/bluetooth/client/map/BluetoothMasClient.java index 7f71693..87f5a38 100644 --- a/src/android/bluetooth/client/map/BluetoothMasClient.java +++ b/src/android/bluetooth/client/map/BluetoothMasClient.java @@ -962,7 +962,7 @@ public class BluetoothMasClient { * @return <code>true</code> if request has been sent, <code>false</code> * otherwise */ - public boolean getMessage(String handle, CharsetType charset, boolean attachment) { + public boolean getMessage(String handle, boolean attachment) { if (mObexSession == null) { return false; } @@ -974,8 +974,10 @@ public class BluetoothMasClient { return false; } - BluetoothMasRequest request = new BluetoothMasRequestGetMessage(handle, charset, - attachment); + // Since we support only text messaging via Bluetooth, it is OK to restrict the requests to + // force conversion to UTF-8. + BluetoothMasRequest request = + new BluetoothMasRequestGetMessage(handle, CharsetType.UTF_8, attachment); return mObexSession.makeRequest(request); } diff --git a/src/android/bluetooth/client/map/BluetoothMasRequestGetMessage.java b/src/android/bluetooth/client/map/BluetoothMasRequestGetMessage.java index b50fd0f..923bff0 100644 --- a/src/android/bluetooth/client/map/BluetoothMasRequestGetMessage.java +++ b/src/android/bluetooth/client/map/BluetoothMasRequestGetMessage.java @@ -25,6 +25,8 @@ import android.bluetooth.client.map.utils.ObexAppParameters; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import javax.obex.ClientSession; import javax.obex.HeaderSet; @@ -69,7 +71,17 @@ final class BluetoothMasRequestGetMessage extends BluetoothMasRequest { Log.e(TAG, "I/O exception while reading response", e); } - String bmsg = baos.toString(); + // Convert the input stream using UTF-8 since the attributes in the payload are all encoded + // according to it. The actual message body may need to be transcoded depending on + // charset/encoding defined for body-content. + String bmsg; + try { + bmsg = baos.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ex) { + Log.e(TAG, + "Coudn't decode the bmessage with UTF-8. Something must be really messed up."); + return; + } mBmessage = BluetoothMapBmessageParser.createBmessage(bmsg); |