Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

How to send SMS using Java with a HSDPA Dongle.

4.67/5 (3 votes)
12 Nov 2012CPOL3 min read 100.8K  
Simply send SMS using Java with GSM Modem or HSDPA Dongle

Hello Everyone!

Today I am going to tell you about how to simply send SMS by using your HSDPA dongle or GSM modem. Cool! Isn't it? yes It's not that difficult either. I have tried so many ways to accomplish this. After so many attempts I got the way to do it right. So I will tell you how I have done that.



Basically this was created by using SMSLib which is an open source library for Java. After studying the API I have created a simple API wrapper for convenience. I have hosted the open source code in Github with a sample project named SMS.Dura.Wrapper. For this sample project I have made and used a prebuilt Jar library named smsdura-1.0.jar which works as the wrapper. So in your project you have to simply import that smsdura-1.0.jar, just configure the modem, put the message, TP, and send. Now SMS from Java? It' s very easy! I will tell you how to do that. May be it will add more value for your project as well.

I will guide you through the rest of the process. Okay let's start!

1. First of all you have to Download this sample project.

2. Now we have to copy few files in the <Extras folder> to your Java Classpath. [Normally JDKDIR can be found in > C:\Program Files\Java] Go to that path and do it as said. Detailed instructions are listed below.

---------------------------    
    External Configurations for the JVM on the targeted machine. (Strictly Recommended!)
    [These files can be found in <extras> folder of the Project path.)
---------------------------
Java Comm Installation <comm dir>
-----------------------------------
    File comm.jar should go under JDKDIR/jre/lib/ext/ 
    File javax.comm.properties should go under JDKDIR/jre/lib/
    Library files (i.e. win32com.dll for Win32 or the .so Linux library files) should go under JDKDIR/jre/bin/
    If you have a separate JRE directory, do the same copies for the JREDIR directory!
RxTx Installation <rxtx dir>
-----------------------------------
    File RXTXcomm.jar should go under JDKDIR/jre/lib/ext/
    The necessary library (e.g.. for Linux 32bit, the librxtxSerial.so) should go under JDKDIR/jre/bin/
    If you have a separate JRE directory, do the same copies for the JREDIR directory!

Huh! All the hard work has been done. be cool now Smile | :)

3. Okay now simply open the project from Netbeans IDE or whatever IDE you are using.

4. Importing the Required Dependencies. I have imported all the Jars for this project. So by default you have to do nothing with importing. But in your project you have to import few Jar files you can found in <lib> folder. You have to import all the Jars in that folder.

5. Okay done! Now take a look at this code.

Java
package logic;

import com.harshadura.gsm.smsdura.GsmModem;

public class TestSMS {

    private static String port = "COM3"; //Modem Port.
    private static int bitRate = 115200; //this is also optional. leave as it is.
    private static String modemName = "ZTE"; //this is optional.
    private static String modemPin = "0000"; //Pin code if any have assigned to the modem.
    private static String SMSC = "+9477000003"; //Message Center Number ex. Mobitel

    public static void main(String[] args) throws Exception {
        GsmModem gsmModem = new GsmModem();
        GsmModem.configModem(port, bitRate, modemName, modemPin, SMSC);
        gsmModem.Sender("+94712244555", "Test Message"); // (tp, msg) 
    } 
}

Okay in here, you will notice that you have to first find out the port number of your USB modem/dongle. For that you can simply right click the MyComputer Icon > go to Mange > then Search for Modems > then it will pop up a interface with several tabs. Okay then you can simply notice theirs a Name called port. In front of that there is the port number. Now you know the port number. Insert that into the code.

Modem name is a optional thing. Give it some relevant name.

Bit rate? Leave it as it is. Or change to a proper one. The number will change depending modem to modem. so just leave as it as.

Some modems are using PIN numbers for Security. Does your one also using such a pin? If so please insert it to the code.

Well then You have to insert the SMS Message Center Number. You already know that. Suppose you own a Mobitel/Dialog SIM you can easily find out it in your Mobile phone/Dongle Message Settings. So get the Number and paste it with +94 prefix.

Wow! Now all are ready. we have completed the Configuration of the modem. Cool isnt it? Big Grin | :-D

Okay now please disconnect your Modem from the Internet Before continue. If its connected to the internet or may be the Mobile Partner or whatever the software you are using to connect with the modem, can disturb our work. So just close them All. You better restart your computer to refresh the Modem Port. Don't Connect it to the Internet!!

Well now we are ready to send SMS thorough Java.. Be cool..! 

C#
gsmModem.Sender("+94712244555", "Test Message"); // (tp, msg)

Just put some telephone number and message for the body. Then do a Clean and Build. Now simply Run the project.

Okay everything has to be working properly.. Wait, give it some time! Did you get the message? Big Grin | :-D

So that's how we can simply send SMS using Java with our USB modem. Cool way.. huh!

Cheers!
 - Harsha

Please note that this is an API wrapper for SMSLib and the Main API has more functionality than this wrapper code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)