添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接


2.设置权限:参考 Android-Sip_开发介绍(1)

Step -1: 初始化

创建一个单例类SipStackAndroid并初始化一个SIP堆栈。 这个类将实现SipListener和相应的方法来处理SIP事件。

public class SipStackAndroid implements SipListener {

private static SipStackAndroid instance = null;

public static SipStack sipStack;

public static SipProvider sipProvider;

public static HeaderFactory headerFactory;

public static AddressFactory addressFactory;

public static MessageFactory messageFactory;

public static SipFactory sipFactory;

public static ListeningPoint udpListeningPoint;

public static String localIp;

public static int localPort = 5080;

public static String localEndpoint = localIp + ":" + localPort;

public static String transport = "udp";

public static String remoteIp = "23.23.228.238";

public static int remotePort = 5080;

public static String remoteEndpoint = remoteIp + ":" + remotePort;

public static String sipUserName;

public String sipPassword;

protected SipStackAndroid() {

initialize();

}

public static SipStackAndroid getInstance() {

if (instance == null) {

instance = new SipStackAndroid();

}

return instance;

}

private static void initialize() {

localIp = getIPAddress(true);

localEndpoint = localIp + ":" + localPort;

remoteEndpoint = remoteIp + ":" + remotePort;

sipStack = null;

sipFactory = SipFactory.getInstance();

sipFactory.setPathName("android.gov.nist");

Properties properties = new Properties();

properties.setProperty("javaxx.sip.OUTBOUND_PROXY", remoteEndpoint + "/"

+ transport);

properties.setProperty("javaxx.sip.STACK_NAME", "androidSip");

try {

// Create SipStack object

sipStack = sipFactory.createSipStack(properties);

System.out.println("createSipStack " + sipStack);

} catch (PeerUnavailableException e) {

e.printStackTrace();

System.err.println(e.getMessage());

System.exit(0);

}

try {

headerFactory = sipFactory.createHeaderFactory();

addressFactory = sipFactory.createAddressFactory();

messageFactory = sipFactory.createMessageFactory();

udpListeningPoint = sipStack.createListeningPoint(localIp,

localPort, transport);

sipProvider = sipStack.createSipProvider(udpListeningPoint);

sipProvider.addSipListener(SipStackAndroid.getInstance());

// this.send_register();

} catch (PeerUnavailableException e) {

e.printStackTrace();

System.err.println(e.getMessage());

System.exit(0);

} catch (Exception e) {

System.out.println("Creating Listener Points");

System.out.println(e.getMessage());

e.printStackTrace();

}

}

}


Step -2:发送注册

以下方法发送一个注册包到服务器。

private void send_register() {

try {

System.out.println();

SipStackAndroid.getInstance();

AddressFactory addressFactory = SipStackAndroid.addressFactory;

SipStackAndroid.getInstance();

SipProvider sipProvider = SipStackAndroid.sipProvider;

SipStackAndroid.getInstance();

MessageFactory messageFactory = SipStackAndroid.messageFactory;

SipStackAndroid.getInstance();

HeaderFactory headerFactory = SipStackAndroid.headerFactory;

// Create addresses and via header for the request

Address fromAddress = addressFactory.createAddress("sip:"

+ SipStackAndroid.sipUserName + "@" + SipStackAndroid.remoteIp);

fromAddress.setDisplayName(SipStackAndroid.sipUserName);

Address toAddress = addressFactory.createAddress("sip:"

+ SipStackAndroid.sipUserName + "@" + SipStackAndroid.remoteIp);

toAddress.setDisplayName(SipStackAndroid.sipUserName);

Address contactAddress = createContactAddress();

ArrayList viaHeaders = createViaHeader();

URI requestURI = addressFactory.createAddress(

"sip:" + SipStackAndroid.remoteEndpoint).getURI();

// Build the request

final Request request = messageFactory.createRequest(requestURI,

Request.REGISTER, sipProvider.getNewCallId(),

headerFactory.createCSeqHeader(1l, Request.REGISTER),

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

// Send the request --- triggers an IOException

// sipProvider.sendRequest(request);

ClientTransaction transaction = sipProvider

.getNewClientTransaction(request);

// Send the request statefully, through the client transaction.

transaction.sendRequest();

} catch (Exception e) {

e.printStackTrace();

}

}

private Address createContactAddress() {

try {

SipStackAndroid.getInstance();

return SipStackAndroid.addressFactory.createAddress("sip:" + SipStackAndroid.sipUserName + "@"

+ SipStackAndroid.localEndpoint + ";transport=udp"

+ ";registering_acc=23_23_228_238");

} catch (ParseException e) {

return null;

}

}

private ArrayList createViaHeader() {

ArrayList viaHeaders = new ArrayList();

ViaHeader myViaHeader;

try {

SipStackAndroid.getInstance();

myViaHeader = SipStackAndroid.headerFactory.createViaHeader(SipStackAndroid.localIp, SipStackAndroid.localPort,

SipStackAndroid.transport, null);

myViaHeader.setRPort();

viaHeaders.add(myViaHeader);

} catch (ParseException e) {

e.printStackTrace();

} catch (InvalidArgumentException e) {

e.printStackTrace();

}

return viaHeaders;

}

Step -3:发送认证信息

为了响应REGISTER包,服务器请求认证信息(407错误)。 哪个会在实现SipListener的类的processResponse()方法中被接收。 下面的代码片段将身份验证发送回服务器。

像AccountManagerImpl这样的助手类可以在JAIN-SIP的例子中找到。

@Override

public void processResponse(ResponseEvent arg0) {

Response response = (Response) arg0.getResponse();

ClientTransaction tid = arg0.getClientTransaction();

System.out.println(response.getStatusCode());

if (response.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED

|| response.getStatusCode() == Response.UNAUTHORIZED) {

AuthenticationHelper authenticationHelper = ((SipStackExt) sipStack)

.getAuthenticationHelper(new AccountManagerImpl(),

headerFactory);

try {

ClientTransaction inviteTid = authenticationHelper

.handleChallenge(response, tid, sipProvider, 5);

inviteTid.sendRequest();

} catch (NullPointerException e) {

e.printStackTrace();

} catch (SipException e) {

e.printStackTrace();

}

}


发送SIP消息

看一下发送SIP消息的流程: