Nidhin
Nidhin

Reputation: 1838

How to create and send Bye message from client to server in jainsip android?

I have tried jainsip example in Mobicents restcomm-android-sdk.Its worked for me but i am not able create bye message from client side properly.

I created a Bye Message class like this

public class Bye {

public Request MakeRequest(SipManager sipManager) throws ParseException,
        InvalidArgumentException {

    AddressFactory addressFactory = sipManager.addressFactory;
    SipProvider sipProvider = sipManager.sipProvider;
    MessageFactory messageFactory = sipManager.messageFactory;
    HeaderFactory headerFactory = sipManager.headerFactory;
    // Create addresses and via header for the request
    Address fromAddress = addressFactory.createAddress("sip:"
            + sipManager.getSipProfile().getSipUserName() + "@"
            + sipManager.getSipProfile().getRemoteIp());
    fromAddress.setDisplayName(sipManager.getSipProfile().getSipUserName());
    Address toAddress = addressFactory.createAddress("sip:"
            + sipManager.getSipProfile().getSipUserName() + "@"
            + sipManager.getSipProfile().getRemoteIp());
    toAddress.setDisplayName(sipManager.getSipProfile().getSipUserName());

    Address contactAddress = sipManager.createContactAddress();
    ArrayList<ViaHeader> viaHeaders = sipManager.createViaHeader();
    URI requestURI = addressFactory.createAddress(
            "sip:" + sipManager.getSipProfile().getRemoteEndpoint())
            .getURI();
    // Build the request
    CallIdHeader callIdHeader = sipManager.sipProvider.;
    final Request request = messageFactory.createRequest(requestURI,
            Request.BYE, sipProvider.getNewCallId(),
            headerFactory.createCSeqHeader(1l, Request.BYE),
            headerFactory.createFromHeader(fromAddress, "c3ff411e"),
            headerFactory.createToHeader(toAddress, null), viaHeaders,
            headerFactory.createMaxForwardsHeader(70));

    // Add the contact header
    request.addHeader(headerFactory.createContactHeader(contactAddress));
    ExpiresHeader eh = headerFactory.createExpiresHeader(300);
    request.addHeader(eh);
    // Print the request
    System.out.println(request.toString());
    return request;
    // Send the request --- triggers an IOException
    // sipProvider.sendRequest(request);
    // ClientTransaction transaction = sipProvider
    // .getNewClientTransaction(request);
    // Send the request statefully, through the client transaction.
    // transaction.sendRequest();

}
}

Call it From SipManager class as

public void disconnectCall() throws NotInitializedException {
    // TODO Auto-generated method stub
    if (!initialized)
        throw new NotInitializedException("Sip Stack not initialized");
    this.sipManagerState = SipManagerState.BYE;
    Bye byeRequest = new Bye();
    Request r=null ;
    try{
    r = byeRequest.MakeRequest(this);//byeRequest.MakeRequest(SipManager.this);

        final ClientTransaction transaction = this.sipProvider
                .getNewClientTransaction(r);
        Thread thread = new Thread() {
            public void run() {
                try {
                    transaction.sendRequest();
                } catch (SipException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    } catch (TransactionUnavailableException e) {
        e.printStackTrace();
    }catch (InvalidArgumentException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

I have got 481 error code as response.I think I have missed the current callid field in the bye message.I have searched for it but not found from sipmanager class.pls help.

Upvotes: 2

Views: 800

Answers (1)

atsakiridis
atsakiridis

Reputation: 1012

Nidhin,

BYE messages are always inside a SIP dialog, so you don't have to create a new message from scratch. Instead, you just need to get ahold of the dialog you want to terminate, create a request of type BYE from that and send it. JAIN will take care of the rest.

For an example, you can check the code at the Mobicents restcomm-android-sdk repo, method sendByeClient():

https://github.com/Mobicents/restcomm-android-sdk/blob/master/sipua/src/main/java/org/mobicents/restcomm/android/sipua/impl/SipManager.java#L931

Please also keep in mind that the JAIN SIP example has been obsoleted by Messenger example that uses the Restcomm Android Client SDK which offers a simpler API. Here's its code for your reference:

https://github.com/Mobicents/restcomm-android-sdk/tree/master/Examples/restcomm-messenger

Upvotes: 3

Related Questions