Java uses smslib connection SMS cat to send SMS code sharing _java

Source: Internet
Author: User
Tags getmessage gettext stringbuffer

Copy Code code as follows:

Import java.util.ArrayList;
Import java.util.List;

Import Org.apache.log4j.Logger;
Import org.smslib.ICallNotification;
Import org.smslib.IInboundMessageNotification;
Import org.smslib.IOutboundMessageNotification;
Import Org.smslib.InboundMessage;
Import org.smslib.InboundMessage.MessageClasses;
Import Org.smslib.Library;
Import org.smslib.Message.MessageEncodings;
Import Org.smslib.Message.MessageTypes;
Import Org.smslib.OutboundMessage;
Import Org.smslib.Service;
Import Org.smslib.modem.SerialModemGateway;

/**
* @author Terry
*
*/
public class Smsmodem {

SMS Gateway
Private Serialmodemgateway gateway = null;
Java.util.ResourceBundle RB = Null;//resourcebundle.getbundle ("SMS");
static Smsmodem Smsmodem = null;
Outboundnotification outboundnotification = new Outboundnotification ();
private static final Logger LOG = Logger.getlogger (Smsmodem.class);
Service SRV;
Inboundnotification inboundnotification = new Inboundnotification ();
Create the notification callback method for inbound voice calls.
Callnotification callnotification = new Callnotification ();

Public Smsmodem () {
try {
Readmessages rm = new Readmessages ();
Rm.doit ();

RB = Resourcebundle.getbundle ("SMS");
String portname= "COM10";
int port = 9600;
Log.info ("Default portname:" + portname);
Log.info ("Default port:" + port);
if (RB!= null)
{
Log.info ("RB is not null");
if (rb.getstring ("Smsport")!= null &&! "". Equals (Rb.getstring ("Smsport"))
{
PortName = rb.getstring ("Smsport");
Log.info ("PortName:" + portname);
}
if (rb.getstring ("smsbolv")!= null &&! "". Equals (Rb.getstring ("Smsbolv"))
{
Port = integer.valueof (rb.getstring ("Smsbolv"));
Log.info ("Port:" + port);
}
}
Initialize SMS Gateway
Gateway = new Serialmodemgateway ("modem." + PortName, PortName, Port,
"WaveCom", "17254");

catch (Exception e) {
Log.error ("Gateway initialization failed:" + e.getmessage ());
E.printstacktrace ();
}
}

public static Smsmodem Getinstant () {
if (Smsmodem = = null) {
Smsmodem = new Smsmodem ();
}
return smsmodem;
}

Public Serialmodemgateway Getgateway () {
return gateway;
}

public void SendMessage (string phone, string content) throws Exception {
DoIt (phone, content);
}

/**
* Send SMS
* @param phone
* @param content
* @throws Exception
*/
public void DoIt (string phone, string content) throws Exception {

OutboundMessage msg;

Log.info ("Sent example:send message from a serial GSM modem.");
Log.info (Library.getlibrarydescription ());
Log.info ("Sent Version:" + library.getlibraryversion ());
if (SRV = null) {
SRV = new Service ();
Srv. S.serial_polling = true;
Gateway.setinbound (TRUE);
Gateway.setoutbound (TRUE);
Gateway.setsimpin ("0000");
Gateway.setoutboundnotification (outboundnotification);
Gateway.setinboundnotification (inboundnotification);
Gateway.setcallnotification (callnotification);
Srv.addgateway (Gateway);
Srv.startservice ();
}

if (gateway!= null) {
Log.info ("Sent Modem Information:");
Log.info ("Sent Manufacturer:" + gateway.getmanufacturer ());
Log.info ("Sent Model:" + Gateway.getmodel ());
Log.info ("Sent serial No:" + Gateway.getserialno ());
Log.info ("Sent SIM IMSI:" + gateway.getimsi ());
Log.info ("Sent Signal level:" + gateway.getsignallevel () + "%");
Log.info ("Sent Battery level:" + gateway.getbatterylevel () + "%");
}
Send a message synchronously.

msg = new OutboundMessage (phone, content);
Msg.setencoding (MESSAGEENCODINGS.ENCUCS2);//This sentence is necessary to send Chinese text messages
Srv.sendmessage (msg);
}

/**
* Send Message Class
* @author Terry
*
*/
public class Outboundnotification implements Ioutboundmessagenotification {
public void process (String Gatewayid, OutboundMessage msg) {
Log.info ("Sent outbound handler called from Gateway:" + Gatewayid);
Log.info (msg);
}
}
Receive Message Class
Public String Readmessage ()
{
StringBuffer sb = new StringBuffer ("");
List<inboundmessage> msglist;
Create the notification callback method for Inbound & Status
Messages.

Try
{
System.out.println ("Read example:read messages from a serial GSM modem.");
System.out.println (Library.getlibrarydescription ());
System.out.println ("Read Version:" + library.getlibraryversion ());
Create New Service object-the parent of all and the main interface
To you.
if (SRV = null) {
SRV = new Service ();
Srv. S.serial_polling = true;
Gateway.setinbound (TRUE);
Gateway.setoutbound (TRUE);
Gateway.setsimpin ("0000");
Gateway.setoutboundnotification (outboundnotification);
Gateway.setinboundnotification (inboundnotification);
Gateway.setcallnotification (callnotification);
Srv.addgateway (Gateway);
Srv.startservice ();
}


Similarly, your may define as many 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)

Log.info ("Read Modem Information:");
Log.info ("Read Manufacturer:" + gateway.getmanufacturer ());
Log.info ("Read Model:" + Gateway.getmodel ());
Log.info ("Read serial No:" + Gateway.getserialno ());
Log.info ("Read SIM IMSI:" + gateway.getimsi ());
Log.info ("Read Signal level:" + gateway.getsignallevel () + "%");
Log.info ("Read Battery level:" + gateway.getbatterylevel () + "%");
Read Messages. The reading is done via the Service object and
Affects all Gateway objects defined. This can also is more directed to a specific
Gateway-look the Javadocs for information on the Service method calls.
Msglist = new arraylist<inboundmessage> ();
This.srv.readMessages (Msglist, Messageclasses.all);
int num = 1;
for (Inboundmessage msg:msglist)
{
Sb.append ("the" "+ num +" section; Sender: "+msg.getoriginator () +"; Content: "+ msg.gettext () +" \ n ");
Sb.append (msg.tostring () + "\ n");
Log.info ("the" "+ num +" section; Sender: "+msg.getoriginator () +"; Content: "+ msg.gettext () +" \ n ");
num++;
Log.info (msg);
}
Sleep now. Emulate real world situation and give a chance to the notifications
Methods to is called in the event's message or voice call reception.
System.out.println ("Now Sleeping-hit <enter> to terminate.");
System.in.read ();
}
catch (Exception e)
{
Sb.append (E.getmessage ());
E.printstacktrace ();
}
Finally
{
This.srv.stopService ();
}
return sb.tostring ();
}

public class Inboundnotification implements Iinboundmessagenotification
{
public void process (String Gatewayid, messagetypes msgtype, Inboundmessage msg)
{
if (Msgtype = = Messagetypes.inbound) System.out.println (">>> New INBOUND message detected from Gateway:" + gatew Ayid);
else if (Msgtype = = Messagetypes.statusreport) System.out.println (">>> New Inbound Status" detecte D from Gateway: "+ Gatewayid";
SYSTEM.OUT.PRINTLN (msg);
Try
{
Uncomment following line if your wish to delete the message upon arrival.
Srv.deletemessage (msg);
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Oops!!! Something gone Bad ... ");
E.printstacktrace ();
}
}
}

public class Callnotification implements Icallnotification
{
public void process (string Gatewayid, String CallerID)
{
System.out.println (">>> New called detected from Gateway:" + Gatewayid + ":" + CallerID);
}
}

}

Related Article

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.