问题
I am working on simple SIP-client Android app.
But when I try to register sipProfile
on Server, I get errorCode = -9
and errorMessage= 0
.
Here is my activity:
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());
}
And my Permissions:
<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" />
If I try with following code:
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);
}
});
I get SipException:
android.net.sip.SipException: SipService.createSession() returns null
FIXED
It looks like one of the sip sessions has been frozen on device. After physical reboot I got my registration
回答1:
In case this error you need close local profile and repeat create session
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();
}
}
}
});
回答2:
After lot of searching and going through multiple stackoverflow answers and wasting 2 days on registration errors, here are the things that you should consider to get the registration done ! These worked for me. Reply to this answer if they work for you too :)
Check for the following your code:
- First of all, check whether all required permissions are given or not
- Use
open
method instead of register &close
instead of unregister - Set your
SipRegisterationListener
after calling open method, passnull
as a parameter inopen
method for listener and callsipManager.setRegisterationListener(listener)
to set the listener. - Your domain must not include port number it should be simple server domain link ; For example
com.google.com
- First try running the app without setting the port number in
SipProfile.Builder
; if that fails, add port manually usingbuilder.setPort(xxxx)
- Try changing the protocol. Currently only
UDP
&TCP
are the options available as mentioned in this documentation - Check that your profile uri built is of the format
sip:<username>@<domain>
. For example if the username is abcd and domain is com.google.com, theSipProfile
uri string should besip:abcd@com.google.com
Try the following things if your android code is correct :
- Check the username and password properly / try some other username and password. Before trying with other credentials, close the existing SipProfile until is is successfully closed.
- Clear data of
Phone Service
system apps from your phone - Clear data of your developed SIP app also
- Close any open / existing SIP accounts on the device from
Phone > Settings > Calling Accouts > Sip Accounts
; - Reboot your phone
来源:https://stackoverflow.com/questions/38347771/android-sip-registration-failed-with-error-in-progress