64位短信猫 RXtx串行 windows平台发送短信

需要在项目里面导入smslib-3.5.4.jar(目前最新的)(注:3.0的jar包只支持1.6以下的jdk)

导入RXTXcomm.jar包

最后找到jdk和jre的bin目录下放入rxtxSerial.dll(这次用的串行,如果需要并行,则放入rxtxParallel.dll)

你也可以都放入,并不影响什么。。。。。。。

设备:

购买的设备有华为,Wavecom多种厂商的,一般厂商会在包装上写好当前设备使用的波特率(这个波特率需要写入代码内,然后我使用的是Wavecom的设备)

设备买回来,驱动肯定是要的,在Wavecom自带的检测软件内可以清楚的看到串口如:COM3

如果没有检测软件,到设备管理器里面找端口就可以看见144707_fKDo_2647908.png

代码部分:

package org.iiomchina.jwt.util.util;

import java.util.ArrayList;
import java.util.List;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.smslib.AGateway;
import org.smslib.ICallNotification;
import org.smslib.IGatewayStatusNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.IOrphanedMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.StatusReportMessage;
import org.smslib.AGateway.GatewayStatuses;
import org.smslib.AGateway.Protocols;
import org.smslib.Message.MessageEncodings;
import org.smslib.Message.MessageTypes;
import org.smslib.crypto.AESKey;
import org.smslib.modem.SerialModemGateway;

public class SendLibUtil {
	private static Log log = LogFactory.getLog("SendLibUtil");
	Service srv;
	SerialModemGateway gateway ;
	public boolean isStart=false;
	public static SendLibUtil slu=null;
	private SendLibUtil(){
	}
	public static SendLibUtil instance(){
		if(slu==null){
			//CommTest.test();
			slu=new SendLibUtil();
		}
		return slu;
	}
    public void doIt(){
       
        // Create the notification callback method for inbound & status report
        // messages.
        InboundNotification inboundNotification = new InboundNotification();

        // Create the notification callback method for inbound voice calls.
        CallNotification callNotification = new CallNotification();

        //Create the notification callback method for gateway statuses.
        GatewayStatusNotification statusNotification = new GatewayStatusNotification();

        OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();

        try {
        	log.info("Example: Read messages from a serial gsm modem.");
        	log.info(Library.getLibraryDescription());
        	log.info("Version: " + Library.getLibraryVersion());

            // Create new Service object - the parent of all and the main interface
            // to you.
            this.srv = Service.getInstance();

            // Create the Gateway representing the serial GSM modem.
            //第一个为网关,如果只有一台设备则按代码原先的写,如有多个设备需要配置不同的网关,来区别设备。
            //第二个参数就是串口
            //第三个参数是生产商,因为发送的命令每个厂家不一样,所以生产商一定不能写错
            //第四个参数设备名(可选)
            this.gateway = new SerialModemGateway("modem.com1", "COM3", 115200, "Wavecom", "WMOD2");
            //linux
            //this.gateway = new SerialModemGateway("modem.ttyUSB0", "/dev/ttyUSB0", 9600, "WAVECOM MODEM", "WMOD2");
            // 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.
            this.srv.setInboundMessageNotification(inboundNotification);
            this.srv.setCallNotification(callNotification);
            this.srv.setGatewayStatusNotification(statusNotification);
            this.srv.setOrphanedMessageNotification(orphanedMessageNotification);

            // Add the Gateway to the Service object.
            this.srv.addGateway(gateway);

            // Similarly, you 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)
            this.srv.startService();

            // Printout some general information about the modem.
            log.info("Modem Information:");
            log.info(" Manufacturer: " + gateway.getManufacturer());
            log.info(" Model: " + gateway.getModel());
            log.info(" Serial No: " + gateway.getSerialNo());
            log.info(" SIM IMSI: " + gateway.getImsi());
            log.info(" Signal Level: " + gateway.getSignalLevel() + "%");
            log.info(" Battery Level: " + gateway.getBatteryLevel() + "%");
            isStart=true;
            // 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.
            this.srv.getKeyManager().registerKey("+86111", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));

           

        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    public void getRpt() throws Exception{
    	 // Define a list which will hold the read messages.
        List<InboundMessage> msgList;
        // 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 calls.
        msgList = new ArrayList<InboundMessage>();
        this.srv.readMessages(msgList, InboundMessage.MessageClasses.ALL);
        for (InboundMessage msg : msgList) {
        	if(msg.getType()==MessageTypes.STATUSREPORT){
        		StatusReportMessage report=(StatusReportMessage)msg;
        	}
        	else{
        		log.info(msg.getText());
            	log.info(msg.getSmscNumber());
            	log.info(msg.getOriginator());
            	
        	}
        	gateway.deleteMessage(msg);
        }
    }
    public long sendMessage(String phone, String content) throws Exception {
		OutboundMessage msg = new OutboundMessage(phone, content);
		msg.setEncoding(MessageEncodings.ENCUCS2);
		msg.setStatusReport(true);
		srv.sendMessage(msg);
		log.info(msg.getRefNo());
		log.info(msg.getMessageStatus());
		return 0L;
	}
    public void close(){
    	try {
    		if(srv!=null)
    			this.srv.stopService();
    		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			isStart=false;
		}
    }
    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: " + gatewayId);
            } else if (msgType == MessageTypes.STATUSREPORT) {
                System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gatewayId);
            }
            System.out.println(msg);
        }

		public void process(AGateway arg0, MessageTypes arg1,
				InboundMessage arg2) {
			// TODO Auto-generated method stub
			
		}
    }

    public class CallNotification implements ICallNotification {

        public void process(String gatewayId, String callerId) {
            System.out.println(">>> New call detected from Gateway: " + gatewayId + " : " + callerId);
        }

		public void process(AGateway arg0, String arg1) {
			// TODO Auto-generated method stub
			
		}
    }

    public class GatewayStatusNotification implements IGatewayStatusNotification {

        public void process(String gatewayId, GatewayStatuses oldStatus, GatewayStatuses newStatus) {
            System.out.println(">>> Gateway Status change for " + gatewayId + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
        }

		public void process(AGateway arg0, GatewayStatuses arg1,
				GatewayStatuses arg2) {
			// TODO Auto-generated method stub
			
		}
    }

    public class OrphanedMessageNotification implements IOrphanedMessageNotification {

        public boolean process(String gatewayId, InboundMessage msg) {
            System.out.println(">>> Orphaned message part detected from " + gatewayId);
            System.out.println(msg);
            // Since we are just testing, return FALSE and keep the orphaned message part.
            return false;
        }

		public boolean process(AGateway arg0, InboundMessage arg1) {
			// TODO Auto-generated method stub
			return false;
		}
    }
    public static void main(String args[]) {
        SendLibUtil app = new SendLibUtil();
        try {
        	app.doIt();
        	app.sendMessage("18666666666", "小伙子,短信猫弄好了!");//电话和短信
        	app.getRpt();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	app.close();
        }
    }
	public boolean isStart() {
		return isStart;
	}
	public void setStart(boolean isStart) {
		this.isStart = isStart;
	}
}

这样就可以实现短信猫发送短信了。

 

问题:

在短信猫第一次发送成功后,第二次发送短信回出现空指针问题。

解决方案:

//在关闭服务的代码里面,取消关闭操作 
public void close(){
        System.out.println("服务端口不关闭,连发模式!");
        try {
//    		if(srv!=null)
//    			this.srv.stopService();
    		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			isStart=false;
		}
    }

public static void main(String args[]) {
        SendLibUtil app = new SendLibUtil();
        try {
        	app.doIt();
        	app.sendMessage("你的电话", "456,短信猫弄好了!");
        	app.getRpt();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //finally{
        //	app.close();
        //}
    }
//或者注释掉这个关闭操作

 

转载于:https://my.oschina.net/maxdeath/blog/809615

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值