You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中使用pjsua2创建SIP账户失败,无法发起呼叫求助

针对pjsua2账户创建失败(错误码171039)及"Account is null in sip"问题的解决方案

1. 错误码171039的核心排查

错误码171039对应pjsip的PJSIP_EINVALIDURI(无效SIP URI),大概率是SIP配置参数格式错误:

  • 检查idUri格式:必须严格遵循sip:用户名@域名/IP:端口标准,比如sip:1001@192.168.1.100:5060,不能省略sip:前缀,域名/IP和端口需与服务器配置匹配。
  • 验证registrarUri:注册服务器地址格式同样为sip:域名/IP:端口,不能为空或出现拼写错误。
  • 代理地址检查:若使用代理服务器,cfg.proxy数组中的每一项都必须是合法的SIP URI。

2. MyAccount类实现的关键要点

确保MyAccount正确继承并实现Account类的核心逻辑:

  • 必须重写onRegState方法,用于追踪注册状态,确认流程是否正常触发:
    @Override
    public void onRegState(OnRegStateParam prm) {
        AccountInfo ai = getInfo();
        Log.d("SIP", "注册状态: " + ai.getRegStatus() + " | " + ai.getRegReason());
    }
    
  • 实例化MyAccount后,必须关联到已初始化的Endpoint实例,不能孤立创建。

3. addAcc方法的正确实现

检查addAcc方法是否存在以下致命问题:

  • 确保Endpoint已完成完整初始化:必须按顺序执行ep.libCreate()、ep.libInit()、ep.transportCreate()、ep.libStart(),再执行账户添加操作。
  • 正确填充AccountConfig参数,尤其是身份验证信息:
    public void addAcc() throws Exception {
        // 假设已初始化全局Endpoint实例 ep
        AccountConfig accCfg = new AccountConfig();
        accCfg.getIdUri().setUri("sip:1001@192.168.1.100:5060");
        accCfg.getRegistrarUri().setUri("sip:192.168.1.100:5060");
        
        // 添加身份验证凭据
        AuthCredInfo cred = new AuthCredInfo("digest", "*", "1001", 0, "123456");
        accCfg.getAuthCreds().add(cred);
        
        // 代理服务器配置(按需添加)
        accCfg.getProxy().add("sip:192.168.1.100:5060");
        
        MyAccount acc = new MyAccount();
        acc.create(accCfg);
        // 关键:必须在类级别持有acc的强引用,避免被GC回收导致后续Account null
        this.sipAccount = acc;
    }
    
  • 强引用保存账户实例:如果仅在addAcc方法内创建临时MyAccount对象,方法执行完后对象会被垃圾回收,后续调用makeCall时必然出现"Account is null in sip"错误。

4. Android平台权限与配置检查

  • 声明必要权限到AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
  • Android 6.0+需动态申请RECORD_AUDIO等危险权限,否则会间接导致账户创建失败。

5. 日志排查技巧

  • 开启pjsip详细日志,获取错误上下文:
    EpConfig epCfg = new EpConfig();
    epCfg.getLogConfig().setLevel(5); // 级别越高日志越详细
    epCfg.getLogConfig().setConsoleLevel(5);
    ep.libInit(epCfg);
    
  • 查看错误码171039之前的日志输出,通常会明确指出哪个参数(如URI)格式无效。

内容的提问来源于stack exchange,提问作者mohamed hossam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 08:25:06