Tuesday, January 20, 2015

Sending and receiving SMS using your dongle

I've almost forgotten I had a blog :|

I had to keep the following somewhere other than my memory since I know how unreliable it is.

There was a need to send and receive SMS for an app and people were wondering if we need to buy a separate GSM modem for that. Easier solution was to use a dongle (which is, well, basically a GSM modem J )

After a little research I thought of using SMSLib as recommended by many folks on internet. (http://smslib.org/)

SMSLib is a SMS messaging library. As mentioned in their home page two SMSLib versions currently available. The old, stable v3.x and the newer v4. SMSLib v4 is under development.

I used smslib-3.5.4.jar.

Downloads can be found at http://smslib.org/download/. Some of the paths given for artifacts in installation directory are not working, so I used the downloads page.

Used
  • HUAWEI Mobile HSPA+ dongle
  • Win64
  • smslib-3.5.4
  • java 7

1. Plug in dongle to USB port and find port number (Control Panel -> Device Manage -> Ports) It was COM11 in my case
2. Add RXTXcomm library as mentioned below
3. Create a sample java project and add smslib-3.5.4.jar in lib
4. Run the following java code. (Update port)

import org.smslib.*;
import org.smslib.modem.SerialModemGateway;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class SmsSenderReceiver {

    public void sendMessage() throws Exception {

        SerialModemGateway gateway = new SerialModemGateway("", "COM11", 9600, "", "");
        gateway.setInbound(true);
        gateway.setOutbound(true);

        OutboundNotification outboundNotification = new OutboundNotification();
        InboundNotification inboundNotification = new InboundNotification();

        Service service = Service.getInstance();
        service.setOutboundMessageNotification(outboundNotification);
        service.setInboundMessageNotification(inboundNotification);
        service.addGateway(gateway);
        service.startService();

        OutboundMessage msg = new OutboundMessage("0722452122", "test111");
        service.sendMessage(msg);
    }

    public class InboundNotification implements IInboundMessageNotification {
        @Override
//Get triggered when a SMS is received
        public void process(AGateway gateway, Message.MessageTypes messageTypes, InboundMessage inboundMessage) {

            System.out.println(inboundMessage);
            try {
                gateway.deleteMessage(inboundMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class OutboundNotification implements IOutboundMessageNotification {

//Get triggered when a SMS is sent
        public void process(AGateway gateway, OutboundMessage outboundMessage) {
            System.out.println(outboundMessage);
        }
    }

    public static void main(String args[]) {
        SmsSenderReceiver app = new SmsSenderReceiver();
        try {
            app.sendMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

A prerequisite of using SMSLib is having Java Communications library as mentioned in http://smslib.org/doc/installation/.
They list two options

1.  Win32 :-> Java Comm v2
2.  For others :-> Java Comm v3 or RxTx

Java Comm didn't work for me. Use RxTx.


(Above are found at http://smslib.org/download/)

File RXTXcomm.jar should go under JDKDIR/jre/lib/ext/
The necessary library (e.g.the librxtxSerial.so) should go under JDKDIR/jre/bin/


In Linux
Tried to do the same in Linux environment, but unfortunately I kept receiving NoSuchPortException.

Tailed /var/log/messages to view updates.

Device was first identified as "usb-storage". usb_modeswitch was used

usb_modeswitch -H -v 0x12d1 -p 0x1506

View changes.
lsusb

View the port numbers and their permissions. Can see the ports that are mapped to GSM modem.
ls /dev/tty* -l

Added read write permissions
chmod o+rw /dev/ttyUSB*
chmod o+rw /dev/ttyS*

Can use minicom to see if the dongle is functioning as expected.

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "Thread-3" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
               at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)

librxtxSerial.so had to be added to JDKDIR/jre/lib/amd64 instead of  JDKDIR/jre/bin/ to solve the above.

As suggested in http://smslib.org/doc/smslib/troubleshooting/ added the soft links to something resembling a standard serial port.
ln -s /dev/ttyUSB_utps_diag /dev/ttyS20
ln -s /dev/ttyUSB_utps_modem /dev/ttyS21
ln -s /dev/ttyUSB_utps_pcui /dev/ttyS23

The above hack and more useful infor can be found at http://rxtx.qbang.org/wiki/index.php/Trouble_shooting too.
View "How does rxtx detect ports? Can I override it?" Section.

Kept on getting the following warning too.

check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock file.
please see: How can I use Lock Files with rxtx? in INSTALL

Adding my user to group uucp didn’t solve it L

usermod -aG uucp myUser

vim /etc/group

13 comments:

  1. Hi!

    Can you tell me how can I choose the location where the inboundSMS gets the received SMS? I think the default is in SIM. How can I change it to local storage of the GSM modem or make it both? By the way, I'm using a broadband stick as my GSM modem.

    Thanks.

    ReplyDelete
  2. Hi Kal

    This was relly helpfull. Anyways i was able to run the code in linux. :)

    ReplyDelete
  3. HI I found exception

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    at org.smslib.Service.listSystemInformation(Service.java:113)
    at org.smslib.Service.initializeService(Service.java:103)
    at org.smslib.Service.(Service.java:95)
    at org.smslib.Service.(Service.java:90)
    at SmsSenderReceiver.sendMessage(SmsSenderReceiver.java:23)
    at SmsSenderReceiver.main(SmsSenderReceiver.java:68)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 6 more

    ReplyDelete
    Replies
    1. Seems u have missing dependencies in your project. "java.lang.ClassNotFoundException: org.apache.log4j.Priority"

      Delete
    2. I also get an exceptions. How can I fix it ?

      Delete
  4. Hi Kal,Sending sms working fine. But receiving SMS not triggering InboundNotification class. Do I need to enable something on dongle settings?

    ReplyDelete
  5. I am facing this error.
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    at org.smslib.Service.listSystemInformation(Service.java:113)
    at org.smslib.Service.initializeService(Service.java:103)
    at org.smslib.Service.(Service.java:95)
    at org.smslib.Service.(Service.java:90)
    at SendSMS.SmsSender.sendMessage(SmsSender.java:22)
    at SendSMS.SmsSender.main(SmsSender.java:57)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 6 more

    ReplyDelete
    Replies
    1. @Abdulrehman Javed That is an indication of one of the required files not being in the proper location. Now, is there anyone who has done an incoming sms listener on Linux?

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. We have two types of sms api Java ; XML and JSON for sending SMS in bulk to your prospects from our gateway.

    ReplyDelete
  8. 2020-07-15 18:28:17,977 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,046 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,128 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,210 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,308 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,396 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,492 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,575 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,666 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,758 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,849 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:18,908 GTW: modem.com8: SEND :(27)
    2020-07-15 18:28:18,956 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,050 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,124 GTW: modem.com8: SEND :+++
    2020-07-15 18:28:19,150 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,274 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,335 GTW: modem.com8: SEND :ATZ(cr)
    2020-07-15 18:28:19,372 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,372 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,469 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,552 GTW: modem.com8: clearBuffer() called.
    2020-07-15 18:28:19,571 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,670 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,771 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,856 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:19,956 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:20,032 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:20,084 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:20,134 GTW: modem.com8: Framing Error!
    2020-07-15 18:28:20,174 GTW: modem.com8: Framing Error!

    ReplyDelete
    Replies
    1. can anyone help me what is the issue here.
      If I connect GPRS modem to laptop, framing Error is coming.

      Delete
  9. Thank You and I have a super present: How Much Are House Renovations Stardew Valley house renovation before and after

    ReplyDelete