Android sip注册失败,错误“IN_PROGRESS”

时间:2016-07-13 09:31:37

标签: android sip

我正在研究简单的SIP客户端Android应用程序。

但是当我尝试在服务器上注册sipProfile时,我会errorCode = -9errorMessage= 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会话已被冻结在设备上。 物理重启后我得到了我的注册

2 个答案:

答案 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天的注册错误,这是您应该考虑完成注册的事情!这些为我工作。如果他们也为您工作,请回复此答案:)

检查以下代码:

  1. 首先,检查是否已提供所有必需的权限
  2. 使用open方法代替注册&close代替注销
  3. 调用open方法后,设置您的SipRegisterationListener,将null作为open方法中的参数传递给侦听器,并调用sipManager.setRegisterationListener(listener)来设置侦听器。
  4. 您的域不得包含端口号,它应该是简单的服务器域链接;例如com.google.com
  5. 首先尝试运行应用程序,而不在SipProfile.Builder中设置端口号;如果失败,请使用builder.setPort(xxxx)
  6. 手动添加端口
  7. 尝试更改协议。目前只有UDPTCP是上述in this documentation
  8. 可用的选项。
  9. 检查您建立的个人资料uri的格式为sip:<username>@<domain>。例如,如果用户名是 abcd 而域是 com.google.com ,则SipProfile uri字符串应为sip:abcd@com.google.com

如果您的Android代码正确,请尝试以下操作:

  1. 正确检查用户名和密码/尝试其他一些用户名和密码。尝试使用其他凭据之前,请关闭现有的SipProfile,直到成功将其关闭。
  2. 从手机中清除Phone Service个系统应用的数据
  3. 还清除您开发的SIP应用程序的数据
  4. Phone > Settings > Calling Accouts > Sip Accounts关闭设备上所有打开的/现有的SIP帐户;
  5. 重新启动手机