#大標=電子行動白板-線上溝通彈指間 #副標=行動Java新應用—JSR180 SIP API #作者=文╱黃富鴻 =======程式1======================================== public void doRegister(String username, String password, String realm) { SipClientConnection scc = null; SipConnectionNotifier scn = null; String contact = null; try { //聽5060 port scn = (SipConnectionNotifier) Connector.open("sip:5060"); // 建立連線至SIP registrar server scc = (SipClientConnection) Connector.open("sip:host.com"); // initialize REGISTER with appropriate headers scc.initRequest("REGISTER", scn); scc.setHeader("From", "sip:user@host.com"); scc.setHeader("To", "sip:user@host.com"); scc.setHeader("Contact", "sip:user@host.com:5060"); scc.send(); boolean handled = false; int scode = 0; while(!handled) { SipHeader sh; // 等待回應timeout值3000ms scc.receive(30000); scode = scc.getStatusCode(); switch(scode) { case 401: sh = new SipHeader("Proxy-Authenticate", scc.getHeader("Proxy-Authenticate")); realm = sh.getParameter("realm"); //帶入使用者密碼資訊後,重新註冊 scc.setCredentials(username, password, realm); break; case 200: //處理OK回應 handled = true; break; default: //處理其它回應 handled = true; } } scc.close(); } catch(Exception ex) { //錯誤處理 } }=======程式1 end============= =========-程式2================================= public void doSubscribe(String name) { SipConnectionNotifier scn = null; SipClientConnection scc = null; try { //聽5060 port scn = (SipConnectionNotifier) Connector.open("sip:5060"); scn.setListener(this); scc = (SipClientConnection) Connector.open("sip:"+name+"host.com"); scc.setListener(this); scc.initRequest("SUBSCRIBE", null); scc.setHeader("From", "sip:host.com"); scc.setHeader("Contact", "sip:user@host.com"); scc.setHeader("Expires", "950"); scc.setHeader("Event", "presence"); scc.addHeader("Accept","application/pidf+xml"); //送出SUBSCRIBE訊息 scc.send(); } catch (Exception e) { e.printStackTrace(); } } public void notifyRequest(SipConnectionNotifier scn) { SipServerConnection ssc; String CSeq; try { ssc = scn.acceptAndOpen(); if (ssc.getMethod().equals("NOTIFY")) { //處理NOTIFY訊息 String cType=ssc.getHeader("Content-Type"); String cLen=ssc.getHeader("Content-Length"); int length = Integer.parseInt(cLen); if (cType.equals("application/pidf+xml")) { //處理上線狀態 InputStream is = ssc.openContentInputStream(); byte buf[] = new byte[length]; int i = is.read(buf); ssc.initResponse(200); ssc.send(); } } } catch (Exception e) { e.printStackTrace(); } } =======程式2 end=================================== ========程式3====================================== public void doInvite(String sdp, String invitee) { SipConnectionNotifier scn = null; SipClientConnection scc = null; try { scn = (SipConnectionNotifier) Connector.open("sip:5060"); scn.setListener(this); scc = (SipClientConnection) Connector.open("sip:"+invitee+"@host.com"); scc.setListener(this); scc.initRequest("INVITE", null); scc.setHeader("From", "sip:user@host.com"); scc.setHeader("To", "sip:"+invitee+"@host.com"); scc.setHeader("Contact", "sip:user@host.com:5060"); scc.setHeader("Content-Length", ""+sdp.length()); scc.setHeader("Content-Type", "application/sdp"); OutputStream os = scc.openContentOutputStream(); os.write(sdp.getBytes()); os.close(); } catch (Exception e) { e.printStackTrace(); } } public void notifyResponse(SipClientConnection scc) { int statusCode = 0; try { String CSeq = scc.getHeader("CSeq"); scc.receive(0); statusCode = scc.getStatusCode(); if (statusCode == 180) { //處理180回應 } else if (statusCode == 200) { //處理200成功回應 if (CSeq.indexOf("INVITE") != -1) { scc.initAck(); scc.send(); scc.close(); } } } } catch (Exception e) { e.printStackTrace(); } } =======程式3 end==========================================