问题
I've been coding a SIP client using the Android SDK's native SIP libs. For some reason I cannot get my account to register with the server.
Here are the testing grounds:
- Linux Mint 17 XFCE running a Kamailio Server(MySQL and TLS enabled).
- Linux Mint 17 Cinnamon running Android Studio (0.8.6).
- Asus Google Nexus 7 (2nd gen).
- All of the above are on 192.168.1.xxx
The server and account info has been tested with a working SipClient (ZoIPer). The port 5060 has been tested with a telnet command from the coding machine to the server. The client I'm building has also been tested with a third-party SIP service on a distant server (with identical results). Is there any idea how to make it works?
MainActivity
public class MainActivity extends Activity {
public CommsController cc;
public IncomingCallReceiver callReceiver;
public Context ctx = this;
Button callButton;
EditText phoneTextBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cc = new CommsController();
cc.createManager(this);
cc.createProfile("linasAndroid","192.168.1.140","android001");
callReceiver = new IncomingCallReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("util.Controller.IncomingCallReceiver");
this.registerReceiver(callReceiver, filter);
cc.openProfile(this);
try {
System.out.println("+++ IS API SUPPORTED: " + cc.getManager().isApiSupported(this));
System.out.println("+++ IS VOIP SUPORTED: " + cc.getManager().isVoipSupported(this));
System.out.println("+++ MANAGER INSTANCE: " + cc.getManager().toString());
boolean isOpened = cc.getManager().isOpened(cc.getMe().getUriString());
System.out.println("+++ IS OPENED: "+isOpened);
if(isOpened){
cc.getManager().register(cc.getMe(), 30000, cc.getSrl());
cc.createRegistrationListener();
}
}catch(SipException sipex){
System.out.println(sipex.getCause()+", "+ sipex.getMessage());
//sipex.printStackTrace();
}
callButton = (Button) findViewById(R.id.callButton);
phoneTextBox = (EditText) findViewById(R.id.phoneTextBox);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cc.makeCall("sip:"+phoneTextBox.getText()+"@192.168.1.140");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(callReceiver);
}
public Context getContext(){return ctx;}
public CommsController getCc() {
return cc;
}
CommsController
public class CommsController {
public String sipAddress = null;
public SipManager manager = null;
public SipProfile me = null;
public SipAudioCall call = null;
public Context ctx;
private PendingIntent pendingIntent;
public SipRegistrationListener srl;
private static final int CALL_ADDRESS = 1;
private static final int SET_AUTH_INFO = 2;
private static final int UPDATE_SETTINGS_DIALOG = 3;
private static final int HANG_UP = 4;
SipAudioCall.Listener listener = new SipAudioCall.Listener(){
@Override
public void onCallEstablished(SipAudioCall call) {
call.startAudio();
call.setSpeakerMode(true);
call.toggleMute();
}
@Override
public void onCallEnded(SipAudioCall call) {
// Do something.
}
};
public void createManager(Context context) {
if (manager == null) {
manager = SipManager.newInstance(context);
}
}
public boolean createProfile(String uname, String domain, String pass){
try {
SipProfile.Builder builder = new SipProfile.Builder(uname, domain);
builder.setPassword(pass);
builder.setProtocol("TCP");
builder.setPort(5060);
me = builder.build();
setMe(me);
System.out.println("+++ User Profile = CREATED");
return true;
} catch (ParseException e) {
e.printStackTrace();
System.out.println("+++ User Profile = FAILED");
return false;
}
}
public void openProfile(Context context){
try {
System.out.println("+++ ATTEMPTING TO OPEN PROFILE: " + me.getUriString());
Intent intent = new Intent();
intent.setAction("util.Controller.IncomingCallReceiver");
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, Intent.FILL_IN_DATA);
manager.open(getMe(), pendingIntent, null);
System.out.println("+++ IS OPEN IN CC: " +manager.isOpened(getMe().getUriString()));
} catch (SipException e) {
e.printStackTrace();
System.out.println("+++ Profile Registration = FAILED");
}
}
public void createRegistrationListener(){
System.out.println("+++ CREATING REG LISTENER...");
srl = new SipRegistrationListener() {
@Override
public void onRegistering(String localProfileUri) {
System.out.println("+++ REGISTERING...");
}
@Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
System.out.println("+++ READY");
}
@Override
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
System.out.println("+++ REGISTRATION FAILED. CHECK SETTINGS. ERROR: "+ errorCode + " MESSAGE: " +errorMessage);
}
};
}
public SipRegistrationListener getSrl(){
return srl;
}
public void closeProfile(SipProfile toClose){
if(manager==null){
return;
}
try {
if (manager != null) {
manager.close(toClose.getUriString());
System.out.println("PROFILE CLOSED: " +toClose.getUriString());
}
} catch (Exception ee) {
ee.printStackTrace();
System.out.println("FAILED TO CLOSE LOCAL PROFILE: "+ toClose.toString());
}
}
public void logOut(){
try {
System.out.println("+++ DEREGISTERING...");
manager.close(me.getUriString());
}catch(SipException se){
System.out.println("+++ DEREGISTRATION FAILED");
se.printStackTrace();
}
}
public void makeCall(String toCall){
try {
call = manager.makeAudioCall(me.getUriString(), toCall, listener, 30);
} catch (SipException e) {
e.printStackTrace();
}
}
//Getters and setters...
Output (LogCat)
09-09 16:46:53.709 22783-22783/com.inlusion.inlusip I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
09-09 16:46:53.739 22783-22783/com.inlusion.inlusip D/OpenGLRenderer﹕ Enabling debug mode 0
09-09 16:46:55.841 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ User Profile = CREATED
09-09 16:46:55.841 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ ATTEMPTING TO OPEN PROFILE: sip:linasAndroid@192.168.1.140;transport=tcp
09-09 16:46:55.921 22783-22783/com.inlusion.inlusip D/dalvikvm﹕ GC_FOR_ALLOC freed 316K, 4% free 9182K/9532K, paused 19ms, total 19ms
09-09 16:46:55.941 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ IS OPEN IN CC: false
09-09 16:46:55.941 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ IS API SUPPORTED: true
09-09 16:46:55.941 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ IS VOIP SUPORTED: true
09-09 16:46:55.941 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ MANAGER INSTANCE: android.net.sip.SipManager@4256de78
09-09 16:46:55.941 22783-22783/com.inlusion.inlusip I/System.out﹕ +++ IS OPENED: false
EDIT:
After messing around with pjsip, mjsip and jain-sip, I desparately made one last attempt to rewrite this using the SDKs sip stack, in a more structured manner, separating the methods into classes that handle talking to the registrar, handling a call etc., you know- standard MVC layout.
After two weeks of repackaging third-party libraries and trying to figure out the NDK... This bastard worked. Goes to show - structure your code right!
EDIT 2:
The issue is back after removing the app from the device and running it again. Once more- no idea what's causing it. Had gotten to the point where I could register, unregister and make two way audio calls until it started happening again.
EDIT 3:
It seems the problem goes away when the device is rebooted. Starting to think it's either an unclosed socket or a previously opened profile. Any suggestions?
EDIT 4:
Bug also occurs when another VoIP SIP application (with the pjsip OR mjsip stack) is present and connected to the server with the same account credentials.
来源:https://stackoverflow.com/questions/25746493/android-sip-client-sipmanager-open-is-not-opening