Log in or register to post comments

Initializing Vuforia: Progress stuck at 50%

May 8, 2018 - 5:21am #1

Hi everyone,

I'm developing an Object Recognition App on Android and while trying to initialize Vuforia via the InitVuforiaTask as also shown in the Vuforia sample apps, progress of initialization gets stuck at 50% and the initialization loop never returns.

I'm using Vuforia version 7.1.34 on Android 8.0.0, API 26.

Funny enough things still work fine on the Vuforia Samples as such using the same setup. Initialization returns on 100% progress and things can go on from there.

What I'm doing is calling the InitVuforiaTask directly in the Main Activity, i.e. I imported/copied the InitVuforiaTask as an inner class into the Main Activity calling it from the initAR Method which I also imported/copied to my Main Activity class instead of using them via the SampleApplicationSession class as stated in the Sample Apps. 

Is there anything I'm missing? Or is this approach dead wrong?

Any help is greatly appreciated... Thanks!

My code basically looks like this:

 
public class MainActivity extends Activity implements VuforiaApplicationControl {
      ...
      ...
      ...

      protected void onCreate(final Bundle savedInstanceState) {
          LOGGER.d("onCreate " + this);
          super.onCreate(null);

          initAR(this, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      }

      ...
      ...
      ...

     // Initializes Vuforia and sets up preferences.
     public void initAR(Activity activity, int screenOrientation)
     {
       VuforiaApplicationException vuforiaException = null;
       mActivity = activity;

       if ((screenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR)
               && (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO))
         screenOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR;

       // Use an OrientationChangeListener here to capture all orientation changes.  Android
       // will not send an Activity.onConfigurationChanged() callback on a 180 degree rotation,
       // ie: Left Landscape to Right Landscape.  Vuforia needs to react to this change and the
       // SampleApplicationSession needs to update the Projection Matrix.
       OrientationEventListener orientationEventListener = new OrientationEventListener(getActvity()) {
         @Override
         public void onOrientationChanged(int i) {
           int activityRotation = getActvity().getWindowManager().getDefaultDisplay().getRotation();
           if(mLastRotation != activityRotation)
           {
             mLastRotation = activityRotation;
           }
         }

         int mLastRotation = -1;
       };

       if(orientationEventListener.canDetectOrientation())
         orientationEventListener.enable();

       // Apply screen orientation
       mActivity.setRequestedOrientation(screenOrientation);

       // As long as this window is visible to the user, keep the device's
       // screen turned on and bright:
        mActivity.getWindow().setFlags(
               WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
               WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

       mVuforiaFlags = INIT_FLAGS.GL_20;

       // Initialize Vuforia SDK asynchronously to avoid blocking the
       // main (UI) thread.
       //
       // NOTE: This task instance must be created and invoked on the
       // UI thread and it can be executed only once!
       if (mInitVuforiaTask != null)
       {
         String logMessage = "Cannot initialize SDK twice";
         vuforiaException = new VuforiaApplicationException(
                 VuforiaApplicationException.VUFORIA_ALREADY_INITIALIZATED,
                 logMessage);
         Log.e(LOGTAG, logMessage);
       }

       if (vuforiaException == null)
       {
         try {
           mInitVuforiaTask = new InitVuforiaTask();
           mInitVuforiaTask.execute();
      }
      catch (Exception e)
      {
        String logMessage = "Initializing Vuforia SDK failed";
        vuforiaException = new VuforiaApplicationException(
                VuforiaApplicationException.INITIALIZATION_FAILURE,
                logMessage);
        Log.e(LOGTAG, logMessage);
      }
    }

    if (vuforiaException != null)
    {
      // Send Vuforia Exception to the application and call initDone
      // to stop initialization process
      mSessionControl.onInitARDone(vuforiaException);
    }
  }

  // An async task to initialize Vuforia asynchronously.
  public class InitVuforiaTask extends AsyncTask
  {
    // Initialize with invalid value:
    private int mProgressValue = -1;


    protected Boolean doInBackground(Void... params)
    {
      // Prevent the onDestroy() method to overlap with initialization:
      synchronized (mLifecycleLock)
      {
        Vuforia.setInitParameters(mActivity, mVuforiaFlags, "mykeyhere");

        do
        {
          // Vuforia.init() blocks until an initialization step is
          // complete, then it proceeds to the next step and reports
          // progress in percents (0 ... 100%).
          // If Vuforia.init() returns -1, it indicates an error.
          // Initialization is done when progress has reached 100%.
          mProgressValue = Vuforia.init();

          // Publish the progress value:
          publishProgress(mProgressValue);

          // We check whether the task has been canceled in the
          // meantime (by calling AsyncTask.cancel(true)).
          // and bail out if it has, thus stopping this thread.
          // This is necessary as the AsyncTask will run to completion
          // regardless of the status of the component that
          // started is.
        } while (!isCancelled() && mProgressValue >= 0
                && mProgressValue < 100);                    // Here's where the progress gets stuck at 50%, i.e. initialization keeps looping forever 

        return (mProgressValue > 0);
      }
    }


    protected void onProgressUpdate(Integer... values)
    {
      // Do something with the progress value "values[0]", e.g. update
      // splash screen, progress bar, etc.
      Log.d(LOGTAG, "onProgressUpdate: " + values[0].toString());
    }


    protected void onPostExecute(Boolean result)
    {
      // Done initializing Vuforia, proceed to next application
      // initialization status:

      Log.d(LOGTAG, "InitVuforiaTask.onPostExecute: execution "
              + (result ? "successful" : "failed"));

      Log.d(LOGTAG, "Result of Vuforia Initialization: " + result);
      VuforiaApplicationException vuforiaException = null;

            if (result)
            {
                try {
                    //mInitTrackerTask = new InitTrackerTask();
                    //mInitTrackerTask.execute();
                }
                catch (Exception e)
                {
                    String logMessage = "Failed to initialize tracker.";
                    vuforiaException = new VuforiaApplicationException(
                            VuforiaApplicationException.TRACKERS_INITIALIZATION_FAILURE,
                            logMessage);
                    Log.e(LOGTAG, logMessage);
                }
            } else
            {
                String logMessage;

                // NOTE: Check if initialization failed because the device is
                // not supported. At this point the user should be informed
                // with a message.
                logMessage = getInitializationErrorString(mProgressValue);

                // Log error:
                Log.e(LOGTAG, "InitVuforiaTask.onPostExecute: " + logMessage
                        + " Exiting.");

                vuforiaException = new VuforiaApplicationException(
                        VuforiaApplicationException.INITIALIZATION_FAILURE,
                        logMessage);
            }

      if (vuforiaException != null)
      {
        // Send Vuforia Exception to the application and call initDone
        // to stop initialization process
        mSessionControl.onInitARDone(vuforiaException);
      }
    }
  }
Log in or register to post comments