hey guys, here is a solution for interactive ussd session :) 

-- 
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/7e514180-f035-491f-8b6f-f6cdf2e0885c%40googlegroups.com.
package interaction.ussd;

import java.io.IOException;
import java.util.Scanner;

import org.ajwcc.pduUtils.gsm3040.PduUtils;
import org.smslib.AGateway;
import org.smslib.IUSSDNotification;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.USSDResponse;
import org.smslib.modem.SerialModemGateway;

public class USSDNotification implements IUSSDNotification {
    SerialModemGateway gateway;
    Scanner            sc;
    SendUSSD           send;
    Service            srv;
    String             s;

    USSDNotification( SerialModemGateway gateway, SendUSSD send, Service srv, 
String s ) {
        this.gateway = gateway;
        this.send = send;
        this.srv = srv;
        this.s = s;

    }

    public void process( AGateway gateway, USSDResponse response ) {
        System.out.println( "USSD handler called from Gateway: " + 
gateway.getGatewayId() );
        String dataToSend = null;
        byte[] biteToSend = null;
        biteToSend = PduUtils.pduToBytes( response.getContent() );
        dataToSend = PduUtils.decode7bitEncoding( biteToSend );
        System.out.println( dataToSend );
        // String s=PduUtils.encodedSeptetsToString( response.get )
        System.out.println( response.getSessionStatus().getNumeric() );
        if ( response.getSessionStatus().getNumeric() == 1 ) {
            this.s = "Donner une réponse svp...";
            try {
                this.send.envoieUSSD( s );

            } catch ( Exception e ) {
                System.out.println( e.getMessage() );
            }
        } else {
            try {
                srv.stopService();
            } catch ( SMSLibException | IOException | InterruptedException e ) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
package interaction.ussd;

import org.smslib.AGateway.Protocols;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

public class main1 {

    /**
     * @param args
     * @throws Exception
     */
    public static void main( String[] args ) throws Exception {
        String s = "Entrer une requette USSD svp ..";
        Service srv = Service.getInstance();
        SerialModemGateway gateway;
        USSDNotification ussdNotification;

        // modemgsm
        gateway = new SerialModemGateway( "modem.com8", "COM8", 460800, 
"HWAWEI", "E303" );
        gateway.setProtocol( Protocols.PDU );
        gateway.setInbound( true );
        gateway.setOutbound( true );

        // Service smslib Api
        srv = Service.getInstance();
        SendUSSD send = new SendUSSD( gateway );
        // Notification USSD
        ussdNotification = new USSDNotification( gateway, send, srv, s );
        srv.setUSSDNotification( ussdNotification );
        srv.addGateway( gateway );

        srv.startService();

        send.doIt( s );

        // System.out.println( "rahi jouze ya hbibna" );
        // srv.stopService();
        // System.out.println( "rahi jouze ya hbibna" );
    }
}
package interaction.ussd;

import java.util.Scanner;

import org.ajwcc.pduUtils.gsm3040.PduUtils;
import org.smslib.Library;
import org.smslib.modem.AModemDriver;
import org.smslib.modem.SerialModemGateway;

public class SendUSSD {
    AModemDriver       modemDriver;
    Scanner            sc;
    SerialModemGateway gateway;

    SendUSSD( SerialModemGateway gateway ) {

        this.gateway = gateway;
    }

    public AModemDriver getModemDriver()
    {
        return this.modemDriver;
    }

    public void doIt( String req ) throws Exception
    {

        System.out.println( "Version: " + Library.getLibraryVersion() );

        this.modemDriver = gateway.getModemDriver();
        // gateway.setSimPin( "0000" );

        envoieUSSD( req );

    }

    public void envoieUSSD( String req ) throws Exception
    {
        byte[] dataToSend = null;
        byte[] biteToSend = null;
        sc = new Scanner( System.in );
        System.out.println( req );
        String stringtoSend = sc.nextLine();

        dataToSend = PduUtils.stringToUnencodedSeptets( stringtoSend );
        biteToSend = PduUtils.encode7bitUserData( null, dataToSend );
        stringtoSend = PduUtils.bytesToPdu( biteToSend );

        System.out.println( stringtoSend );
        // AT+CMGF=1
        // ATZ

        String resp2 = this.gateway.sendCustomATCommand( "AT+CUSD=1,\"" + 
stringtoSend + "\",15\r" );
        System.out.println( "REPONSE1 : " + resp2 );

        System.out.println( "Now Sleeping - Hit <enter> to terminate." );

    }

    public void envoieUSSD2() throws Exception
    {

        // sc = new Scanner( System.in );
        // System.out.println( req );
        String stringtoSend = "at+cops?";

        // AT+CMGF=1
        // ATZ

        String resp2 = this.gateway.sendCustomATCommand( stringtoSend );
        System.out.println( "REPONSE1 : " + resp2 );

        System.out.println( "Now Sleeping - Hit <enter> to terminate." );

    }
}
  • [smslib] Solution for u... mechahed nour el islam via SMSLib Discussion Group

Reply via email to