Android GB28181 SIP注册
demo下载地址:https://download.csdn.net/download/m0_37039192/12487977
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;
// 本地IP
public static String localIp = "192.168.10.88";
// 本地断开
public static int localPort = 5080;
// 本地路径
public static String localEndpoint = localIp + ":" + localPort;
// 类型
public static String transport = "udp";
// 服务器IP
public static String remoteIp = "192.168.10.147";
// 服务器断开
public static int remotePort = 5060;
// 服务区路径
public static String remoteEndpoint = remoteIp + ":" + remotePort;
// 账户
public static String sipUserName = "123";
// 密码
public String sipPassword = "12345678";
protected SipStackAndroid() {
initialize();
}
public static SipStackAndroid getInstance() {
if (instance == null) {
instance = new SipStackAndroid();
}
return instance;
}
private static void initialize() {
sipStack = null;
sipFactory = SipFactory.getInstance();
sipFactory.setPathName("android.gov.nist");
Properties properties = new Properties();
properties.setProperty("android.javax.sip.OUTBOUND_PROXY", remoteEndpoint + "/" + transport);
properties.setProperty("android.javax.sip.STACK_NAME", "TextClient");
try {
// 创建 SipStack 对象
sipStack = sipFactory.createSipStack(properties);
QLog.e("创建sipStack " +sipStack);
QLog.e("createSipStack " + sipStack);
} catch (PeerUnavailableException e) {
e.printStackTrace();
QLog.e("创建 SipStack 对象出错: " + 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());
QLog.e("初始化完成");
} catch (PeerUnavailableException e) {
e.printStackTrace();
QLog.e(e.getLocalizedMessage());
System.exit(0);
} catch (Exception e) {
QLog.e("创建侦听器点 出错" + e.getLocalizedMessage());
e.printStackTrace();
}
}
/**
* 开始注册
*/
public void send_register() {
try {
SipStackAndroid.getInstance();
AddressFactory addressFactory = SipStackAndroid.addressFactory;
SipStackAndroid.getInstance();
SipProvider sipProvider = SipStackAndroid.sipProvider;
SipStackAndroid.getInstance();
MessageFactory messageFactory = SipStackAndroid.messageFactory;
SipStackAndroid.getInstance();
HeaderFactory headerFactory = SipStackAndroid.headerFactory;
// 为请求创建地址和via头
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();
// 生成请求
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));
// 添加 contact 请求头
request.addHeader(headerFactory.createContactHeader(contactAddress));
ExpiresHeader eh = headerFactory.createExpiresHeader(300);
request.addHeader(eh);
QLog.e("请求的内容\n" + request.toString());
ClientTransaction transaction = sipProvider.getNewClientTransaction(request);
//通过客户端事务有状态地发送请求。
transaction.sendRequest();
} catch (Exception e) {
e.printStackTrace();
}
}
ClientTransaction inviteTid;
public void send_cancellation(){
// 为请求创建地址和via头
try {
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();
// 生成请求
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));
// 添加 contact 请求头
request.addHeader(headerFactory.createContactHeader(contactAddress));
ExpiresHeader eh = headerFactory.createExpiresHeader(0);
request.addHeader(eh);
QLog.e("注销内容\n" + request.toString());
ClientTransaction transaction = sipProvider.getNewClientTransaction(request);
//通过客户端事务有状态地发送请求。
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;
}
@Override
public void processResponse(ResponseEvent responseEvent) {
QLog.e("过程响应" + responseEvent.toString());
ResponseEventExt responseEventExt = (ResponseEventExt) responseEvent;
Response response = responseEvent.getResponse();
ClientTransaction tid = responseEvent.getClientTransaction();
QLog.e("过程响应" + response.toString());
// 获取返回信息的状态code
if (response.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED || response.getStatusCode() == Response.UNAUTHORIZED) {
AuthenticationHelper authenticationHelper = ((SipStackExt) sipStack).getAuthenticationHelper(new AccountManagerImpl(sipUserName, responseEventExt.getRemoteIpAddress(), sipPassword), headerFactory);
try {
inviteTid = authenticationHelper.handleChallenge(responseEventExt.getResponse(), tid, sipProvider, 5, true);
QLog.e("发送信息:" + inviteTid.getRequest().toString());
inviteTid.sendRequest();
} catch (NullPointerException e) {
QLog.e("*********" + e.getLocalizedMessage());
e.printStackTrace();
} catch (SipException e) {
QLog.e("*********" + e.getLocalizedMessage());
e.printStackTrace();
}
}
}
@Override
public void processRequest(RequestEvent requestEvent) {
QLog.e("处理请求" + requestEvent.getRequest().toString());
}
@Override
public void processTimeout(TimeoutEvent timeoutEvent) {
QLog.e("进程超时" + timeoutEvent.toString());
}
@Override
public void processIOException(IOExceptionEvent ioExceptionEvent) {
QLog.e("IO 超时" + ioExceptionEvent.toString());
}
@Override
public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
QLog.e("进程事务已终止" + transactionTerminatedEvent.toString());
}
@Override
public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
QLog.e("进程对话已终止" + dialogTerminatedEvent.toString());
}
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();
}
}
3.使用cmd进行查看端口号是否被监听来保证打开服务器是否正常
输入 netstat -ano|findstr 端口号 ----> 查看占用端口号的进程
4.参考文献:
1.https://blog.csdn.net/xiaoyu8388/article/details/81943794?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.nonecase
2.https://www.jianshu.com/p/383a242bed79
3.https://blog.csdn.net/lyyslsw1230_163com/article/details/8454851