ubuntu服務器安裝短信貓

到http://smslib.org/download/下載安裝文件

1.將comm.jar和smslib-3.5.1.jar放到web項目的WEB-INF下的lib目錄;

2、將javax.comm.properties放到JDK安裝的jre的lib目錄中,例如E:\work\software\javahome\jre\lib;
3、將libLinuxSerialParallel_g.so和libLinuxSerialParallel.so放到JDK安裝的jre的bin目錄下,例如E:\work\software\javahome\jre\bin

配置文件就寫好了。你可以安裝linux超級終端minicom(sudo apt-get install minicom安裝),然後把gnokii裝起來(sudo apt-get install gnokii)用AT命令發送一下短信,看自己是否配置正確。

1.發送短信主要代碼

public void sendSMS(String number, String content) throws GatewayException, IOException {

String mobilePhones = "18312349876";
String content = "123yff";
System.out.println("hgj=sms##sendSMS##mobilePhones="+mobilePhones+",content="+content+"\n");

Service srv;
OutboundMessage msg;
srv = Service.getInstance();
srv.S.SERIAL_POLLING=true;//啓用輪循模式
SerialModemGateway gateway = new SerialModemGateway("modem.com7", "/dev/ttyUSB1", 9600, "wavecom", "");//115200是波特率,一般爲9600。可以通過超級終端測試出來
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
srv.addGateway(gateway);
System.out.println("hgj=sms##sendSMS##initService success!\n");
try {
srv.startService();
System.out.println("hgj=sms##sendSMS##startService success!\n");
String[] phones = mobilePhones.split(",");
for (int i = 0; i < phones.length; i++) {
msg = new OutboundMessage(phones[i], content);
msg.setEncoding(MessageEncodings.ENCUCS2); // 中文
msg.setDate(new Date());
msg.setMessageStatus(OutboundMessage.MessageStatuses.SENT);
srv.sendMessage(msg);
System.out.println("hgj=sms##sendSMS##msg="+msg+"\n");
}
srv.stopService();
} catch (Exception e) {
e.printStackTrace();
}
}


2.接收短信主要代碼

private Service srv;
public void acceptSMS() throws IOException, InterruptedException, SMSLibException {
if(srv!=null&&srv.getServiceStatus()!= ServiceStatus.STOPPED){ //該判斷是爲了重啓
srv.stopService();
Collection<AGateway> smgList = srv.getGateways();
List<SerialModemGateway> ss = new ArrayList<SerialModemGateway>();
for(AGateway gateway:smgList){
SerialModemGateway smg = (SerialModemGateway)gateway;
ss.add(smg);
}
for(SerialModemGateway s :ss){
srv.removeGateway(s);
}
srv = null;
}
srv = Service.getInstance();// 創建服務
srv.S.SERIAL_POLLING=true;//啓用輪循模式
// 實例化短信接入監聽 
InboundMessageNotification inboundMessageNotification = new InboundMessageNotification();
srv.setInboundMessageNotification(inboundMessageNotification);//添加短信監聽

for(int i=5;i<8;i++){ //我們的設備有8個USB端口
// 設置相關參數(一般情況下以下默認參數不要變)
if(i==6){
continue;
}
SerialModemGateway gateway = new SerialModemGateway("modem.com"+i,"/dev/ttyUSB"+i, 9600, "wavecom", "");
// 設置短信接收狀態爲可接收
gateway.setInbound(true);
// 設置短信發送狀態爲可發送
gateway.setOutbound(true);
gateway.setSimPin("0000");
// 添加網關參數到服務
srv.addGateway(gateway);
}
try{
srv.startService();
System.out.println("acceptSMS##startService success!\n");
}catch (Exception e){
System.out.println("acceptSMS##startService exception!\n");
e.printStackTrace();
}
}

private class InboundMessageNotification implements IInboundMessageNotification
{
@SuppressWarnings("unchecked")
public void process(AGateway agw, MessageTypes messageTypes,InboundMessage message) {

List msgList;
String Originator = message.getOriginator(); //短信發送號碼
if (messageTypes == MessageTypes.INBOUND)  //收到短信
{
try
{
msgList = new ArrayList();
srv.readMessages(msgList, MessageClasses.ALL); //  讀取短信  
for (int i = 0; i < msgList.size(); i++){
InboundMessage acceptsms = (InboundMessage)msgList.get(i);
String smstime = sdf.format(acceptsms.getDate());  //接收短信的時間
Originator = acceptsms.getOriginator();  //電話號碼
String text = acceptsms.getText(); //短信內容
System.out.println("acceptSMS##Originator="+Originator+",text="+text);
//srv.deleteMessage(acceptsms);// 插入成功後刪除當前接收到的信息
}
}
catch (Exception e)
{
System.out.println("acceptSMS##srv.readMessages exception !\n");
e.printStackTrace();
}
}else if (messageTypes == MessageTypes.STATUSREPORT)
{
System.out.println("acceptSMS##dev exception : id = " +message.getGatewayId() );
}
}
}


參考文獻:http://blog.sina.com.cn/s/blog_9f1c09310101a4i6.html


發佈了27 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章