Android-Session Initiation Protocol(SIP)

来源:互联网 发布:php程序员自我评价 编辑:程序博客网 时间:2024/05/17 06:23

 Android includes a full SIP protocol stack and integrated call management services that let applications easily set up outgoing and incoming voice calls, without having to manage sessions, transport-level communication, or audio record or playback directly.

Here are examples of the types of applications that might use the SIP API:

  • Video conferencing.
  • Instant messaging.

Here are the requirements for developing a SIP application:

  • You must have a mobile device that is running Android 2.3 or higher.
  • SIP runs over a wireless data connection, so your device must have a data connection (with a mobile data service or Wi-Fi). This means that you can't test on AVD—you can only test on a physical device. For details, see Testing SIP Applications.
  • Each participant in the application's communication session must have a SIP account. There are many different SIP providers that offer SIP accounts.
If you are developing an application that uses the SIP API, remember that the feature is supported only on Android 2.3 (API level 9) and higher versions of the platform. Also, among devices running Android 2.3 (API level 9) or higher, not all devices will offer SIP support.

To use SIP, add the following permissions to your application's manifest:

  • android.permission.USE_SIP

<uses-feature android:name="android.hardware.sip.voip" />

Here are excerpts from the SipDemo manifest:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.android.sip">  ...     <receiver android:name=".IncomingCallReceiver" android:label="Call Receiver"/>  ...  <uses-sdk android:minSdkVersion="9" />  <uses-permission android:name="android.permission.USE_SIP" />  <uses-permission android:name="android.permission.INTERNET" />  ...  <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" /></manifest>
To use the SIP API, your application must create a SipManager object. The SipManager takes care of the following in your application:
  • Initiating SIP sessions.
  • Initiating and receiving calls.
  • Registering and unregistering with a SIP provider.
  • Verifying session connectivity.

》 You create a SipProfile object as follows:

public SipProfile mSipProfile = null;...SipProfile.Builder builder = new SipProfile.Builder(username, domain);builder.setPassword(password);mSipProfile = builder.build();

Intent intent = new Intent();intent.setAction("android.SipDemo.INCOMING_CALL");PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);mSipManager.open(mSipProfile, pendingIntent, null);

Finally, this code sets a SipRegistrationListener on the SipManager. This tracks whether the SipProfilewas successfully registered with your SIP service provider:

mSipManager.setRegistrationListener(mSipProfile.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) {    updateStatus("Registration failed.  Please check settings.");}

. For information on downloading and installing the SDK samples, see Getting the Samples.

/*** Listens for incoming SIP calls, intercepts and hands them off to WalkieTalkieActivity. */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();            }        }    }}
>Testing SIP Applications

To test SIP applications, you need the following:

  • A mobile device that is running Android 2.3 or higher. SIP runs over wireless, so you must test on an actual device. Testing on AVD won't work.
  • A SIP account. There are many different SIP providers that offer SIP accounts.
  • If you are placing a call, it must also be to a valid SIP account.

To test a SIP application:

  1. On your device, connect to wireless (Settings > Wireless & networks > Wi-Fi > Wi-Fi settings).
  2. Set up your mobile device for testing, as described in Developing on a Device.
  3. Run your application on your mobile device, as described in Developing on a Device.
  4. If you are using Android Studio, you can view the application log output by opening the Event Log console (View > Tool Windows > Event Log).
  5. Ensure your application is configured to launch Logcat automatically when it runs:
    1. Select Run > Edit Configurations.
    2. Select the Miscellaneous tab in the Run/Debug Configurations window.
    3. Under Logcat, select Show logcat automatically then select OK.
0 0