Java serial device (SMS cat) SMS sending

Source: Internet
Author: User

Java provides two methods to send SMS messages: One is through the carrier's gateway, and the other is through the SMS cat, which is not very complicated (this article mainly introduces ). I. SMS sending methods (2 types) 1. in Java programming, you can use Java comm class for serial communication between mobile phones and computers, and use at commands to control mobile phone operations. (Bottom-layer mode) 2. The smslib project encapsulates the underlying AT Instruction Set and does not need to write the underlying at command to communicate with the mobile phone. (Smslib Development Kit is required. If there is no complete API documentation, you can only view the sample code. The sample code must be supported by slf4j (similar to log4j) and Java comm) 2. The required Java comm file and configuration method :( 1) download file: javacomm20-win32.zip java_home % is the JDK path, rather than JRE. Smslib only supports jdk1.6 and later. 1. Comm. jar and provides Java APIs for communication. Copy to the % java_home % \ JRE \ Lib \ ext directory. 2. win32com. dll provides a local driver interface called by comm. jar. Copy to % java_home % \ bin 3. javax. Comm. properties is the class configuration file of this driver. Copy to the % java_home % \ JRE \ lib directory. (2) download smslib. 3. Development steps: (1) Create and configure related objects and start services. // create service objects. (There is only one service object, but there can be multiple gateways) This. SRV = new service (); // create a GSM Modem type to replace the gateway (you can also create another type ). Serialmodemgateway gateway = new serialmodemgateway ("modem. COM1 "," COM1 ", 57600," Nokia "," "); gateway. setprotocol (protocols. PDU); // set the PDU protocol (PDU is the default protocol, you can also set it to text simple text protocol ). gateway. setinbound (true); // sets whether the gateway can receive messages (inbound) gateway. setoutbound (true); // sets whether the gateway can send messages (outbound) gateway. setsimpin ("0000"); // Let smslib know which is the sim pin. // The event-driven mechanism is used for asynchronous processing of short messages, as shown in the following code: // set the callback class of the event (the following only includes part of the callback class. each event is a class that implements the corresponding interface and implements its process method ). th Is. SRV. setinboundmessagenotification (inboundnotification); // function for receiving the SMS event this. SRV. setcallnotification (callnotification); // receives the call event function this. SRV. setgatewaystatusnotification (statusnotification); // Gateway status change event function this. SRV. setorphanedmessagenotification (orphanedmessagenotification); this. SRV. addgateway (GATEWAY); // Add the gateway to the service object. this. SRV. startservice (); // start the service object. (2) Synchronous reading of text messages: msglist = new arraylist (); // reading of information (main method)) -- Two Parameters specify the location to be saved and the type of the text message to be read, such as read, unread, and information report. This. SRV. readmessages (msglist, messageclasses. all); For (inboundmessage MSG: msglist) system. out. println (MSG); there are two ways to read messages: the synchronous way: Just call the readmessages () method of the service class. smslib will iterate all defined methods, collect messages from all and return them in a collection. note that you will be blocked until smslib returns. there are terraform S of the readmessages () call-choose the one according to your needs. the asynchronous way: you can implement some callback methods in your code (listeners) and pass them to the service. smslib will then automatically call you upon message exception. for more information, see callback methods. synchronous read: This. SRV. readmessages (msglist, messageclasses. all); asynchronous reading: Read in the event processing function. (3) Send text message there are two ways Send a message: the synchronous way: create a message object and call the sendmessage () method of the service class. note that you will be blocked until smslib returns. upon return, you can examine the message object to see the fate of your message. the asynchronous way: Use the queuemessage () family of methods to queue your message. queueing a message returns control immediately to you. smslib Ke EPS this message in internal queues and send its in the background. to learn about the fate of your message, you should setup a ioutboundmessagenotification callback method (see callback methods ). synchronous sending: sendmessage () asynchronous sending: queuemessage () Glossary: Introduction to the concept of Gateway: Gateway is a device or service interface used to send and receive SMS messages, A gateway can be a GSM Modem or a service provider. smslib can process multiple gateways at the same time to send priority messages. smslib can send SMS messages with an unlimited priority, (It is a policy for sending multiple text messages. The smslib priority policy corresponds to your gateway (GSM Modem or SMS provider), unless they are fake photos. Asynchronous and synchronous receiving of smslib messages can be received in two ways: 1. synchronous receiving: You can call the read () method at any time to receive messages. 2. asynchronous reception: wait for smslib to automatically receive SMS messages when the SMS arrives. Asynchronous and synchronous sending of smslib can also send text messages in two ways. synchronous sending: when your app uses smslib to send a text message, the app thread will be locked until the text message is sent completely or fails to be sent. 2. asynchronous sending: when you use this method to send a text message, the app thread will not be locked, smsliib will return immediately, and smslib will send your text message (s) in the background. You can choose whether to remind you of the status of the text message. When a new event occurs in the callback function (a new message is received, or an SMS is successfully sent or fails), smslib can define the callback method (event-driven) inbound voice cballs: when a call arrives, you can set whether to remind you. Please note that this call will automatically and unconditionally automatically end up inbound messages: whenever a new text message comes, smlib can be used to call your specified method outbound messages: When you use asynchronous message sending, you can call your specified method to obtain the status information of sent messages. Gateway status changes: smslib can remind you that each time the gateway changes the status, queue sending operation: smslib can send a text message at any time. It can get the sending status of the current text message anytime, anywhere.

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.