我正在研究简单的SIP客户端Android应用程序。
但是当我尝试在服务器上注册sipProfile
时,我会errorCode = -9
和errorMessage= 0
。
这是我的活动:
public SipManager sipManager;
private SipProfile sipProfile;
// here is the data, I've just erased it
private String USERNAME = "";
private String AUTHUSERNAME = "";
private String DOMAIN = "";
private String PASSWORD = "";
private int PORT = 5060;
public SipAudioCall call = null;
public String sipAddress = null;
private Button btnRegister, btnCloseProfile;
private TextView tvStatus;
public IncomingCallReceiver callReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRegister = (Button) findViewById(R.id.btnRegister);
tvStatus = (TextView) findViewById(R.id.tvStatus);
btnCloseProfile = (Button) findViewById(R.id.btnCloseProfile);
btnRegister.setOnClickListener(register);
btnCloseProfile.setOnClickListener(closeProfile);
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
callReceiver = new IncomingCallReceiver();
this.registerReceiver(callReceiver, filter);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
View.OnClickListener closeProfile = new View.OnClickListener() {
@Override
public void onClick(View v) {
closeLocalProfile();
}
};
View.OnClickListener register = new View.OnClickListener() {
@Override
public void onClick(View v) {
initializeManager();
}
};
void initializeManager(){
if(sipManager == null) {
sipManager = SipManager.newInstance(this);
}
initializeLocalProfile();
}
@Override
protected void onStart() {
super.onStart();
initializeManager();
}
public void initializeLocalProfile(){
if (sipManager == null){
return;
}
if (sipProfile != null){
closeLocalProfile();
}
try {
SipProfile.Builder builder = new SipProfile.Builder(USERNAME, DOMAIN);
builder.setPassword(PASSWORD);
builder.setAuthUserName(AUTHUSERNAME);
sipProfile = builder.build();
Intent intent = new Intent();
intent.setAction("ru.tenet.apdu.INCOMING_CALL");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
sipManager.open(sipProfile, pi, null);
sipManager.setRegistrationListener(sipProfile.getUriString(), new SipRegistrationListener() {
@Override
public void onRegistering(String localProfileUri) {
updateStatus("Registering with SIP Server...");
}
@Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
updateStatus("Ready");
}
@Override
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
updateStatus("Registration failed with error:\n" + SipErrorCode.toString(errorCode) +"\n"+errorMessage);
}
});
}
catch (SipException e){
e.printStackTrace();
updateStatus("SipException");
} catch (ParseException e) {
e.printStackTrace();
updateStatus("ParseException");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (call != null) {
call.close();
}
closeLocalProfile();
if (callReceiver != null) {
this.unregisterReceiver(callReceiver);
}
}
public void closeLocalProfile() {
if (sipManager == null) {
return;
}
try {
if (sipProfile != null) {
sipManager.close(sipProfile.getUriString());
}
} catch (Exception ee) {
Log.d("StatusWindow/onDestroy", "Failed to close local profile.", ee);
}
}
void updateStatus(final String status){
Log.d("mylog","status = " +status);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvStatus.setText(status);
}
});
}
public void updateStatus(SipAudioCall call) {
String useName = call.getPeerProfile().getDisplayName();
if(useName == null) {
useName = call.getPeerProfile().getUserName();
}
updateStatus(useName + "@" + call.getPeerProfile().getSipDomain());
}
我的权限:
<uses-permission android:name="android.permission.USE_SIP"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
如果我尝试使用以下代码:
sipManager.open(sipProfile, pi, null);
sipManager.register(sipProfile, 20, new SipRegistrationListener() {
@Override
public void onRegistering(String localProfileUri) {
updateStatus("Registering with SIP Server...");
}
@Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
updateStatus("Ready");
}
@Override
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
updateStatus("Registration failed with error:\n" + SipErrorCode.toString(errorCode) +"\n"+errorMessage);
}
});
我得到SipException:
android.net.sip.SipException: SipService.createSession() returns null
固定
看起来其中一个sip会话已被冻结在设备上。 物理重启后我得到了我的注册
答案 0 :(得分:0)
如果出现此错误,您需要关闭本地个人资料并重复创建会话
manager.setRegistrationListener(me.getUriString(), new SipRegistrationListener() {
public void onRegistering(String localProfileUri) {
updateStatus("Registering with SIP Server...");
}
public void onRegistrationDone(String localProfileUri, long expiryTime) {
updateStatus("Ready");
}
public void onRegistrationFailed(String localProfileUri, int errorCode,
String errorMessage) {
if(errorCode == SipErrorCode.IN_PROGRESS) {
closeLocalProfile();
try {
manager.open(me, pi, null);
} catch (SipException e) {
e.printStackTrace();
}
}
}
});
答案 1 :(得分:0)
经过大量搜索并经历了多个stackoverflow答案,并浪费了2天的注册错误,这是您应该考虑完成注册的事情!这些为我工作。如果他们也为您工作,请回复此答案:)
检查以下代码:
open
方法代替注册&close
代替注销SipRegisterationListener
,将null
作为open
方法中的参数传递给侦听器,并调用sipManager.setRegisterationListener(listener)
来设置侦听器。com.google.com
SipProfile.Builder
中设置端口号;如果失败,请使用builder.setPort(xxxx)
UDP
和TCP
是上述in this documentation sip:<username>@<domain>
。例如,如果用户名是 abcd 而域是 com.google.com ,则SipProfile
uri字符串应为sip:abcd@com.google.com
如果您的Android代码正确,请尝试以下操作:
Phone Service
个系统应用的数据Phone > Settings > Calling Accouts > Sip Accounts
关闭设备上所有打开的/现有的SIP帐户;