Android-SIP库:android-jain-sip-ri(2)

上一篇:Android-Sip_开发介绍(1)

讲了这么多废话,上一点干货吧,这篇我们主要讲的是Android Sip中的第三方框架 android-jain-sip-ri-1.2.224.jar,这篇将介绍android-jain-sip-ri-1.2.224.jar的使用

为什么使用这个库?

1.本地SIP堆栈的限制

Google原生的android sip堆栈是建立在JAIN SIP之上的,但是它没有所有的功能,现在不支持即时消息,状态和视频通话。Google分叉了SIP堆栈,并且从未真正地更新默认发货的SIP API,并使用与原始JAIN SIP项目相同的软件包名称。这对于那些希望在全世界增加SIP功能的开发者来说是一个麻烦,因为他们不能使用JAIN SIP。

2.JAIN SIP是基于Java的

这是一个基于java的SIP协议栈,它允许你在没有NDK的麻烦的情况下将SIP协议栈无缝集成到你的android应用程序中。

3.完整的SIP协议栈

JAIN SIP是RFC 3261规范的完整实现,同时也支持多个SIP RFC。


怎么使用?

1.导包:将android-jain-sip-ri-1.2.224.jar和log4导入到项目中,以Android Studio为例:



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消息的流程:


private void sendMessage(String to, String message) throws ParseException,

InvalidArgumentException, SipException {

SipStackAndroid.getInstance();

SipURI from = SipStackAndroid.addressFactory.createSipURI(SipStackAndroid.getInstance().sipUserName, SipStackAndroid.getInstance().localEndpoint);

SipStackAndroid.getInstance();

Address fromNameAddress = SipStackAndroid.addressFactory.createAddress(from);

SipStackAndroid.getInstance();

// fromNameAddress.setDisplayName(sipUsername);

FromHeader fromHeader = SipStackAndroid.headerFactory.createFromHeader(fromNameAddress,

"Tzt0ZEP92");

SipStackAndroid.getInstance();

URI toAddress = SipStackAndroid.addressFactory.createURI(to);

SipStackAndroid.getInstance();

Address toNameAddress = SipStackAndroid.addressFactory.createAddress(toAddress);

SipStackAndroid.getInstance();

// toNameAddress.setDisplayName(username);

ToHeader toHeader = SipStackAndroid.headerFactory.createToHeader(toNameAddress, null);

SipStackAndroid.getInstance();

URI requestURI = SipStackAndroid.addressFactory.createURI(to);

// requestURI.setTransportParam("udp");

ArrayList viaHeaders = createViaHeader();

SipStackAndroid.getInstance();

CallIdHeader callIdHeader = SipStackAndroid.sipProvider.getNewCallId();

SipStackAndroid.getInstance();

CSeqHeader cSeqHeader = SipStackAndroid.headerFactory.createCSeqHeader(50l,

Request.MESSAGE);

SipStackAndroid.getInstance();

MaxForwardsHeader maxForwards = SipStackAndroid.headerFactory

.createMaxForwardsHeader(70);

SipStackAndroid.getInstance();

Request request = SipStackAndroid.messageFactory.createRequest(requestURI,

Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader,

toHeader, viaHeaders, maxForwards);

SipStackAndroid.getInstance();

SupportedHeader supportedHeader = SipStackAndroid.headerFactory

.createSupportedHeader("replaces, outbound");

request.addHeader(supportedHeader);

SipStackAndroid.getInstance();

SipURI routeUri = SipStackAndroid.addressFactory.createSipURI(null, SipStackAndroid.getInstance().remoteIp);

routeUri.setTransportParam(SipStackAndroid.transport);

routeUri.setLrParam();

routeUri.setPort(SipStackAndroid.remotePort);

SipStackAndroid.getInstance();

Address routeAddress = SipStackAndroid.addressFactory.createAddress(routeUri);

SipStackAndroid.getInstance();

RouteHeader route =SipStackAndroid.headerFactory.createRouteHeader(routeAddress);

request.addHeader(route);

SipStackAndroid.getInstance();

ContentTypeHeader contentTypeHeader = SipStackAndroid.headerFactory

.createContentTypeHeader("text", "plain");

request.setContent(message, contentTypeHeader);

System.out.println(request);

SipStackAndroid.getInstance();

ClientTransaction transaction = SipStackAndroid.sipProvider

.getNewClientTransaction(request);

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

transaction.sendRequest();

}

这将发送一个消息到服务器,服务器将要求我们已经覆盖的授权。

资源:完成项目代码Apk

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容