Android Question Third part library - JavaObject

Discussion in 'Android Questions' started by LuigiTasca, Sep 19, 2019.

  1. LuigiTasca

    LuigiTasca Member Licensed User

    Hi All,
    I have to intercept, from an Android device, an event raised from a library of the device.
    I added the library with

    Code:
    #AdditionalJar: companyregdevicelibrary
    In this library there is an "IButton" object that I initialize with JavaObject

    Code:
    Dim CompDevice As JavaObject
                           
    CompDevice.InitializeNewInstance(
    "jp.co.company.vx.framework.device.IButton",Array As Object())
               
    status = CompDevice.RunMethod(
    "open",Array As Object(CompDevice.GetField("OPENMODE_COMMON"),CompDevice.GetField("DEVICE_HOST_LOCALHOST")))
    Now I have to intercept the event, by a callback, when is read from the device a sort of key.

    I tried with two paths:

    In the first one I use "CreateEvent" with JavaObject:
    Code:
    CompDevice.CreateEvent("jp.co.company.vx.framework.device.IButton.StatCallback","Test",False)
    'OR
    CompDevice.CreateEventFromUI("jp.co.company.vx.framework.device.IButton.StatCallback","Test",False)
    But the sub "Test_Event" is never called.

    This is how it's implemented
    Code:
    public interface StatCallback
    {
    void onChangebutton(boolean stat, byte[] data);
    };
    And here and exaple for the callback:
    Code:
    public class MainActivity extends Activity
    implements IButton.StatCallback {
    :
    void onChangeIbutton(boolean stat, byte[] data) {
    // this procedure should be implemented
    :
    }
    :
    int initXXX() {
    setCallback(this); // register callback
    :
    }
    }
    Where "setCallback" is used to register the event, "Register a call-back handler. By registering, the call-back handler is called when an event occurred."

    Then we tryed to use inline java code to get the event for then call an a B4A Sub with
    Code:
    activityBA.raiseEvent(activityBA, "test123""");
    Do you have any ideas?

    Ask me if you need more informations

    Best regards
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Calling CreateEvent or CreateEventFromUI doesn't do anything except of creating a special object that implements an interface. You must use the returned object and set it as a listener somehow (it depends on the API).
     
  3. warwound

    warwound Expert Licensed User

    It looks like a very clumsy thing to do using JavaObject.
    Some inline java could soon mimic that example code.
     
  4. LuigiTasca

    LuigiTasca Member Licensed User

    Can I use method "SetCallback" in your opinion to set it as a listener?

    Here from the documentation:
    Code:
    int setCallback(StatCallback cb)


    Here an example from the manual:

    Code:
    IntentFilter intentFilter = new IntentFilter("jp.co.company.vx.regdevice.comm.POWER_RECOVERY");
    private BroadcastReceiver intentReceiver;
       (略)
      @Override
      protected void onResume()
      {
        super.onResume();
        StartIbtn();
      
    }
      @Override
      protected void onPause()
      {
        super.onPause();
        EndIbtn();
      }
      private String errMessage(int errno) {
        String text = "";
        switch (errno) {
          case IButton.Response.ERR_PARAMETER:
          text = getString(R.string.error_parameter);
          break;
        case IButton.Response.ERR_NOTOPEN:
          text = getString(R.string.error_unopened);
          break;
        case IButton.Response.ERR_BOARD_NOTRUNNING:
          text = getString(R.string.error_boardnotrun);
          break;
        (.... omission ....)
      }
      return text;
    }
    private void StartIbtn()
    {
      int ret;
      ibutton = new IButton();
      ret = ibutton.open(IButton.OPENMODE_COMMON,
      IButton.DEVICE_HOST_LOCALHOST);
      if(ret < 0) {
        //---------------------
        // Display error
        //---------------------
        return;
      }
      handler = new Handler();                                            '<
      ibutton.setCallback(MainActivity.this);                           '<----------- HERE 
      callback.start();                                                        '<
    }
    private void EndIbtn()
    {
      if(ibutton != null) {
        ibutton.setCallback(null);
        ibutton.close();
      }
    }
    public void onChangeIbutton(final boolean stat, final byte[] arg1) {
      if(arg1 != null) {
        handler.post(new Runnable() {
        @Override
        public void run() {
          if(stat) {
            //---------------
            // In case there is iButton
            //---------------
          }else{
            //---------------
            // In case there is no iButton
            //---------------
          }
        }
      });
    }
    }
    On the marked code, in the line "ibutton.setCallback(MainActivity.this);" I got the error (using "ibutton" like a JavaObject) "method not matched". Anyway they pass in the example "MainActivity.this" but the method want "statCallBack" parameter.

    Code:
    Dim ComDevice As JavaObject
    ComDevice.InitializeNewInstance(
    "jp.co.company.vx.framework.device.IButton",Array As Object())

    status = ComDevice.RunMethod(
    "open",Array As Object(ComDevice.GetField("OPENMODE_COMMON"),ComDevice.GetField("DEVICE_HOST_LOCALHOST")))
              
    Dim prova1, prova2,prova3 As JavaObject
    Dim Act, NativeMe As JavaObject
    Dim obj As JavaObject, Ref As Reflector
    Act = Act.InitializeContext
               
    obj = ComDevice.CreateEvent(
    "jp.co.company.vx.framework.device.IButton.StatCallback","test",False)

    'I tried 
    status = obj.RunMethod("setCallback",Array As Object(Act))
    'and 
    status = ComDevice.RunMethod("setCallback",Array As Object(Act))

     
  5. LuigiTasca

    LuigiTasca Member Licensed User

    Ok , I've resolved.
    I've change :
    Code:
    status = obj.RunMethod("setCallback",Array As Object(Act))
    'with
    status = ComDevice.RunMethod("setCallback",Array As Object(obj))
    Thanks for the reply
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice