Android Question Third part library - JavaObject

LuigiTasca

Member
Licensed User
Longtime 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

B4X:
#AdditionalJar: companyregdevicelibrary

In this library there is an "IButton" object that I initialize with JavaObject

B4X:
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:
B4X:
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
B4X:
public interface StatCallback
{
void onChangebutton(boolean stat, byte[] data);
};
And here and exaple for the callback:
B4X:
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
B4X:
activityBA.raiseEvent(activityBA, "test123", "");

Do you have any ideas?

Ask me if you need more informations

Best regards
 

LuigiTasca

Member
Licensed User
Longtime 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).

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

Here from the documentation:
B4X:
int setCallback(StatCallback cb)
Function:
Register a call-back handler. By registering, the call-back handler is called when an event
occurred.
Parameter:
cb:Specify the instance of the class by which StatCallback was implemented.
If null is specified, call-back call is stopped.
Also close deactivate call-back handler.
Return value:
int response: response ( See 2.7.7 )
“SUCCESS” for Normal end, Error code for Abnormal end


Here an example from the manual:

B4X:
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.

B4X:
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))


 
Upvote 0

LuigiTasca

Member
Licensed User
Longtime User
Ok , I've resolved.
I've change :
B4X:
status = obj.RunMethod("setCallback",Array As Object(Act))
'with
status = ComDevice.RunMethod("setCallback",Array As Object(obj))

Thanks for the reply
 
Upvote 0
Top