複製程式碼如下:
導入java.util.ArrayList;
導入java.util.List;
導入 org.apache.log4j.Logger;
導入 org.smslib.ICallNotification;
導入 org.smslib.IInboundMessageNotification;
導入 org.smslib.IOutboundMessageNotification;
導入 org.smslib.InboundMessage;
導入 org.smslib.InboundMessage.MessageClasses;
導入 org.smslib.Library;
導入 org.smslib.Message.MessageEncodings;
導入 org.smslib.Message.MessageTypes;
導入 org.smslib.OutboundMessage;
導入 org.smslib.Service;
導入 org.smslib.modem.SerialModemGateway;
/**
* @作者特里
*
*/
公共類別 SmsModem {
// 簡訊網關
私人 SerialModemGateway 閘道 = null;
java.util.ResourceBundle rb = null;//ResourceBundle.getBundle("SMS");
靜態 SmsModem smsModem = null;
OutboundNotification outboundNotification = new OutboundNotification();
私有靜態最終 Logger LOG = Logger.getLogger(SmsModem.class);
服務srv;
InboundNotification inboundNotification = new InboundNotification();
// 建立語音入站通知回呼方法。
CallNotification callNotification = new CallNotification();
公共 SmsModem() {
嘗試 {
//ReadMessages rm = new ReadMessages();
//rm.doIt();
rb = ResourceBundle.getBundle("sms");
字串連接埠名稱=“COM10”;
國際端口= 9600;
LOG.info("預設埠名稱:" + 埠名稱);
LOG.info("預設埠:" + 埠);
如果(rb!=空)
{
LOG.info("RB不為空");
if(rb.getString("smsport") != null && !"".equals(rb.getString("smsport")))
{
portName = rb.getString("smsport");
LOG.info("連接埠名稱:" + 埠名稱);
}
if(rb.getString("smsbolv") != null && !"".equals(rb.getString("smsbolv")))
{
端口 = Integer.valueOf(rb.getString("smsbolv"));
LOG.info("埠:" + 埠);
}
}
// 初始化簡訊網關
gateway = new SerialModemGateway("數據機。" + 埠名稱, 埠名稱, 埠,
“wavecom”,”17254”);
} catch (異常 e) {
LOG.error("網關初始化失敗:" + e.getMessage());
e.printStackTrace();
}
}
公用靜態 SmsModem getInstant() {
如果(smsModem == null){
smsModem = 新 SmsModem();
}
返回簡訊調製解調器;
}
公用 SerialModemGateway getGateway() {
返回網關;
}
公共無效sendMessage(字串電話,字串內容)拋出異常{
doIt(電話,內容);
}
/**
* 傳簡訊
* @參數電話
* @參數內容
* @拋出例外
*/
公共無效doIt(字串電話,字串內容)拋出異常{
OutboundMessage 訊息;
LOG.info("發送範例:從序列 gsm 數據機發送訊息。");
LOG.info(Library.getLibraryDescription());
LOG.info("發送版本:" + Library.getLibraryVersion());
如果(srv == null){
srv = 新服務();
srv.S.SERIAL_POLLING = true;
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
gateway.setInboundNotification(inboundNotification);
gateway.setCallNotification(callNotification);
srv.addGateway(網關);
srv.startService();
}
if (網關!= null) {
LOG.info("發送數據機資訊:");
LOG.info("發送的製造商:" + gateway.getManufacturer());
LOG.info("發送的模型:" + gateway.getModel());
LOG.info("發送序號:" + gateway.getSerialNo());
LOG.info("發送的 SIM IMSI: " + gateway.getImsi());
LOG.info("發送訊號電平:" + gateway.getSignalLevel() + "%");
LOG.info("發送的電池電量:" + gateway.getBatteryLevel() + "%");
}
// 同步發送訊息。
msg = new OutboundMessage(電話, 內容);
msg.setEncoding(MessageEncodings.ENCUCS2);//這句話是發中文簡訊必須的
srv.sendMessage(msg);
}
/**
* 發送訊息類
* @作者特里
*
*/
公共類別 OutboundNotification 實作 IOutboundMessageNotification {
公共無效程序(字串gatewayId,OutboundMessage訊息){
LOG.info("從網關呼叫的傳送出站處理程序:" + gatewayId);
LOG.info(訊息);
}
}
//接收訊息類
公共字串 readMessage()
{
StringBuffer sb = new StringBuffer("");
List<InboundMessage> msgList;
// 建立入站和狀態報告的通知回呼方法
// 訊息。
嘗試
{
System.out.println("讀取範例:從串列 gsm 數據機讀取訊息。");
System.out.println(Library.getLibraryDescription());
System.out.println("讀取版本:" + Library.getLibraryVersion());
// 建立新的 Service 物件 - 所有服務的父物件和主介面
// 給你。
如果(srv == null){
srv = 新服務();
srv.S.SERIAL_POLLING = true;
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
gateway.setInboundNotification(inboundNotification);
gateway.setCallNotification(callNotification);
srv.addGateway(網關);
srv.startService();
}
// 同樣,你可以定義任多個 Gateway 對象,代表
// 各種 GSM 數據機,將它們新增至服務對象並控制它們。
//
// 開始! (即連接到所有定義的網關)
LOG.info("讀取調變解調器資訊:");
LOG.info("讀取製造商:" + gateway.getManufacturer());
LOG.info("讀取模型:" + gateway.getModel());
LOG.info("讀取序號:" + gateway.getSerialNo());
LOG.info("讀取SIM卡IMSI: " + gateway.getImsi());
LOG.info("讀取訊號電平:" + gateway.getSignalLevel() + "%");
LOG.info("讀取電池電量:" + gateway.getBatteryLevel() + "%");
// 讀取訊息。讀取是透過服務對象完成的
// 影響所有定義的網關物件。這也可以更針對特定的
// 網關 - 查看 JavaDocs 以取得服務方法呼叫的資訊。
msgList = new ArrayList<InboundMessage>();
this.srv.readMessages(msgList, MessageClasses.ALL);
整數數 = 1;
for (InboundMessage 訊息:訊息清單)
{
sb.append("第" + num + "條;寄件者:"+msg.getOriginator() + ";內容:" + msg.getText() + "/n");
//sb.append(msg.toString() + "/n");
LOG.info("第" + num + "條;寄件者:"+msg.getOriginator() + ";內容:" + msg.getText() + "/n");
數++;
LOG.info(訊息);
}
// 現在睡覺。模擬現實世界的情況並給予通知機會
// 收到訊息或語音通話時呼叫的方法。
//System.out.println("正在睡眠 - 按 <enter> 終止。");
//System.in.read();
}
捕獲(異常 e)
{
sb.append(e.getMessage());
e.printStackTrace();
}
最後
{
//this.srv.stopService();
}
返回 sb.toString();
}
公共類別 InboundNotification 實作 IInboundMessageNotification
{
公共無效流程(字串gatewayId,MessageTypes msgType,InboundMessage msg)
{
if (msgType == MessageTypes.INBOUND) System.out.println(">>> 從網關偵測到新的入站訊息:" + gatewayId);
else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> 從網關偵測到新的入站狀態報告訊息:" + gatewayId);
System.out.println(msg);
嘗試
{
// 如果您希望在訊息到達時刪除訊息,請取消註解以下行。
// srv.deleteMessage(msg);
}
捕獲(異常 e)
{
System.out.println("哎呀!!!出了什麼問題......");
e.printStackTrace();
}
}
}
公共類別 CallNotification 實作 ICallNotification
{
公共無效流程(字串gatewayId,字串callerId)
{
System.out.println(">>> 從網關偵測到新呼叫: " + gatewayId + " : " + callerId);
}
}
}