// SendMessage.java - Sample application.//// This application shows you the basic procedure for sending messages.// You will find how to send synchronous and asynchronous messages.//// For asynchronous dispatch, the example application sets a callback// notification, to see what's happened with messages.package examples.modem;import org.smslib.IOutboundMessageNotification;import org.smslib.Library;import org.smslib.OutboundMessage;import org.smslib.Service;import org.smslib.Message.MessageEncodings;import org.smslib.modem.SerialModemGateway;public class SendMessage{ public void doIt(String PhoneNum,String Mesg) throws Exception { Service srv; //聲明一個服務 OutboundMessage msg; //OutboundMessage繼承IOutboundMessageNotification介面,重寫了process方法,用於網關設定回呼函數 OutboundNotification outboundNotification = new OutboundNotification(); System.out.println("Example: Send message from a serial gsm modem."); System.out.println(Library.getLibraryDescription());//擷取類庫描述 System.out.println("Version: " + Library.getLibraryVersion());//擷取類庫版本 srv = new Service(); //初始化服務 //115200是傳輸速率,一般為9600。可以通過超級終端測試出來,聲明網關 第一個參數為:網關ID,第二個是本機上簡訊貓的com口名稱,第三是傳輸速率,第四是簡訊貓生產廠商,第五裝置的型號(可選) SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM7", 9600, "Wavecom", "17254"); gateway.setInbound(true);//設定true,表示該網關可以接收簡訊,根據需求修改 gateway.setOutbound(true);//設定true,表示該網關可以傳送簡訊,根據需求修改 gateway.setSimPin("0000");//sim卡鎖,一般預設為0000或1234 gateway.setOutboundNotification(outboundNotification);//傳送簡訊成功後的回調函方法 srv.addGateway(gateway); //將網關添加到簡訊貓服務中 srv.startService(); //啟動服務,進入簡訊發送就緒狀態 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(); // Send a message synchronously. msg = new OutboundMessage(PhoneNum, Mesg); msg.setEncoding(MessageEncodings.ENCUCS2);//這句話是發中文簡訊必須的 srv.sendMessage(msg); System.out.println(msg); System.out.println("Now Sleeping - Hit <enter> to terminate."); System.in.read(); srv.stopService(); } public class OutboundNotification implements IOutboundMessageNotification { public void process(String gatewayId, OutboundMessage msg) { System.out.println("Outbound handler called from Gateway: " + gatewayId); System.out.println(msg); } } public static void main(String args[]) { SendMessage app = new SendMessage(); try { app.doIt("13215243120","Hello,test content"); } catch (Exception e) { e.printStackTrace(); } }}
需要兩個jar包
首先,把smslib-3.3.0b2.jar和comm.jar,放入工程lib中,javax.comm.properties放到%JAVA_HOME%/jre/lib下,
win32com.dll放到%JAVA_HOME%/jre/bin下。路徑放錯了,調用起來就會報錯的。
環境配置好了以後,把examples\modem下的SendMessage.java和ReadMessages.java拷貝到你的開發工具下