繁体   English   中英

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

[英]Android sip registration failed with error “IN_PROGRESS”

我正在开发简单的 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 会话已在设备上冻结。 物理重启后我得到了我的注册

如果出现此错误,您需要关闭本地配置文件并重复创建会话

   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();
                        }
                    }

                }
            });

经过大量搜索和多个 stackoverflow 答案并在注册错误上浪费了 2 天之后,以下是完成注册时您应该考虑的事项! 这些对我有用。 如果他们也为您工作,请回复此答案:)

检查以下代码:

  1. 首先,检查是否提供了所有必需的权限
  2. 使用open方法代替 register & close代替 unregister
  3. 在调用 open 方法后设置您的SipRegisterationListener ,在监听器的open方法中将null作为参数传递并调用sipManager.setRegisterationListener(listener)来设置监听器。
  4. 您的域不能包含端口号,它应该是简单的服务器域链接; 例如com.google.com
  5. 首先尝试在SipProfile.Builder不设置端口号的情况下运行应用程序; 如果失败,请使用builder.setPort(xxxx)手动添加端口
  6. 尝试更改协议。 当前只有UDPTCP本文档中提到的可用选项
  7. 检查您构建的配置文件 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 Phone > Settings > Calling Accouts > Sip Accounts
  5. 重启手机

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM