Smslib Development Guide

Source: Internet
Author: User
Tags call back

1. Introduction to smslib

Smslib is an open-source SMS cat secondary development kit, which has two versions: Java and. net. The latest version is v3.5.2.

Figure 1-1

Ii. Windows Platform


1. Download the "smslib for Java v3.5.2" development kit from the smslib official website and decompress it. The directory structure is shown in:

Figure 2-1

Figure 2-2

Lib:Store Secondary Development Kit (smslib-3.5.2.jar) and runtime dependent packages (slf4j, log4j, commons-net, jsmpp, etc.) (important)

Dist:Stores the SMS cat Service Development Kit (smsserver-3.5.2.jar), which includes all the core classes in the smslib-3.5.2.jar. If you deploy SMS as a service, you do not need to write code to develop an interface for sending and receiving text messages, and directly deploy the SMS service, for more information, see the guide for installing and configuring the SMS cat service. (Important)

DOC:Smslib introduction, user guide, smsserver installation and configuration documents (important)

Javadoc:Secondary Development Kit API

SRC:Store the source code and sample code of the secondary development kit

Misc:Smslib log (log4j) configuration template, smsserver database creation script, service interface, and other files

Build:Project management documents (not important)


2. Download the sun javacomm V2 (win32) dynamic library and decompress it. The directory structure is shown in:

Figure 3-1

3. Running Environment Configuration

  • Copy the javax. Comm. properties file in Figure 3-1 to the % java_home % \ JRE \ lib directory, and the win32com. dll file to the % java_home % \ JRE \ bin directory.
  • Copy all the jar files in Comm. jar and Figure 2-2 in Figure 3-1 to the classpath directory (if you use eclipse and other ide tools, import these jar packages to the Project ).
4. Run the sample program and test and modify the parameter configuration for sending SMS messages in the SRC \ Java \ examples \ modem directory in Figure 2-1. Compile and run the program. As shown in:
// Sendmessage. java-sample application. // text message sending test program // This application shows you the basic procedure for sending messages. // you will find how to send synchronous and asynchronous messages. //// for asynchronous dispatch, the example application sets a callback // notification, to see what's happened with messages. package examples. modem; import Org. smslib. agateway; import Org. smslib. ioutboundmessageno Tification; import Org. smslib. library; import Org. smslib. outboundmessage; import Org. smslib. service; import Org. smslib. modem. serialmodemgateway; public class sendmessage {public void doit () throws exception {outboundnotification = new outboundnotification (); system. out. println ("Example: Send message from a serial GSM modem. "); system. out. println (library. getlibrarydescription (); system. Out. println ("version:" + Library. getlibraryversion ();/* modem. COM1: Gateway ID (that is, the SMS cat port number) com4: Serial Port name (in the window to comxx represents the port name, in Linux, UNIX platform to ttyS0-N or ttyUSB0-N represents the port name ), use the port detection program to obtain the available port 115200: Bit bits of data sent from the serial port per second. You must set it correctly to send messages normally. You can use the program to detect the messages. Commonly used 115200 and 9600 Huawei: SMS cat manufacturers. The at command interfaces encapsulated by smslib of different SMS cat manufacturers are inconsistent and must be set correctly. common manufacturers such as Huawei and Wavecom use the last parameter to indicate the device model. Optional */serialmodemgateway gateway = new serialmodemgateway ("modem. COM1 "," com4 ", 115200," Huawei "," "); gateway. setinbound (true); // set true to indicate that the gateway can receive text messages and modify the gateway as needed. setoutbound (true); // set true to indicate that the gateway can send text messages and modify the gateway as needed. setsimpin ("0000"); // SIM card lock. The default value is 0000 or 1234. // explicit SMSC address set is required Some modems. // below is for Vodafone Greece-be sure to set your own! Gateway. setsmscnumber ("+ 306942190000"); // service of the SMS service center number. getinstance (). setoutboundmessagenotification (outboundnotification); // call back method after the message is sent successfully. getinstance (). addgateway (GATEWAY); // Add the gateway to the SMS cat service. getinstance (). startservice (); // start the service and enter the SMS sending readiness status system. out. println (); // print the device information system. out. println ("modem information:"); system. out. println ("Manufacturer:" + gateway. getmanufacturer (); system. out. print Ln ("model:" + gateway. getModel (); system. out. println ("Serial No:" + gateway. getserialno (); system. out. println ("Sim imsi:" + gateway. getimsi (); system. out. println ("signal level:" + gateway. getsignallevel () + "dBm"); system. out. println ("battery level:" + gateway. getbatterylevel () + "%"); system. out. println (); // send a message synchronously. outboundmessage MSG = new outboundmessage ("30697400" 0000 "," hello from smslib! "); // Parameter 1: Mobile Phone Number Parameter 2: SMS content service. getinstance (). sendmessage (MSG); // execute the message system. out. println (MSG); // or, send out a WAP Si message. // outboundwapsimessage wapmsg = new outboundwapsimessage ("306974000000", // new URL ("http://www.smslib.org/"), "Visit smslib now! "); // Service. getinstance (). sendmessage (wapmsg); // system. out. println (wapmsg); // You can also queue some asynchronous messages to see how the callbacks // are called... /msg = new outboundmessage ("309999999999", "wrong number! "); // Srv. queuemessage (MSG, Gateway. getgatewayid (); // MSG = new outboundmessage (" 308888888888 "," wrong number! "); // Srv. queuemessage (MSG, gateway. getgatewayid (); system. out. println ("now sleeping-hit <enter> to terminate. "); system. in. read (); service. getinstance (). stopservice ();}/* call this interface after the SMS is successfully sent. The gateway and text message content object sent to the process interface */public class outboundnotification implements ioutboundmessagenotification {public void process (agateway gateway, outboundmessage MSG) {system. out. println ("outbound handler called from Gateway:" + gateway. getgatewayid (); system. out. println (MSG) ;}} public static void main (string ARGs []) {sendmessage APP = new sendmessage (); try {app. doit ();} catch (exception e) {e. printstacktrace ();}}}
// Readmessages. java-sample application. // SMS reader // This application shows you the basic procedure needed for reading // SMS messages from your GSM Modem, in synchronous mode. /// operation description: // The application setup the necessary objects and connects to the phone. // as a first step, it reads all messages found in the phone. // then, it goes to sleep, allowing the asynchronous callback ha Ndlers to // be called. Furthermore, for callback demonstration purposes, it responds // to each received message with a "got it! "Reply. //// tasks: // 1) setup service object. // 2) setup one or more gateway objects. // 3) Attach gateway objects to service object. // 4) setup callback events. // 5) runpackage examples. modem; import Java. util. arraylist; import Java. util. list; import javax. crypto. spec. secretkeyspec; import Org. smslib. agateway; import Org. smslib. agateway. gatewaystatuses; import Org. smslib. agateway. protocols; import Org. smslib. icallnotification; import Org. smslib. igatewaystatusnotification; import Org. smslib. iinboundmessagenotification; import Org. smslib. iorphanedmessagenotification; import Org. smslib. inboundmessage; import Org. smslib. inboundmessage. messageclasses; import Org. smslib. library; import Org. smslib. message. messagetypes; import Org. smslib. service; import Org. smslib. crypto. aeskey; import Org. smslib. modem. seri Almodemgateway; public class readmessages {public void doit () throws exception {// define a list which will hold the read messages. list <inboundmessage> msglist; // create the notification callback Method for Inbound & Status Report // messages. inboundnotification = new inboundnotification (); // create the notification callback Method for inbound voice CILS. callnotification callnotif Ication = new callnotification (); // create the notification callback Method for gateway statuses. gatewaystatusnotification statusnotification = new gatewaystatusnotification (); orphanedmessagenotification = new orphanedmessagenotification (); try {system. out. println ("Example: read messages from a serial GSM modem. "); system. out. println (library. getlibrarydescription (); system. Out. println ("version:" + Library. getlibraryversion (); // create the gateway representing the serial GSM modem. serialmodemgateway gateway = new serialmodemgateway ("modem. com4 "," com4 ", 115200," Huawei "," e160 "); // set the modem protocol to PDU (alternative is text ). PDU is the default, anyway... gateway. setprotocol (protocols. PDU); // do we want the gateway to be used for inbound messages? Gateway. setinbound (true); // do we want the gateway to be used for outbound messages? Gateway. setoutbound (true); // Let smslib know which is the sim pin. gateway. setsimpin ("0000"); // set up the notification methods. service. getinstance (). setinboundmessagenotification (inboundnotification); service. getinstance (). setcallnotification (callnotification); service. getinstance (). setgatewaystatusnotification (statusnotification); service. getinstance (). setorphanedmessagenotification (orphanedmessageno Tification); // Add the gateway to the service object. service. getinstance (). addgateway (GATEWAY); // Similarly, you may define as your gateway objects, representing // various GSM modems, add them in the service object and control all of them. // start! (I. e. connect to all defined gateways) service. getinstance (). startservice (); // printout some general information about the modem. system. out. println (); system. out. println ("modem information:"); system. out. println ("Manufacturer:" + gateway. getmanufacturer (); system. out. println ("model:" + gateway. getModel (); system. out. println ("Serial No:" + gateway. getserialno (); system. out. println ("Sim imsi:" + gateway. getimsi (); system. out. println ("signal level:" + gateway. getsignallevel () + "dBm"); system. out. println ("battery level:" + gateway. getbatterylevel () + "%"); system. out. println (); // in case you work with encrypted messages, its a good time to declare your keys. // create a new AES key with a known key value. // register it in keymanager in order to keep it active. smslib will then automatically // encrypt/decrypt all messages send to/received from this number. service. getinstance (). getkeymanager (). registerkey ("+ 306948494037", new aeskey (New secretkeyspec ("0011223344556677 ". getbytes (), "AES"); // read messages. the reading is done via the service object and // affects all gateway objects defined. this can also be more directed to a specific // gateway-look the javadocs for information on the service method CILS. msglist = new arraylist <inboundmessage> (); service. getinstance (). readmessages (msglist, messageclasses. all); For (inboundmessage MSG: msglist) system. out. println (MSG); // sleep now. emulate real world situation and give a chance to the specifications // methods to be called in the event of message or voice call interval tion. system. out. println ("now sleeping-hit <enter> to stop service. "); system. in. read (); system. in. read ();} catch (exception e) {e. printstacktrace ();} finally {service. getinstance (). stopservice () ;}} public class inboundnotification implements iinboundmessagenotification {public void process (agateway gateway, messagetypes msgtype, inboundmessage MSG) {If (msgtype = messagetypes. inbound) system. out. println (">>> new inbound message detected from Gateway:" + gateway. getgatewayid (); else if (msgtype = messagetypes. statusreport) system. out. println (">>> new inbound status" + "Report Message detected from Gateway:" + gateway. getgatewayid (); system. out. println (MSG) ;}} public class callnotification implements icallnotification {public void process (agateway gateway, string callerid) {system. out. println (">>> new call detected from Gateway:" + gateway. getgatewayid () + ":" + callerid) ;}} public class gatewaystatusnotification implements igatewaystatusnotification {public void process (agateway gateway, gatewaystatuses oldstatus, gatewaystatuses newstatus) {system. out. println (">>> Gateway status change for" + gateway. getgatewayid () + ", old:" + oldstatus + "-> New:" + newstatus) ;}} public class orphanedmessagenotification implements {public Boolean Process (agateway gateway, inboundmessage MSG) {system. out. println (">>> orphaned message part detected from" + gateway. getgatewayid (); system. out. println (MSG); // since we are just testing, return false and keep the orphaned message part. return false ;}} public static void main (string ARGs []) {readmessages APP = new readmessages (); try {app. doit ();} catch (exception e) {e. printstacktrace ();}}}

Iii. Linux, UNIX, and Solaris platformsDifferent from the window platform, the dynamic library and the secondary development package are inconsistent.1. Download rxtx v2.1.7 R22 and unzip the package. The directory structure is shown in Figure 4-13. Running Environment Configuration

  • Copy librxtxserial under the Linux directory in Figure 4-1. copy the so file to the $ java_home/JRE/lib/$ (ARCH)/directory and copy rxtxcomm. jar to the classpath or $ java_home/JRE/lib/EXT directory of the application.
  • Copy javax in Figure 3-1. comm. properties file to the $ java_home/JRE/lib directory, and. sun. comm. change win32driver to driver = GNU. io. commdriver. The file content is shown in:
4. Modify the sample program and compile and run it.
Iv. Test the available port of the SMS cat Device
Import gnu. io. *; import java. util. *; import java. io. *; public class CommTest {static comatrix ortidentifier portId; static Enumeration portList; static int bauds [] = {9600,192 00, 57600,115 }; // The public static void main (String [] args) {portList = comatrix ortidentifier. getPortIdentifiers (); System. out. println ("text message device port connection test... "); while (portList. hasMoreElements () {portId = (comatrix ortidentifier) portList. n ExtElement (); if (portId. getPortType () = comatrix ortidentifier. PORT_SERIAL) {System. out. println ("find the serial port:" + portId. getName (); for (int I = 0; I <bauds. length; I ++) {System. out. print ("Trying at" + bauds [I] + "... "); try {SerialPort serialPort; InputStream inStream; OutputStream outStream; int c; String response; serialPort = (SerialPort) portId. open ("SMSLibCommTester", 1971); serialPort. setFlowControlMode (Seri AlPort. FLOWCONTROL_RTSCTS_IN); serialPort. setSerialPortParams (bauds [I], SerialPort. DATABITS_8, SerialPort. STOPBITS_1, SerialPort. PARITY_NONE); inStream = serialPort. getInputStream (); outStream = serialPort. getOutputStream (); serialPort. enableReceiveTimeout (1000); c = inStream. read (); while (c! =-1) c = inStream. read (); outStream. write ('A'); outStream. write ('T'); outStream. write ('\ R'); try {Thread. sleep (1000);} catch (Exception e) {} response = ""; c = inStream. read (); while (c! =-1) {response + = (char) c; c = inStream. read ();} if (response. indexOf ("OK")> = 0) {try {System. out. print ("Get device information... "); outStream. write ('A'); outStream. write ('T'); outStream. write ('+'); outStream. write ('C'); outStream. write ('G'); outStream. write ('M'); outStream. write ('M'); outStream. write ('\ R'); response = ""; c = inStream. read (); while (c! =-1) {response + = (char) c; c = inStream. read ();} System. out. println ("device found:" + response. replaceAll ("\ s + OK \ s + ",""). replaceAll ("\ n ",""). replaceAll ("\ r", "");} catch (Exception e) {System. out. println ("no device found! ") ;}} Else System. out. println (" no device found! "); SerialPort. close ();} catch (Exception e) {System. out. println (" no device found! ");}}}}}}

Note:

1. Before using the smslib library, if your device is a USB data cable, first check whether the driver of the device is installed in the system. In the window environment, the vendor generally provides the driver of the device. in Linux, the kernel 2.6.32 or later is pre-installed with the USB-to-serial driver of a common device. if the system does not automatically recognize the device, you need to install the driver on your own.

2. During Development, org. smslib. timeoutexception: no response from device is the most common exception. For solutions, see smslib, org. smslib. timeoutexception: no response from device Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.