Hi !
I'm using smslib-3.5.4 , but i have a probleme on sending Ussd command and
reading the response .
The response of ussd command should be:
You have $ 200 in your account
but I have as response "OK";
How to send USSD command and how to read the received response ?
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ajwcc.pduUtils.gsm3040.PduUtils;
import org.smslib.AGateway;
import org.smslib.AGateway.AsyncEvents;
import org.smslib.AGateway.Protocols;
import org.smslib.GatewayException;
import org.smslib.IUSSDNotification;
import org.smslib.Library;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.USSDResponse;
import org.smslib.modem.AModemDriver;
import org.smslib.modem.SerialModemGateway;
public class SendUSSD {
AModemDriver modemDriver;
public AModemDriver getModemDriver() {
return this.modemDriver;
}
public void doIt() throws Exception {
USSDNotification ussdNotification = new USSDNotification();
System.out.println("Version: " + Library.getLibraryVersion());
SerialModemGateway gateway = new SerialModemGateway("modem.com8",
"COM8", 115200, "HUAWEI", "E153");
gateway.setProtocol(Protocols.PDU);
gateway.setInbound(true);
gateway.setOutbound(true);
this.modemDriver = gateway.getModemDriver();
// gateway.setSimPin("0000");
Service.getInstance().addGateway(gateway);
Service.getInstance().setUSSDNotification(ussdNotification);
Service.getInstance().startService();
byte[] dataToSend = null;
byte[] biteToSend = null;
String stringtoSend = "*100*1#";
dataToSend = PduUtils.stringToUnencodedSeptets(stringtoSend);
biteToSend = PduUtils.encode7bitUserData(null, dataToSend);
stringtoSend = PduUtils.bytesToPdu(biteToSend);
System.out.println(stringtoSend);
String resp = gateway.sendCustomATCommand("AT+CUSD=1,\"" + stringtoSend
+ "\",15\r");
System.out.println("REPONSE1 : " + resp);
System.out.println("Now Sleeping - Hit <enter> to terminate.");
System.in.read();
Service.getInstance().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 String sendUSSDRequest(SerialModemGateway gateway,
String presentation, String content, String dcs)
throws GatewayException, TimeoutException, IOException,
InterruptedException {
String rawRequest = formatUSSDCommand(presentation, content, dcs);
System.out.println(rawRequest);
String ussdResponse = gateway.sendCustomATCommand(rawRequest);
if (!ussdResponse.contains("OK")) {
return ussdResponse;
}
return ussdResponse;
}
private String formatUSSDCommand(String ussdCommand) {
return formatUSSDCommand("1", ussdCommand, null);
}
private String formatUSSDCommand(String presentation, String content,
String dcs) {
StringBuffer buf = new StringBuffer();
buf.append("AT+CUSD=");
buf.append(presentation);
buf.append(",");
buf.append("\"");
buf.append(content);
buf.append("\"");
if (dcs != null && dcs.length() > 0) {
buf.append(",");
buf.append(dcs);
buf.append("\r");
}
buf.append("\r");
return buf.toString();
}
public String formatUSSDResponse(String ussdResponse) {
// noop for most modems but some may require additional processing e.g.
// pdudecode
return ussdResponse;
}
public static void main(String args[]) {
SendUSSD app = new SendUSSD();
try {
app.doIt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
--
You received this message because you are subscribed to the Google Groups
"SMSLib Discussion Group" group.
To view this discussion on the web visit
https://groups.google.com/d/msgid/smslib/b3d385f9-6e15-4bf3-a795-83347212e827%40googlegroups.com.
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ajwcc.pduUtils.gsm3040.PduUtils;
import org.smslib.AGateway;
import org.smslib.AGateway.AsyncEvents;
import org.smslib.AGateway.Protocols;
import org.smslib.GatewayException;
import org.smslib.IUSSDNotification;
import org.smslib.Library;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.USSDResponse;
import org.smslib.modem.AModemDriver;
import org.smslib.modem.SerialModemGateway;
public class SendUSSD {
AModemDriver modemDriver;
public AModemDriver getModemDriver()
{
return this.modemDriver;
}
public void doIt() throws Exception
{
USSDNotification ussdNotification = new USSDNotification();
System.out.println("Version: " + Library.getLibraryVersion());
SerialModemGateway gateway = new SerialModemGateway("modem.com8","COM8", 115200, "HUAWEI", "E153");
gateway.setProtocol(Protocols.PDU);
gateway.setInbound(true);
gateway.setOutbound(true);
this.modemDriver = gateway.getModemDriver();
//gateway.setSimPin("0000");
Service.getInstance().addGateway(gateway);
Service.getInstance().setUSSDNotification(ussdNotification);
Service.getInstance().startService();
byte[] dataToSend = null;
byte[] biteToSend = null;
String stringtoSend = "*100*1#";
dataToSend = PduUtils.stringToUnencodedSeptets(stringtoSend);
biteToSend = PduUtils.encode7bitUserData(null, dataToSend);
stringtoSend = PduUtils.bytesToPdu(biteToSend);
System.out.println(stringtoSend);
String resp = gateway.sendCustomATCommand("AT+CUSD=1,\""+stringtoSend+"\",15\r");
System.out.println("REPONSE1 : "+resp);
System.out.println("Now Sleeping - Hit <enter> to terminate.");
System.in.read();
Service.getInstance().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 String sendUSSDRequest(SerialModemGateway gateway,String presentation, String content, String dcs) throws GatewayException, TimeoutException, IOException, InterruptedException
{
String rawRequest = formatUSSDCommand(presentation, content, dcs);
System.out.println(rawRequest);
String ussdResponse = gateway.sendCustomATCommand(rawRequest);
if (!ussdResponse.contains("OK"))
{
return ussdResponse;
}
return ussdResponse;
}
private String formatUSSDCommand(String ussdCommand)
{
return formatUSSDCommand("1", ussdCommand, null);
}
private String formatUSSDCommand(String presentation, String content, String dcs)
{
StringBuffer buf = new StringBuffer();
buf.append("AT+CUSD=");
buf.append(presentation);
buf.append(",");
buf.append("\"");
buf.append(content);
buf.append("\"");
if (dcs != null && dcs.length() > 0)
{
buf.append(",");
buf.append(dcs);
buf.append("\r");
}
buf.append("\r");
return buf.toString();
}
public String formatUSSDResponse(String ussdResponse)
{
// noop for most modems but some may require additional processing e.g. pdudecode
return ussdResponse;
}
public static void main(String args[])
{
SendUSSD app = new SendUSSD();
try
{
app.doIt();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}