3

I have been very persistent in solving this problem, I've been to almost every single website which talks about the subject (including old stack overflow posts), now I am trying to follow the Android "official" documentation in order to make my SIP client register with my asterisk server, the app is not even displaying status messages that I've setup in a function, The official Android Studio code is so terrible that I made modification and corrections to make it compile, and still after 3 days, I got no where with the results, I can't make a breakthrough, this is what I have in my only activity Java file:

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.net.sip.SipSession;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.text.ParseException;


public class MainActivity extends AppCompatActivity {

    public void updatestatus(TextView tv, String st){
        tv.setText(st);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //cancel begin

        TextView tv1=(TextView) findViewById(R.id.txt1);
        TextView tv2=(TextView) findViewById(R.id.txt2);
        TextView tv3=(TextView) findViewById(R.id.txt3);
        TextView tv4=(TextView) findViewById(R.id.txt4);
        final TextView tv5=(TextView) findViewById(R.id.txt5);
        tv1.setText("THIS IS BEFORE MANAGER");
        SipManager mSipManager = null;
        SipSession ss=null;
        if(mSipManager == null) {
            mSipManager = SipManager.newInstance(this);

        }


       // ss.setListener(new SipSession.Listener());

        tv2.setText("THIS IS AFTER MANAGER");
        SipProfile mSipProfile = null;
        SipProfile.Builder builder = null;
        tv3.setText("THIS IS BEFORE PRIFILE");
        try {
            builder = new SipProfile.Builder("hey2", "192.168.0.5");
            builder.setPassword("tech1");
        } catch (ParseException e) {
            tv1.setText(e.toString());
        }


        mSipProfile = builder.build();
        //mSipProfile = builder.build();


        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.USE_SIP)
                == PackageManager.PERMISSION_GRANTED) {

            System.out.println("GRANTEDDDDDDDDDDDDDDDDD");
        }
        else{
            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.USE_SIP},
                    1);
        }

        try {
            mSipManager.register(mSipProfile,30, new SipRegistrationListener() {

                public void onRegistering(String localProfileUri) {
                    updatestatus(tv5,"Registering with SIP Server...");
                }

                public void onRegistrationDone(String localProfileUri, long expiryTime) {
                    updatestatus(tv5,"Ready...");
                }

                public void onRegistrationFailed(String localProfileUri, int errorCode,
                                                 String errorMessage) {

                    updatestatus(tv5,"Registration failed.");
                }
            });
        } catch (SipException e) {
            e.printStackTrace();
        }
        SipAudioCall call ;

        SipAudioCall.Listener listener;


        listener = new SipAudioCall.Listener() {

            @Override
            public void onCallEstablished(SipAudioCall call) {
                call.startAudio();
                call.setSpeakerMode(true);
                call.toggleMute();


            }

            @Override
            public void onCallEnded(SipAudioCall call) {

            }
        };

        try {
            call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:1234@192.168.0.5", listener, 30);
        } catch (SipException e) {
            e.printStackTrace();
        }
    }
}

the code runs and the activity is launching on the Android, but nothing ever happens, no registration with the server, no audio call no updatestatus TextView is displayed (see updateStatus function), nothing.

Any ideas? Suggestions?

UPDATE After IvBaranov's answer:

Still no effect, I also tried registering with a SIP provider (not my asterisk server) and still nothing (by nothing i mean the monitor in the Android Studio console does not print the things expected i.e:registering with server...), so it seems that the program is not entering the registration methods (because it does not reach for the printing/logging part), However I am getting output with some runtime exceptions and other warning, this is my current code integrated with IvBaranov's answer:

import android.Manifest;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.net.sip.SipSession;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.text.ParseException;


public class MainActivity extends AppCompatActivity {


    public void updatestatus(TextView tv, String st){
        tv.setText(st);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //cancel begin

        TextView tv1=(TextView) findViewById(R.id.txt1);
        TextView tv2=(TextView) findViewById(R.id.txt2);
        TextView tv3=(TextView) findViewById(R.id.txt3);
        TextView tv4=(TextView) findViewById(R.id.txt4);
        final TextView tv5=(TextView) findViewById(R.id.txt5);
        tv1.setText("THIS IS BEFORE MANAGER");
        SipManager mSipManager = null;
        SipSession ss=null;
        if(mSipManager == null) {
            mSipManager = SipManager.newInstance(this);

        }



       // ss.setListener(new SipSession.Listener());

        tv2.setText("THIS IS AFTER MANAGER");
       SipProfile mSipProfile = null;
        SipProfile.Builder builder = null;
        tv3.setText("THIS IS BEFORE PRIFILE");
        try {
            builder = new SipProfile.Builder("hey2", "192.168.0.5");//my asterisk server running on that IP, to make sure it's reachable i made sure that I can register with Zoiper App (softphone) and I can
            builder.setPassword("tech1");
        } catch (ParseException e) {
            tv1.setText(e.toString());
        }


        mSipProfile = builder.build();
        //mSipProfile = builder.build();




        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.USE_SIP)
                == PackageManager.PERMISSION_GRANTED) {

          System.out.println("GRANTEDDDDDDDDDDDDDDDDD");
        }
        else{

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.USE_SIP},
                    1);

        }

        //begin IvBaranov answer
        try {
            mSipManager.setRegistrationListener(mSipProfile.getUriString(),
                    new SipRegistrationListener() {

                        public void onRegistering(String localProfileUri) {
                            Log.i("SipTest", "Registering with SIP Server...");
                        }

                        public void onRegistrationDone(String localProfileUri, long expiryTime) {
                            Log.i("SipTest", "Ready");
                        }

                        public void onRegistrationFailed(String localProfileUri, int errorCode,
                                                         String errorMessage) {
                            Log.i("SipTest", "Registration failed.  Please check settings.");
                        }
                    });
        } catch (SipException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent();
        intent.setAction("android.SipDemo.INCOMING_CALL");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
        try {
            mSipManager.open(mSipProfile, pendingIntent, null);
        } catch (SipException e) {
            e.printStackTrace();
        }

        //end IvBaranov answer
        SipAudioCall call ;

        SipAudioCall.Listener listener;



        listener = new SipAudioCall.Listener() {

            @Override
            public void onCallEstablished(SipAudioCall call) {
                call.startAudio();
                call.setSpeakerMode(true);
                call.toggleMute();


            }

            @Override
            public void onCallEnded(SipAudioCall call) {

            }
        };

        try {
            call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:1234@192.168.0.5", listener, 30); //where 1234 is the extension of a user called hey1, the registered user (caller) is hey2 with extension 4321
        } catch (SipException e) {
            e.printStackTrace();
        }

    }
}

And my Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aeronavigator.voip">


    <receiver android:name=".IncomingCallReceiver" android:label="Call Receiver"/>

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


    <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>
    <uses-permission android:name="android.permission.USE_SIP" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

</manifest>

The output I am getting from the monitor:

05-30 13:03:36.710 7812-7937/? W/art: Native thread exiting without having called DetachCurrentThread (maybe it's going to use a pthread_key_create destructor?): Thread[22,tid=7937,Native,Thread*=0xb9c787e8,peer=0x22c03b30,"CL--1643955920"]
05-30 13:03:36.854 7751-7771/? E/MC_LineReader: Error opening /sys/class/power_supply/bms/resistance
                                                java.io.FileNotFoundException: /sys/class/power_supply/bms/resistance: open failed: ENOENT (No such file or directory)
                                                    at libcore.io.IoBridge.open(IoBridge.java:452)
                                                    at java.io.FileInputStream.<init>(FileInputStream.java:76)
                                                    at java.io.FileInputStream.<init>(FileInputStream.java:103)
                                                    at java.io.FileReader.<init>(FileReader.java:66)
                                                    at com.motorola.motocare.util.LineReader.<init>(LineReader.java:20)
                                                    at com.motorola.motocare.util.LineReader$1.<init>(LineReader.java:53)
                                                    at com.motorola.motocare.util.LineReader.firstLineReader(LineReader.java:53)
                                                    at com.motorola.motocare.util.BatteryUtils.getBatteryResistance(BatteryUtils.java:130)
                                                    at com.motorola.motocare.action.BatteryLevelAction.onReceiveImpl(BatteryLevelAction.java:42)
                                                    at com.motorola.motocare.util.BackgroundReceiver$1.run(BackgroundReceiver.java:14)
                                                    at android.os.Handler.handleCallback(Handler.java:746)
                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                    at android.os.Looper.loop(Looper.java:148)
                                                    at android.os.HandlerThread.run(HandlerThread.java:61)
                                                 Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                    at libcore.io.Posix.open(Native Method)
                                                    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
                                                    at libcore.io.IoBridge.open(IoBridge.java:438)
                                                    at java.io.FileInputStream.<init>(FileInputStream.java:76) 
                                                    at java.io.FileInputStream.<init>(FileInputStream.java:103) 
                                                    at java.io.FileReader.<init>(FileReader.java:66) 
                                                    at com.motorola.motocare.util.LineReader.<init>(LineReader.java:20) 
                                                    at com.motorola.motocare.util.LineReader$1.<init>(LineReader.java:53) 
                                                    at com.motorola.motocare.util.LineReader.firstLineReader(LineReader.java:53) 
                                                    at com.motorola.motocare.util.BatteryUtils.getBatteryResistance(BatteryUtils.java:130) 
                                                    at com.motorola.motocare.action.BatteryLevelAction.onReceiveImpl(BatteryLevelAction.java:42) 
                                                    at com.motorola.motocare.util.BackgroundReceiver$1.run(BackgroundReceiver.java:14) 
                                                    at android.os.Handler.handleCallback(Handler.java:746) 
                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                    at android.os.Looper.loop(Looper.java:148) 
                                                    at android.os.HandlerThread.run(HandlerThread.java:61) 

Also this (during running the program) :

05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err: android.net.sip.SipException: SipService.createSession() returns null
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.net.sip.SipManager.register(SipManager.java:496)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at com.aeronavigator.voip.MainActivity.onCreate(MainActivity.java:84)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.Activity.performCreate(Activity.java:6245)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.os.Looper.loop(Looper.java:148)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
05-30 13:04:14.203 8436-8436/com.aeronavigator.voip W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err: android.net.sip.SipException: VOIP API is not supported
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.net.sip.SipManager.makeAudioCall(SipManager.java:368)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at com.aeronavigator.voip.MainActivity.onCreate(MainActivity.java:133)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.Activity.performCreate(Activity.java:6245)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.os.Looper.loop(Looper.java:148)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
05-30 13:04:14.205 8436-8436/com.aeronavigator.voip W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
HMA
  • 81
  • 3
  • 11
  • VOIP API is not supported its mean your device not supported, maybe you can test it with another device – wanz Jul 22 '19 at 02:43

1 Answers1

3

Try not to register manually, but via open() SIP profile:

try {
      mSipManager.setRegistrationListener(mSipProfile.getUriString(),
          new SipRegistrationListener() {

            public void onRegistering(String localProfileUri) {
              Log.i("SipTest", "Registering with SIP Server...");
            }

            public void onRegistrationDone(String localProfileUri, long expiryTime) {
              Log.i("SipTest", "Ready");
            }

            public void onRegistrationFailed(String localProfileUri, int errorCode,
                String errorMessage) {
              Log.i("SipTest", "Registration failed.  Please check settings.");
            }
          });
    } catch (SipException e) {
      e.printStackTrace();
    }

Intent intent = new Intent();
intent.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
try {
  mSipManager.open(mSipProfile, pendingIntent, null);
} catch (SipException e) {     
  e.printStackTrace();
}

And double check your permissions in Manifest.xml. You should have:

  <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" />

Update.

To check if your device supports Android SIP stack use this methods:

SipManager.isApiSupported(), SipManager.isVoipSupported()

Otherwise consider using one of the third party SIP stacks.

IvBaranov
  • 540
  • 2
  • 10
  • 24
  • Thanks for your reply, I did not seem to have an effect, I updated the question with both files and output (after integrating your code with mine) – HMA May 30 '17 at 18:36
  • Do you use real device to run app? Some mobile vendors like AT&T or else block build-in SIP stack support. `android.net.sip.SipException: VOIP API is not supported` – IvBaranov May 30 '17 at 20:37
  • yes, iam using a real device, i looked up that error earlier, according to this answer, this is not the root issue, https://stackoverflow.com/a/9965345/8068090 , but sill it's a possibility. – HMA May 30 '17 at 20:44
  • So this methods return _true_ on your device? `SipManager.isApiSupported(), SipManager.isVoipSupported()` – IvBaranov May 30 '17 at 20:52
  • iam trying that, trying to figure out which context to pass to function – HMA May 30 '17 at 21:33
  • not sure which context to pas, but in any case i think since i already used that device to register the same sip user to my asterisk server using Zoiper app , then it should support it. – HMA May 30 '17 at 21:47
  • It does not. Zoiper uses its own SIP realization, which is not connected to android one. – IvBaranov May 30 '17 at 22:32
  • i am experiencing the same error. any 3. party app installed on my phone can connect to my sip server but my code using android.sip.net cannot register. so interesting. – Tezcan Oct 30 '17 at 13:16
  • @Tezcan, so, have you checked `SipManager.isApiSupported(), SipManager.isVoipSupported()`. Third party apps don't use `android.sip` stack for sure. But the vendor of your cellphone can restrict usage of this package. – IvBaranov Oct 30 '17 at 16:13
  • Thanks IvBranaov. I already did those checks. Also my code sends REGISTER sip packet to the server, on servers concole i see the arriving sip register message, after than server sends UnAuthorized sip message to my code (to the client i mean) as expected... but SipManager or listener never sends back authorization header to the sip server even if i setAutoRegistration(true)... – Tezcan Oct 31 '17 at 09:06
  • @Tezcan, than you have another issue, not connected with lack of VOIP stack support. Consider creating a separate question with full description of your problem. – IvBaranov Oct 31 '17 at 11:02