如何在jainsip android中创建和发送从客户端到服务器的Bye消息?

时间:2015-10-12 13:04:47

标签: android sip jain-sip

我在Mobicents中尝试了jainsip示例restcomm-android-sdk.Its对我有用但是我无法正确地从客户端创建再见消息。

  

我创建了一个像这样的Bye Message类

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();

}
}

将其从SipManager类调用为

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();
    }

}

我有481个错误代码作为响应。我想我已经错过了再见消息中的当前callid字段。我已经搜索过它,但是没有找到sipmanager class.pls帮助。

1 个答案:

答案 0 :(得分:3)

Nidhin,

BYE消息始终位于SIP对话框中,因此您无需从头开始创建新消息。相反,您只需要获取要终止的对话框,从中创建BYE类型的请求并发送它。 JAIN将负责其余的工作。

例如,您可以在Mobicents restcomm-android-sdk repo上检查代码,方法sendByeClient():

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

还请记住,使用Restcomm Android Client SDK的Messenger示例已经废弃了JAIN SIP示例,该客户端SDK提供了更简单的API。以下是供您参考的代码:

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

相关问题