Android技术知识Android开发经验谈Android开发

Android - SIP(一) 自带SIP模块

2018-01-27  本文已影响3522人  Allens_Jiang
老婆保佑,代码无BUG

前言

好久没写文章的感觉,最近一直在研究Sip协议,公司让做一个SIP通话的App,没听过,google一下,发现android 是有sip 接口的,那就SO easy了,然而正在开发时候不是这样,很多手机会吧sip阉割了,蛋蛋忧伤,这里先说一下,自带的sip 怎么玩,下一篇文章会说一下,PJSIP


目录


一:SIP 接口

一: 权限

<uses-sdk android:minSdkVersion="9" />  
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />

权限说明

<uses-sdk android:minSdkVersion="9" />

如果你开发一个用到SIP API的应用程序,记住它需要Android2.3(API9)或者更高版本的平台的支持。所以在你的设备上要运行Android2.3(API9)或者更高的版本,并不是所有的设备都提供SIP的支持。

二:注册

1.初始化SipManager

SipManager manager = SipManager.newInstance(this);
//很多手机这里依然会返回null,原因就是不支持SIP
//之后会说PJSIP,比较通用
if (manager == null) {
    return;
}

2. SipProfile

//uesrname表示注册用户名,domain表示域,实际就是sip服务器ip
SipProfile.Builder builder = new SipProfile.Builder(username, domain); 
builder.setPassword(password); //注册用户密码
SipProfile me = builder.build();


//添加一个本地的过滤器,用于接受电话
//构造一个PendingIntent对象,这样当sip Service收到一个通话请求时,
//SipService会调用PendingIntent的send方法发送相应广播消息给调用者,也就是当前的SipProfile对象.
Intent i = new Intent();
i.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA);
//此处就是用于注册一个账户到sip服务器
manager.open(me, pi, null);

3. 注册状态监听

//注册一个监听器,用于获取注册账户时的通知状态,当然也可以不注册.
manager.setRegistrationListener(me.getUriString(), new SipRegistrationListener() {
        public void onRegistering(String localProfileUri) { //正在注册
        }

        public void onRegistrationDone(String localProfileUri, long expiryTime) {//注册成功
        }

        public void onRegistrationFailed(String localProfileUri, int errorCode,
                String errorMessage) {//注册失败
        }
    });

三:打电话

 public void initiateCall(String Adress) {
        try {
            SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                //准备打电话
                @Override
                public void onReadyToCall(SipAudioCall call) {
                    Logger.i("onReadyToCall---->");
                }

                @Override
                public void onRinging(SipAudioCall call, SipProfile caller) {
                    Logger.i("onRinging---->");
                }

                @Override
                public void onCallEstablished(SipAudioCall call) {
                    Logger.i("onCallEstablished---->");
                    call.startAudio();
                    call.setSpeakerMode(true);
                    call.toggleMute();
                }

                @Override
                public void onCallEnded(SipAudioCall call) {
                    Logger.i("onCallEnded----start");
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ToastView.newInstance(mContext).setToastMessage("通话结束");
                        }
                    });
                }
            };

            call = MyApplication.mSipManager.makeAudioCall(mSipProfile.getUriString(), Adress + "@" + ip, listener, 30);

            call.toggleMute();

        } catch (Exception e) {
            if (mSipProfile != null) {
                try {
                    mSipManager.close(mSipProfile.getUriString());
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
            if (call != null) {
                call.close();
            }
        }
    }

adress 的格式

xxx@ip 例如 202@192.198.1.101

四:接受电话

public class IncomingCallReceiver extends BroadcastReceiver {  
    /** 
     * Processes the incoming call, answers it, and hands it over to the 
     * WalkieTalkieActivity. 
     * @param context The context under which the receiver is running. 
     * @param intent The intent being received. 
     */   
    @Override  
    public void onReceive(Context context, Intent intent) {  
        SipAudioCall incomingCall = null;  
        try {  
            SipAudioCall.Listener listener = new SipAudioCall.Listener() {  
                @Override  
                public void onRinging(SipAudioCall call, SipProfile caller) {  
                    try {  
                        call.answerCall(30);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                }  
            };  
            WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;  
            incomingCall = wtActivity.mSipManager.takeAudioCall(intent, listener);  
            incomingCall.answerCall(30);  
            incomingCall.startAudio();  
            incomingCall.setSpeakerMode(true);  
            if(incomingCall.isMuted()) {  
                incomingCall.toggleMute();  
            }  
         //   wtActivity.call = incomingCall;  
       //     wtActivity.updateStatus(incomingCall);  
        } catch (Exception e) {  
            if (incomingCall != null) {  
                incomingCall.close();  
            }  
        }  
    }  
} 

五:接受到对方挂电话的状态

  private Handler handler = new Handler();
    private void initGetSipSession() {
        try {
            SipSession sessionFor = MyApplication.mSipManager.getSessionFor(intent);

            SipSession.Listener listener = new SipSession.Listener(){
                @Override
                public void onCalling(SipSession session) {
                    Logger.i("onCalling");
                }
                @Override
                public void onCallEnded(SipSession session) {
                    Logger.i("onCallEnded");
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ToastView.newInstance(mContext).setToastMessage("通话通断");
                        }
                    });
                }
            };
            sessionFor.setListener(listener);
        } catch (SipException e) {
            e.printStackTrace();
        }
    }

六:主动挂电话

 incomingCall.endCall();

最后

github 上的地址,可以下载下来玩玩

SipDemo

上一篇下一篇

猜你喜欢

热点阅读