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<InboundMessage> 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<InboundMessage>();
srv.readMessages(msgList, MessageClasses.ALL);
for (InboundMessage msg : msgList) {
System.out.println(msg);
// srv.deleteMessage(msg); //刪除短信
}
System.out.println("Now Sleeping - Hit <enter> 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);即可。