Android Question JavaObject & AAR Call

MarcoRome

Expert
Licensed User
Longtime User
Hi Erel, hi all.
I often find wrapper aar obfuscated. In this way it becomes difficult to write wrappers.
So i think that the JavaObject is the right way to interface any JAR / AAR in our B4 programs.

I am working on one of these libraries AAR.
In the AAR file i have the following lines of code:

The Class start so:
B4X:
public class IrHandler
{
  public static final String libVersion = "1.15";
  private static boolean a = Build.VERSION.SDK_INT >= 11;
  public static final int COM_IR = 1;
......

public IrHandler(Context context, Handler handler)
  {
    this.mContext = context;
    mHandler = handler;
    mAudioManager = (AudioManager)context.getSystemService("audio");
   .......

Now i have this code in B4A:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim jo As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    'Inizializzo oggetto JavaObject
    If FirstTime Then jo.InitializeContext
End Sub


Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub

Sub GetHandler As JavaObject
   Return GetBA.GetField("handler")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

Sub Cooffee As JavaObject
   Dim jo As JavaObject
   Return jo.InitializeNewInstance("com.rft.irlib.IrHandler", Array(GetContext, GetHandler))   
  
End Sub



Sub btn_start_Click
  
    'PowerStart
    Dim batteria, memtot, memused, nfile As Int
    Dim sn, version As String
    Dim temperature As Float

    batteria = Cooffee.RunMethod("GetBattery", Null)
     Log("Batteria: " & batteria)
    memtot = Cooffee.RunMethod("GetMemTot", Null)
    Log("MemTot: " & memtot)

    memused = Cooffee.RunMethod("GetMemUsed", Null)
    Log("MemUsed: " & memused)
   
    nfile =  Cooffee.RunMethod("GetNFile", Null)
    Log("NFile: " & nfile)
   
    sn = Cooffee.RunMethod("GetSN", Null)
    Log("SN: " & sn)
   
    version = Cooffee.RunMethod("GetVersion", Null)
    Log("Version: " & version)
   
    temperature = Cooffee.RunMethod("GetTemperature", Null)
    Log("Temperature: " & temperature)
   

End Sub



Sub Activity_Resume


End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Is right this line in B4A ?:

Return jo.InitializeNewInstance("com.rft.irlib.IrHandler", Array(GetContext, GetHandler))

This is call in Java:

public IrHandler(Context context, Handler handler)
{
this.mContext = context;
mHandler = handler;
mAudioManager = (AudioManager)context.getSystemService("audio");
.......

Thank you
Marco
 

MarcoRome

Expert
Licensed User
Longtime User

This work without problem is very very power this library JO.
Another question:

I have this Method in Java:
public final void init(Context aContext, BTDataKeyModuleIface listener)
{
if (!this.p)
{
this.t = aContext;
.......

The BTDataKeyModuleIface.class i have this code:


In B4A i have this code:

B4X:
Sub Test As JavaObject
    Dim ir As JavaObject
    Return ir.InitializeNewInstance("eu.xxxx.btdatakeymodule.BTDataKeyModule", Null)
End Sub



Sub btn_start_Click
    Dim Iface As JavaObject
    Iface.InitializeStatic("eu.xxxx.btdatakeymodule.BTDataKeyModuleIface")
  
    Dim ctxt As JavaObject
    ctxt.InitializeContext
  
    Test.RunMethod("init", Array As Object(ctxt, Iface))
....

But return me:

java.lang.RuntimeException: Method: init not matched.

Where i wrong ?
Thank you
Marco
 
Last edited:
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Is so easy

In this mode work:

B4X:
Dim Iface As JavaObject = jo.CreateEvent("eu.xxxxx.btdatakeymodule.BTDataKeyModuleIface", "Coges", False)
Dim ctxt As JavaObject
ctxt.InitializeContext
  
Test.RunMethod("init", Array As Object(ctxt, Iface))

Really great and very usefull library.
Thank you again Erel.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…