4 Replies Latest reply on Jul 23, 2015 7:28 AM by Jon-Luke West

    EMDKManager.getEMDKManager() is locking my GUI thread. Workarounds?

    Jon-Luke West

      Hello,

       

      I am writing an Android application using the EMDK.  I am noticing that the end of the EMDKManager.getEMDKManager() method is holding up my GUI when launching an activity or adding a fragment to an activity.  This causes an awkward delay between a method being called to interface with the device and the GUI being displayed telling the user what is happening.  (roughly 1 second)  I have been playing with different threading architectures in order to circumvent this, but I am out of ideas.  In the following example, I am adding a fragment to the current activity which performs a an unlock of the cradle the device is in using the EMDK, displays a screen saying that is what is happening and then cleans itself up after 15 seconds. (Timing is handled by the parent activity.)

       

      public class myFragment extends Fragment
              implements EMDKManager.EMDKListener
      {
           private EMDKManager manager = null;
           private static final String LOGTAG = "FRAGMENT";
           private Handler myHandler = null;
      
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
               Log.v(LOGTAG, "Creating View");
              return inflater.inflate(R.layout.activity_unlock,container,false);
          }
          @Override
          public void onResume() {
                super.onResume();
                EMDKManager.getEMDKManager(getActivity().getApplicationContext(), this);
          }
          @Override
          public void onStop() {
              Log.v(LOGTAG, "Stopping");
              if(manager != null) {
                  Log.v(LOGTAG, "Releasing EMDK");
                  manager.release();
                  manager = null;
              }
              super.onStop();
          }
           
           @Override
          public void onClosed() {
                Log.v(LOGTAG, "Releasing EMDK");
              manager.release();
          }
      
      
           @Override
          public void onOpened(EMDKManager emdkManager) {
              this.manager = emdkManager;
              myHandler = new Handler();
              myHandler.post(threadAction);
          }
           private Runnable threadAction = new Runnable() {
              @Override
              public void run() {
                  //Perform Action
                  MC18Device.cmd_UnlockCradle(manager,10,500,500,true);
      
                  final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
                  tg.startTone(ToneGenerator.TONE_PROP_BEEP);
              }
           };
      }
              }
          };
      
      

       

       

      I notice that the EMDK unlock command that I make occurs before the layout of the fragment is actually displayed on the screen.  I have traced this to the EMDKManager.getEMDKManager() method.  It seems to be holding the GUI thread until it finishes whatever it is doing between onOpened() and returning to fragment.onResume(), but I cannot figure out an effective method of pushing this to another thread so the GUI displays before the unlock or as the unlock is taking place.  Does anyone have any recommendations?

        • Re: EMDKManager.getEMDKManager() is locking my GUI thread. Workarounds?
          Jon-Luke West

          I have managed to push the getEMDKManager() method to a new thread that is created in onResume(), but the method is still hanging my system/UI thread.  My onResume looks like this:

           

          @Override
          public void onResume() {
          
             super.onResume();
            Log.v(LOGTAG, "Starting");
             new Thread(initEMDK).start();
          }
          

           

          and I am still experiencing a delay in the GUI display.  Only when removing the call to get the EMDKManager does this delay go away.

           

          Thoughts?

            • Re: Re: EMDKManager.getEMDKManager() is locking my GUI thread. Workarounds?
              Jon-Luke West

              I have been doing more playing/research on this matter, and I have discovered another peculiarity.  Because of this little delay, if I remove the device from its cradle immediately errors are produced:

               

              07-20 14:57:53.878  20121-20177/com.opticalphusion.app D/com.opticalphusion.app.MQTTService.MQTTConnection.MsgHandler﹕ 4083de5b3e0d:NOTIFY UNLOCK

              07-20 14:57:54.003  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Notification Action Received

              07-20 14:57:54.019  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Building Fragment

              07-20 14:57:54.120  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Count-Down Started

              07-20 14:57:54.144  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Creating View

              07-20 14:57:54.190  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Starting

              07-20 14:57:54.198  20121-20600/com.opticalphusion.app D/com.symbol.emdk.EMDKServiceConnection﹕ The EMDK Service will be connected soon (asynchronus call)!

              07-20 14:57:54.276  20121-20121/com.opticalphusion.app D/com.symbol.emdk.EMDKServiceConnection﹕ The EMDK Service is now connected!

              07-20 14:57:54.284  20121-20601/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Unlock Cradle

              07-20 14:57:54.284  20121-20601/com.opticalphusion.app D/com.symbol.emdk.personalshopper.PersonalShopper﹕ Initialized

              07-20 14:57:56.690  20121-20601/com.opticalphusion.app E/Cradle﹕ Failure in unlock

              07-20 14:57:56.690  20121-20601/com.opticalphusion.app E/OPIMC18DEVICE﹕ Failure

              07-20 14:57:56.690  20121-20601/com.opticalphusion.app D/com.symbol.emdk.personalshopper.PersonalShopperHelper﹕ release

              07-20 14:58:04.128  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Stopping

              07-20 14:58:04.636  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Stopping

              07-20 14:58:04.636  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Releasing EMDK

              07-20 14:58:04.644  20121-20121/com.opticalphusion.app D/com.symbol.emdk.EMDKServiceConnection﹕ The connection to the EMDK Service was closed.!

               

              If I wait a second to remove the device from the cradle, no errors are produced:

               

              07-20 14:59:25.425  20121-20177/com.opticalphusion.app D/com.opticalphusion.app.MQTTService.MQTTConnection.MsgHandler﹕ 4083de5b3e0d:NOTIFY UNLOCK

              07-20 14:59:25.558  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Notification Action Received

              07-20 14:59:25.581  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Building Fragment

              07-20 14:59:25.675  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Count-Down Started

              07-20 14:59:25.698  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Creating View

              07-20 14:59:25.745  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Starting

              07-20 14:59:25.761  20121-21257/com.opticalphusion.app D/com.symbol.emdk.EMDKServiceConnection﹕ The EMDK Service will be connected soon (asynchronus call)!

              07-20 14:59:25.847  20121-20121/com.opticalphusion.app D/com.symbol.emdk.EMDKServiceConnection﹕ The EMDK Service is now connected!

              07-20 14:59:25.847  20121-21258/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Unlock Cradle

              07-20 14:59:25.847  20121-21258/com.opticalphusion.app D/com.symbol.emdk.personalshopper.PersonalShopper﹕ Initialized

              07-20 14:59:27.112  20121-21258/com.opticalphusion.app D/com.symbol.emdk.personalshopper.PersonalShopperHelper﹕ release

              07-20 14:59:35.683  20121-20121/com.opticalphusion.app V/NOTIFICATIONACTIVITY﹕ Stopping

              07-20 14:59:36.175  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Stopping

              07-20 14:59:36.175  20121-20121/com.opticalphusion.app V/UNLOCKFRAGMENT﹕ Releasing EMDK

              07-20 14:59:36.183  20121-20121/com.opticalphusion.app D/com.symbol.emdk.EMDKServiceConnection﹕ The connection to the EMDK Service was closed.!

            • Re: EMDKManager.getEMDKManager() is locking my GUI thread. Workarounds?
              Billie Hecox

              Jon-Luke,  the service manager will always return on the main thread, there is no work around to avoid the blocking  main thread.  Moving the getEMDKManger() call to your OnCreate could save some time, as it would only be done once.