短信猫接收与发送短信整理

一、主要就用到三个包:

1、log4j-1.2.16.jar
2、smslib-3.5.1.jar
3、comm.jar(这个不需要拷到lib下面)

二、在Windows环境下使用SMSLib编程的时候,我们需要做一下comm的配置:

1、将win32com.dll放置在%JAVA_HOME%\jre\bin下
2、将comm.jar放置在%JAVA_HOME%\jre\lib\ext下
3、将javax.comm.properties放置在%JAVA_HOME%\jre\lib下
上面三个文件可以去http://smslib.googlecode.com/files/javacomm20-win32.zip这里下载

然后下面是两个发送与接收短信的示例代码(经过一些修改)

SendMessage.java

package ob;

import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

public class SendMessage {
	public class OutboundNotification implements IOutboundMessageNotification {
		public void process(AGateway agateway, OutboundMessage outboundmessage) {
			System.out.println("Outbound handler called from Gateway: " + agateway);
			System.out.println(outboundmessage);
			
		}
	}

	@SuppressWarnings("deprecation")
	public void sendSMS(String mobilePhones, String content) throws GatewayException {
		Service srv;
		OutboundMessage msg;
		OutboundNotification outboundNotification = new OutboundNotification();
//		srv = new Service();
		srv = Service.getInstance();
		SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "wavecom", ""); // 设置端口与波特率
		gateway.setInbound(true);
		gateway.setOutbound(true);
		gateway.setSimPin("1234");
//		gateway.setOutboundNotification(outboundNotification);
		srv.setOutboundMessageNotification(outboundNotification);
		srv.addGateway(gateway);
		System.out.println("初始化成功,准备开启服务");
		try {
			srv.startService();
			System.out.println("服务启动成功");
			String[] phones = mobilePhones.split(",");
			for (int i = 0; i < phones.length; i++) {
				msg = new OutboundMessage(phones[i], content);
				msg.setEncoding(MessageEncodings.ENCUCS2); // 中文
				srv.sendMessage(msg);
			}
			srv.stopService();
                        srv.removeGateway(gateway);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws GatewayException {
		SendMessage sendMessage = new SendMessage();
		sendMessage.sendSMS("13808080808", "短信内容");
	}
}

ReadMessages.java

package ob;

import java.util.ArrayList;
import java.util.List;
import javax.crypto.spec.SecretKeySpec;
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.Service;
import org.smslib.AGateway.GatewayStatuses;
import org.smslib.AGateway.Protocols;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Message.MessageTypes;
import org.smslib.crypto.AESKey;
import org.smslib.modem.SerialModemGateway;

public class ReadMessages {
	public static Service srv = Service.getInstance();

	public void doIt() throws Exception {
		List msgList;
		InboundNotification inboundNotification = new InboundNotification();
		CallNotification callNotification = new CallNotification();
		GatewayStatusNotification statusNotification = new GatewayStatusNotification();
		OrphanedMessageNotification 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());
			SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, null, null);
			gateway.setProtocol(Protocols.PDU);
			gateway.setInbound(true);
			gateway.setOutbound(true);
			srv.setInboundMessageNotification(inboundNotification);
			srv.setCallNotification(callNotification);
			srv.setGatewayStatusNotification(statusNotification);
			srv.setOrphanedMessageNotification(orphanedMessageNotification);
			srv.addGateway(gateway);
			srv.startService();
			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() + "%");
			System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");
			System.out.println();
			srv.getKeyManager().registerKey("+8613808080808", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));
			msgList = new ArrayList();
			srv.readMessages(msgList, MessageClasses.ALL);
			for (InboundMessage msg : msgList) {
				System.out.println(msg);
//				srv.deleteMessage(msg);		//删除短信
			}
			System.out.println("Now Sleeping - Hit  to stop service.");
			System.in.read();
			System.in.read();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	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 IOrphanedMessageNotification {
		public boolean process(AGateway gateway, InboundMessage msg) {
			System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId());
			System.out.println(msg);
			return false;
		}
	}

	public static void main(String args[]) {
		ReadMessages app = new ReadMessages();
		try {
			app.doIt();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

上面代码在第一次运行都是正常的,有个问题是,用的是usb接口的短信猫,好像是停止不了服务的,第二次接收或者发送,总会报一个错:

org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by org.smslib
    at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
    at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
    at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
    at org.smslib.Service$1Starter.run(Service.java:276)

找了很多资料,都没找到解决方法。估计只有串口的短信猫设备才能正常。。

目前是把Service一直开启,不做停止,然后一直重复读取短信,弄了个循环,临时解决。

会报错是因为没有移除端口,在srv.stopService();后面加一句srv.removeGateway(gateway);即可。


你可能感兴趣的:(短信猫)