SIP connection - Softphone Java Android

Asked

Viewed 351 times

5

My intention is to create a Softphone, for this I am consulting the libraries of google and doing some tests only that I have enclosed in a part.

I don’t want to do start functions, or multiple screens, or anything complicated. I just want to make a call.

I created the instances as google says and etc. Follows below my code.

Mainactivity.java

package br.com.coligarse.testevoip;

import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.text.ParseException;

public class MainActivity extends AppCompatActivity {
    TextView status;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SipManager mSipManager = null;

        if (mSipManager == null) {
            mSipManager = SipManager.newInstance(this);
        }
        SipProfile mSipProfile = null;


        SipProfile.Builder builder = null;
        try {
            builder = new SipProfile.Builder("100", "voip.servidor.net.br:5082");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        builder.setPassword("123456");
        mSipProfile = builder.build();

        try {
            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.");
                }

            });
        } catch (SipException e) {
            e.printStackTrace();
        }

    }

    public void updateStatus(String msg){
        status = (TextView) findViewById(R.id.status);
        status.setText(msg);
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.coligarse.testevoip">
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="br.com.coligarse.testevoip.MainActivity">

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

When I run this code. It opens the app blank and in the sequence it says it stopped working and closes.

Could someone tell me where I’m going wrong or give me some guidance on how to continue?

I really have doubts about how to proceed. I’ve read several documents, but I’m a beginner and I’m not finding a right Romo. My intention is to develop everything in android studio by SDK without using external plugins or NDK.

LOGCAT

 --------- beginning of crash
 09-12 13:06:47.543 2416-2416/br.com.coligarse.testevoip E/AndroidRuntime: FATAL EXCEPTION: main
 Process: br.com.coligarse.testevoip, PID: 2416
 java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.coligarse.testevoip/br.com.coligarse.testevoip.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.sip.SipManager.setRegistrationListener(java.lang.String, android.net.sip.SipRegistrationListener)' on a null object reference
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.sip.SipManager.setRegistrationListener(java.lang.String, android.net.sip.SipRegistrationListener)' on a null object reference
 at br.com.coligarse.testevoip.MainActivity.onCreate(MainActivity.java:38)
 at android.app.Activity.performCreate(Activity.java:6237)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  • Can post error log ??

  • logcat posted , a conferred by kindness

  • Checks whether the mSipProfile or the mSipManager is not null when you call setRegistrationListener

1 answer

1

The logcat says:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.sip.SipManager.setRegistrationListener(java.lang.String, android.net.sip.SipRegistrationListener)' on a null object reference

That is, the mSipManager era null.

See the documentation of the method SipManager.newInstance(Context):

newInstance

SipManager newInstance (Context context)

Creates a manager instance. Returns null if SIP API is not supported.

Parameters

context Context: application context for Creating the manager Object

Returns

SipManager the manager instance or null if SIP API is not supported

Translating into Portuguese:

newInstance

SipManager newInstance (Context context)

Creates an instance of manager. Returns null if the SIP API is not supported.

Parameters

context Context: context of the application for creating the manager object

Returns

SipManager the manager instance or null if the SIP API is not supported

That is, the method newInstance(Context) can return null. You don’t treat this in your code. See:

    SipManager mSipManager = null;

    // SEMPRE vai entrar.
    if (mSipManager == null) {
        // Pode retornar null.
        mSipManager = SipManager.newInstance(this);
    }

    // ...

    try {
        // Se o newInstance retornou null, vai dar NullPointerException.
        mSipManager.setRegistrationListener(/* ... */);

That is, if the SipManager.newInstance(Context) return null, your mSipManager.setRegistrationListener will give a NullPointerException.

The solution to this is first, check whether the SipManager.newInstance(Context) returns null, and if that is the case treat it properly.

Secondly, I think you should use the InCallService instead of AppCompatActivity.

  • now my oncreate that gave problem I changed the manifest but the main oncreate is not rolling

  • @Jasarorion You’re using this on a real cell phone with chip enabled or are using it in android studio?

  • true cell phone with chip enabled ( LG G3 )

  • @Jasarorion Well, then I don’t know. I hope my answer has at least helped you a little. Good luck there, and if you find the solution, don’t forget to post it here to share the knowledge with everyone.

  • yes I’m googling now to find out why my on create da main stopped working when I took Appcompactactivity

  • I couldn’t make it work ;(

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.