Search code examples
c#unicodeencodingat-command

Send SMS containting Emojis with AT Commands


I would like to send an SMS containing Emojis from my GSM Modem with AT Commands.

However, no matter which encoding I try, it never works (Encoding.BigEndianUnicode, Encoding.Unicode or Default) make the SMS unreadable.

My code looks a bit like this:

// [..]
// send message with UCS2
command = "AT+CSCS=\"UCS2\"" + char.ConvertFromUtf32(13);
send(command);   

// [..]
// convert my message (string from a WPF TextBox) to a unicode hex-string
byte[] ba = Encoding.BigEndianUnicode.GetBytes(message);
var hexString = BitConverter.ToString(ba);
hexString = hexString.Replace("-", "");

// send the converted string
command = hexString  + char.ConvertFromUtf32(26);
send(command);
// [..]

The SMS successfully reaches its destination but the message is just some unreadable stuff.

Is this even possible to do? My GSM Modem would also support "HEX" as encoding.

Update: It kinda works if i replace this line:

command = hexString  + char.ConvertFromUtf32(26);

With this:

command = "80 " + hexString + char.ConvertFromUtf32(26);

But then i get this 㣩 letter at the start of the message...


Solution

  • Make sure that your modem uses the correct data coding scheme for the SMS. See this answer and

    1. Set the modem to text mode using AT+CMGF=1
    2. Set the coding scheme to "UCS2" using AT+CSCS="UCS2"
    3. Send AT+CSMP=1,167,0,8 to the modem.
    4. Now in UCS2 mode, the modem needs the recipient number in UCS2 form as well, i.e. for +123456890 send AT+CGMS="002B003100320033003400350036003800390030",145 (145 is type-of-address for numbers with country code, use 129 otherwise.
    5. The modem should respond with a > prompt. Then, send the message in the right encoding i.e. sending the message as what I would describe as an ASCII hex sequence of UCS2/UTF-16 bytes, i.e. encoding the string to UTF-16BE, then taking each byte and formatting it as ASCII HEX characters, i.e. 🐈 which is U+1F408 becomes D83DDC08 that is sent to the modem, 012ABC becomes 003000310032 004100420043.