Search code examples
javasmslib

How to read SMS from sim in dongle using JAVA


I am using following code to send sms from dongle. Its sending Sucessfullly. Now I want to read SIM sms or unread sms from dongle so plaese can any one tell me how to read that

following is the code to send sms

import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway; 

...

private  String port = "COM4";          // Modem Port.
private  int bitRate = 9600;            // This is also optional. Leave as it is.
private  String modemName = "ZTE";      // This is optional.
private  String modemPin = "0000";      // Pin code if any have assigned to the modem.
private  String SMSC = "+919822078000"; // Message Center Number ex. Mobitel

...

SerialModemGateway gateway = new SerialModemGateway("", port, 9600, "InterCEL", "");
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
// System.out.println("center number=" + gateway.getSmscNumber());
gateway.setSmscNumber(SMSC);
gateway.setOutbound(true); 

OutboundMessage o = new OutboundMessage(number, str);
gateway.sendMessage(o);

There is InboundMessage class which takes three parameters sunch as gateway, MemoryIndexNumber, SimMemoryLocation which I am unable to get so it returns null

InboundMessage n=new InboundMessage()
gateway.readMessage(n);

If there is any other way to read sms from SIM of dongle.


Solution

  • To read the messages currently in the SIM memory, you can just do

    ArrayList<InboundMessage> msgList = new ArrayList<InboundMessage>();
    Service.getInstance().readMessages(msgList, InboundMessage.MessageClasses.ALL);
    for (InboundMessage im : msgList) {
    
    }
    

    But to do live detection of incoming messages, you need to implement org.smslib.IInboundMessageNotification

    E.g.

    import org.smslib.AGateway;
    import org.smslib.IInboundMessageNotification;
    import org.smslib.InboundMessage;
    import org.smslib.Message.MessageTypes;
    
    public class SMSInNotification implements IInboundMessageNotification
    {   
        public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg)
        {
            switch (msgType)
            {
                case INBOUND:
                    System.out.println(">>> New Inbound message detected from " + "+" + msg.getOriginator() + " " + msg.getText());
                    break;
                case STATUSREPORT:
    
                    break;
            }
        }
    }
    

    Then run these before the line which starts the service with .startService()

    gateway.setInbound(true);
    Service.getInstance().setInboundMessageNotification(new SMSInNotification());
    

    You can read more in the documentation on github https://github.com/smslib/smslib-v3/tree/master/doc