SMSLib example for interactive USSD session

对着背影说爱祢 提交于 2019-12-03 21:17:16
Chris Snow

The code at this link gives an example of sending and receiving USSD data using smslib:

// SendUSSD.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.AGateway;
import org.smslib.AGateway.Protocols;
import org.smslib.IUSSDNotification;
import org.smslib.Library;
import org.smslib.Service;
import org.smslib.USSDResponse;
import org.smslib.modem.SerialModemGateway;

public class SendUSSD
{
        public void doIt() throws Exception
        {
                Service srv;
      USSDNotification ussdNotification = new USSDNotification();
                System.out.println("Example: Send USSD Command from a serial gsm modem.");
                System.out.println(Library.getLibraryDescription());
                System.out.println("Version: " + Library.getLibraryVersion());
                srv = new Service();
                SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM4", 19200, "Huawei", "E220");
                gateway.setProtocol(Protocols.PDU);
      gateway.setInbound(true);
                gateway.setOutbound(true);
                gateway.setSimPin("0000");
      srv.setUSSDNotification(ussdNotification);
                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();

      String resp = gateway.sendUSSDCommand("*888#"); // not working
//      String resp = gateway.sendCustomATCommand("AT+CUSD=1,\"*888#\",15\r"); // working
      System.out.println(resp);

      System.out.println("Now Sleeping - Hit <enter> to terminate.");
                System.in.read();
                srv.stopService();
        }

   public class USSDNotification implements IUSSDNotification
   {
      public void process(AGateway gateway, USSDResponse response) {
                        System.out.println("USSD handler called from Gateway: " + gateway.getGatewayId());
                        System.out.println(response);
      }
   }

        public static void main(String args[])
        {
                SendUSSD app = new SendUSSD();
                try
                {
                        app.doIt();
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
        }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!