Java 注册时发生Sip错误
我正在使用本机的sip调用方法,它工作正常,但有一段时间它在进行注册时出现了错误(错误代码-9)。我搜索了sip错误代码,但没有找到任何此类错误代码。这是我的sip注册代码。如果有人知道这个错误,请告诉我Java 注册时发生Sip错误,java,android,sip,Java,Android,Sip,我正在使用本机的sip调用方法,它工作正常,但有一段时间它在进行注册时出现了错误(错误代码-9)。我搜索了sip错误代码,但没有找到任何此类错误代码。这是我的sip注册代码。如果有人知道这个错误,请告诉我 import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import andr
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.*;
import android.net.sip.*;
import java.text.ParseException;
/**
* Handles all calling, receiving calls, and UI interaction in the WalkieTalkie app.
*/
public class WalkieTalkieActivity extends Activity implements View.OnTouchListener {
public String sipAddress = null;
public static SipManager manager = null;
public static SipProfile me = null;
public SipAudioCall call = null;
public SipErrorCode sipcode;
public SipException sipexeception;
public static WalkieTalkieActivity walkiy;
public static WalkieTalkieActivity getInstance() {
if (walkiy == null) {
walkiy = new WalkieTalkieActivity();
}
return walkiy;
}
@
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Log.e("onCreate", "onCreate");
initializeManager(this);
}
@
Override
public void onStart() {
super.onStart();
// When we get back from the preference setting Activity, assume
// settings have changed, and re-login with new auth info.
}
@
Override
public void onDestroy() {
super.onDestroy();
if (call != null) {
call.close();
}
closeLocalProfile();
}
@
Override
protected void onResume() {
initializeManager(this);
super.onResume();
}
public void initializeManager(Context ctx) {
Log.e("inilitize", "initialize manager");
if (manager == null) {
manager = SipManager.newInstance(ctx);
}
initializeLocalProfile(ctx);
}
/**
* Logs you into your SIP provider, registering this device as the location to
* send SIP calls to for your SIP address.
*/
public void initializeLocalProfile(final Context ctx) {
Log.e("inilitize", "initialize profile");
if (manager == null) {
Log.e("inilitize", "return in profile");
updateStatus("we are unable to establish this feature on your device ", ctx);
Log.e("Wifi state", "" + SipManager.isSipWifiOnly(ctx));
return;
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String username = prefs.getString("namePref", "XYZ");
String domain = prefs.getString("domainPref", "236.343.230.2");
String password = prefs.getString("passPref", "XYZ");
try {
Log.e("inilitize", "building profile ");
SipProfile.Builder builder = new SipProfile.Builder(username, domain);
builder.setPassword(password);
me = builder.build();
manager.open(me);
Log.e("alredy registered1", "" + manager.isRegistered(me.getUriString()));
try {
if (manager.isRegistered(me.getUriString())) {
Log.e("alredy registered2", "" + manager.isRegistered(me.getUriString()));
updateStatus("Ready", ctx);
return;
}
} catch (SipException e) {
// TODO Auto-generated catch block
closeLocalProfile();
e.printStackTrace();
}
manager.setRegistrationListener(me.getUriString(), new SipRegistrationListener() {
public void onRegistering(String localProfileUri) {
Log.e("Sip restration", "Registering with SIP Server..." + me.getUriString());
updateStatus("Registering with SIP Server...", ctx);
}
public void onRegistrationDone(String localProfileUri, long expiryTime) {
Log.e("Sip restration", "Ready");
updateStatus("Ready", ctx);
}
public void onRegistrationFailed(String localProfileUri, int errorCode,
String errorMessage) {
Log.e("Error Code Registration ", SipErrorCode.toString(errorCode) + " " + errorCode);
updateStatus(SipErrorCode.toString(errorCode), ctx);
}
});
} catch (ParseException pe) {
updateStatus("Connection Error.", ctx);
} catch (SipException se) {
updateStatus("Connection error.", ctx);
}
}
/**
* Closes out your local profile, freeing associated objects into memory
* and unregistering your device from the server.
*/
public void closeLocalProfile() {
if (manager == null) {
return;
}
try {
if (me != null) {
manager.unregister(me, new SipRegistrationListener() {
@
Override
public void onRegistrationFailed(String localProfileUri, int errorCode,
String errorMessage) {
// TODO Auto-generated method stub
Log.e("unregister failed", "unregister failed");
}
@
Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
// TODO Auto-generated method stub
Log.e("unregister done", "unregister done");
}
@
Override
public void onRegistering(String localProfileUri) {
// TODO Auto-generated method stub
Log.e("unregistering", "unregistering");
}
});
manager.close(me.getUriString());
}
} catch (Exception ee) {
Log.d("WalkieTalkieActivity/onDestroy", "Failed to close local profile.", ee);
}
}
/**
* Make an outgoing call.
*/
public void initiateCall(String number, final Context ctx, final View v) {
Log.d("Number", "" + number);
sipAddress = number;
Log.e("initiating call", "initiating call");
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {@
Override
public void onCallBusy(SipAudioCall call) {
// TODO Auto-generated method stub
Log.e("buzy", "buzy");
super.onCallBusy(call);
}
@
Override
public void onCallHeld(SipAudioCall call) {
// TODO Auto-generated method stub
Log.e("held", "held");
super.onCallHeld(call);
}
@
Override
public void onCalling(SipAudioCall call) {
// TODO Auto-generated method stub
Log.e("calling", "calling");
super.onCalling(call);
}
@
Override
public void onChanged(SipAudioCall call) {
// TODO Auto-generated method stub
Log.e("changed", "changed");
super.onChanged(call);
}
@
Override
public void onError(SipAudioCall call, int errorCode,
String errorMessage) {
// TODO Auto-generated method stub
Log.e("call error", "error" + SipErrorCode.toString(errorCode) + " " + errorCode);
CallingScreen.fa.finish();
super.onError(call, errorCode, errorMessage);
}
@
Override
public void onReadyToCall(SipAudioCall call) {
// TODO Auto-generated method stub
Log.e("ready to call", "ready to call ");
super.onReadyToCall(call);
}
@
Override
public void onRinging(SipAudioCall call, SipProfile caller) {
// TODO Auto-generated method stub
Log.e("ringing", "ringing");
super.onRinging(call, caller);
}
@
Override
public void onRingingBack(SipAudioCall call) {
// TODO Auto-generated method stub
Log.e("ringing back", "ringing back");
super.onRingingBack(call);
}
// Much of the client's interaction with the SIP Stack will
// happen via listeners. Even making an outgoing call, don't
// forget to set up a listener to set things up once the call is established.
@
Override
public void onCallEstablished(SipAudioCall call) {
Log.e("call established", "call established");
call.startAudio();
updateTime(true, ctx);
}
@
Override
public void onCallEnded(SipAudioCall call) {
Log.e("call ended", "call ended");
updateTime(false, ctx);
CallingScreen.fa.finish();
}
};
Log.e("param 1 ", "" + me.getUriString());
call = manager.makeAudioCall(me.getUriString(), sipAddress + "@216.245.200.2:5060", listener, 30);
} catch (Exception e) {
e.printStackTrace();
Log.i("WalkieTalkieActivity/InitiateCall", "Error when trying to close manager.", e);
if (me != null) {
try {
closeLocalProfile();
} catch (Exception ee) {
ee.printStackTrace();
Log.i("WalkieTalkieActivity/InitiateCall",
"Error when trying to close manager.", ee);
ee.printStackTrace();
}
}
if (call != null) {
call.close();
}
}
}
public void updateStatus(final String status, final Context context) {
// Be a good citizen. Make sure UI changes fire on the UI thread.
this.runOnUiThread(new Runnable() {
public void run() {
generateNotification(context, status);
}
});
}
public void updateTime(final Boolean status, final Context context) {
// Be a good citizen. Make sure UI changes fire on the UI thread.
this.runOnUiThread(new Runnable() {
public void run() {
if (status) {
CallingScreen.fa.calling_screen_text.setVisibility(View.GONE);
CallingScreen.fa.ch.setVisibility(View.VISIBLE);
CallingScreen.fa.ch.start();
} else {
CallingScreen.fa.ch.stop();
}
}
});
}
public void updateStatus(SipAudioCall call) {
String useName = call.getPeerProfile().getDisplayName();
if (useName == null) {
useName = call.getPeerProfile().getUserName();
}
// updateStatus(useName + "@" + call.getPeerProfile().getSipDomain());
}
public boolean onTouch(View v, MotionEvent event) {
if (call == null) {
return false;
} else if (event.getAction() == MotionEvent.ACTION_DOWN && call != null && call.isMuted()) {
call.toggleMute();
} else if (event.getAction() == MotionEvent.ACTION_UP && !call.isMuted()) {
call.toggleMute();
}
return false;
}
public void speaker(Boolean state) {
if (call != null)
call.setSpeakerMode(state);
}
public void endcall() {
if (call != null) {
try {
call.endCall();
} catch (SipException se) {
Log.d("WalkieTalkieActivity/onOptionsItemSelected",
"Error ending call.", se);
}
call.close();
}
}
public static void generateNotification(Context context, String message) {
Log.e("inilitize", "generate notification");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, Splash_screen.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notificationManager.notify(0, notification);
}
}
SIP是基于事务的协议,这意味着每个协商过程都是一个事务(例如,基本注册过程意味着发送REISTER请求并接收200 OK响应)。我贴了一个小例子作为回应 IN_PROGRESS error最初表示您试图启动一个新事务,在您的场景中,当客户端已经在另一个相关事务中时,您试图发送一个注册请求 一个非常典型的场景是,在快速注销/登录中,尝试在注销进程仍在运行时注册(未收到200 OK最终响应) 关于Android的SIP实现和此错误代码的主要问题是,当处理SIP请求失败时,它作为一般错误使用,因此,前面的含义失去了意义 由于前面的问题,您需要检查logcat以获取更多信息(您应该看到类似“~~~~~~~SipSessionGroup::processing”的内容),但处理此问题的最佳方法是使用堆栈提供的不同侦听器 堆栈提供了两个可以获取注册错误的侦听器:和。两者都提供回调,但后面的一个还提供,这可能非常有用。实际上,您已经在代码中使用了SipRegistrationListener,但我不明白您为什么说它应该设置在
open
之后,因为open
将尝试自动注册,如果发生错误,您将不会得到通知
另一方面,此侦听器的主要问题是,注册可能会由于多种原因而失败(您可以检查不同错误响应的列表),并且当前的实现只通知错误代码>=500(这些是服务器和全局故障),而不通知其他类似于常见406的错误代码。在我看来,这是这个堆栈中的一个重要错误 非常感谢您的回答,但您能解释一下我现在应该做些什么来避免这种情况吗?我很欣赏关于这个主题的知识,但我是第一次使用它,这也是sdk的示例代码。因此,请您简单地解释一下:(另外,我想知道我可以得到这个错误列表。如果你能与我分享,这将对我很有帮助。)that@Bansal_Sneha当然,没问题,如果我太直接了,对不起。我会更新答案。我收到的错误代码是-9,-4等等。我可以得到这个错误的列表吗?@Bansal_Sneha你有一个来自Android SIP堆栈的不同错误的列表。