TransWikia.com

Send unicode sms with java

Stack Overflow Asked by hong4rc on February 21, 2021

I send SMS via GSM device, try this: https://stackoverflow.com/a/31361225/6250402

It can send normal SMS, I try to send a Unicode text but It auto-convert to ASCII: VD¥n ba:#n D_a:?n: TBM01

Lib:

<dependency>
    <groupId>com.neuronrobotics</groupId>
    <artifactId>nrjavaserial</artifactId>
    <version>5.1.1</version>
</dependency>

My code:

CommPortIdentifier portId = "I get CommPort match name";
SerialPort serialPort = portId.open("name", 2000);
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, 
        SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
this.outputStream = serialPort.getOutputStream();

// each command function
private void send(String cmd) throws IOException {
    outputStream.write(cmd.getBytes(StandardCharsets.UTF_8)); //
    outputStream.flush();
}

// this sends message method

 send("AT+CMGS=" + '"' + phone + '"' + "rn");
 send(message + '32');

Did I have the wrong config?

Any code to send SMS via GSM for java is a good answer.

Result I missing AT+CSMP=1,167,0,8 (https://stackoverflow.com/a/15312611/6250402, to more info, look https://www.smssolutions.net/tutorials/gsm/sendsmsat/)

3 Answers

I've an example project of GSM device communication in Java which I run in a raspberry attached to a SIMCom800L module. It uses jSerialComm for serial communications which works nice with no configuration at all. Please take a look to my repo: https://github.com/pepevalbe/sms-gateway

To send Unicode you need to configure your GSM device Character Set. Check possible sets with this command:

AT+CSCS=?
+CSCS: ("GSM","UCS2","IRA","HEX")

If the answer contains "HEX" or "UCS2", Unicode seems to be supported. I tried with UCS2 and it is working nice. Just change the Character Set with: AT+CSCS="UCS2" This will only affect to sms, not regular commands.

Now your device will properly recognize the unicode strings you send from Java. Don't forget to set the sms text mode as well: AT+CMGF=1

I give you a full example based on my repo:

//SIMCom serial port configuration: 115200 bps, 8 bit data, no parity, 1 bit stop, no data stream control
SerialPort serialPort = SerialPort.getCommPort("serial0");
serialPort.setComPortParameters(115200, 8, ONE_STOP_BIT, NO_PARITY);
serialPort.setFlowControl(FLOW_CONTROL_DISABLED);
serialPort.setComPortTimeouts(TIMEOUT_READ_BLOCKING, 3000, 0);
serialPort.openPort();

// Set sms text mode
String textModeCommand = "AT+CMGF=1r";
serialPort.writeBytes(textModeCommand.getBytes(StandardCharsets.UTF_8), textModeCommand.length());

// Set UCS2 charset
String characterSetCommand = "AT+CSCS="UCS2"r";
serialPort.writeBytes(characterSetCommand.getBytes(StandardCharsets.UTF_8), characterSetCommand.length());

// Send sms command
String sendTextCommand1 = "AT+CMGS="" + number + ""n";
String sendTextCommand2 = text + (char) 26 + "r";
serialPort.writeBytes(sendTextCommand1.getBytes(StandardCharsets.UTF_8), sendTextCommand1.length());
serialPort.writeBytes(sendTextCommand2.getBytes(StandardCharsets.UTF_16), sendTextCommand2.length());

Notice that in the send sms command I encoded the text part using UTF_16 charset, not UTF_8

Correct answer by pepevalbe on February 21, 2021

To send unicode text ,first the charset to be selected as UTF-16 (UCS2)

AT+CSCS="UCS2"

After you set AT+CSCS="UCS2" every single string parameter you send must be encoded to UCS2 format ,

You canrefer the blow utilty to enCode to UCS2 http://d-chips.blogspot.com/2012/06/coding-of-alpha-fields-in-sim-for-ucs2.html

Answered by Rajesh Gopu on February 21, 2021

Following are the reasons need to be check in sending/receiving unicoded text

  • Make sure your reading the input in UTF-8 format, create InputstreamReader constructor with UTF-8 encoding

InputStream inputStream = new FileInputStream("data.txt");

InputStreamReader inputStreamReader =  new InputStreamReader(inputStream, Charset.forName("UTF-8"));

For more references : https://docs.oracle.com/javase/tutorial/i18n/text/stream.html

  • Use bytemessage methods instead of text message for sending/receiving unicode text

https://docs.oracle.com/javaee/7/api/javax/jms/BytesMessage.html

Answered by Mirudhubashini on February 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP