Android Question Exception using JavaObject and AdditionalJar SDK

Glpm_2017

Member
Licensed User
Hello, I'be been trying to use the JavaObject to access a Java API for a rfid reader sdk.
The fist code I tried is below :

B4X:
#Region  Project Attributes
   #ApplicationLabel: Test RFID
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: True
#End Region

#AdditionalJar: DeviceAPIver20150929

Sub Process_Globals
   Dim mReader As JavaObject
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

       mReader.InitializeStatic("com.rscja.deviceapi.RFIDWithUHF")
       mReader.RunMethod("init",Null)   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

When running this code, the following exception occurs :

java.lang.IllegalArgumentException: expected receiver of type com.rscja.deviceapi.RFIDWithUHF, but got java.lang.Class<com.rscja.deviceapi.RFIDWithUHF>

in the line
mReader.RunMethod("init",Null)

Next, I tried to use the following :

B4X:
Sub Activity_Create(FirstTime As Boolean)
       Dim F As Boolean
       mReader.InitializeNewInstance("com.rscja.deviceapi.RFIDWithUHF",Null)
       F =mReader.RunMethod("init",Null)

End Sub

which throws me the error :

java.lang.IllegalAccessException: access to constructor not allowed

I have just a basic knowledge of Java, not enough to determine the cause of the errors...

Could someone please give me some advice on this issue ?

Thanks in advance for your kind assistance.
 

Glpm_2017

Member
Licensed User
Hi, Erel, thanks for your patience. I've done a search in the java code of the sample program, and here is what I've found :
The creation of the RFIDWithUHF in the main activity, is shown below (only part of the code is shown) :
B4X:
package com.example.uhf;
import com.example.uhf.fragment.UHFKillFragment;
import com.example.uhf.fragment.UHFLockFragment;
import com.example.uhf.fragment.UHFReadFragment;
import com.example.uhf.fragment.UHFReadTagFragment;
import com.example.uhf.fragment.UHFSetFragment;
import com.example.uhf.fragment.UHFWriteFragment;
import com.rscja.deviceapi.RFIDWithUHF;
import com.rscja.deviceapi.exception.ConfigurationException;
import com.rscja.utility.StringUtility;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class UHFMainActivity extends BaseTabFragmentActivity {

   private final static String TAG = "MainActivity";

   public RFIDWithUHF mReader;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           initViewPageData();
           initViewPager();
           initTabs();     
       try {
           mReader = RFIDWithUHF.getInstance();
       } catch (Exception ex) {

           Toast.makeText(UHFMainActivity.this, ex.getMessage(),
                   Toast.LENGTH_SHORT).show();

           return;
       }

       if (mReader != null) {
           new InitTask().execute();
       }
   }

The instance is created in the line :
public RFIDWithUHF mReader;

I've decompiled te api jar file to inspect the class declaration. Below is a part of the code for the class RFIDWithUHF :

B4X:
package com.rscja.deviceapi;

import android.util.Log;
import com.rscja.deviceapi.DeviceAPI;
import com.rscja.deviceapi.a;
import com.rscja.deviceapi.b;
import com.rscja.deviceapi.entity.SimpleRFIDEntity;
import com.rscja.deviceapi.exception.ConfigurationException;
import com.rscja.utility.StringUtility;
import java.text.DecimalFormat;
import java.util.Arrays;

public class RFIDWithUHF extends a {
    private static RFIDWithUHF a = null;
    protected b config = b.e();

    protected DeviceAPI getDeviceAPI() {
        return DeviceAPI.a();
    }

    public static synchronized RFIDWithUHF getInstance() throws ConfigurationException {
        if(a == null) {
            a = new RFIDWithUHF();
        }

        return a;
    }

    private RFIDWithUHF() throws ConfigurationException {
    }

    public synchronized boolean init() {
        int var1;
        if((var1 = this.getDeviceAPI().UHFInit(this.config.j())) == 0) {
            if((var1 = this.getDeviceAPI().UHFOpenAndConnect(this.config.k())) == 0) {
                this.setPowerOn(true);
                return true;
            }

            Log.e("RFIDWithUHF", "init() err UHFOpenAndConnect result:" + var1);
        } else {
            Log.e("RFIDWithUHF", "init() err UHFInit result:" + var1);
        }

        return false;
    }

It seems that the class RFIDWithUHF extends the class a ( probably com.rscja.deviceapi.a).
Also, the constructor for the class is really private and empty, with no code.
Base on this information, I've been figuring out whether it is necessary first create a javaobject for the class "a" and then a javaobject for the class RFIDWithUHF.
Am I in the right direction ? If so, how should the objects for both classes be created in B4A ?
Thanks.
 
Upvote 0

Glpm_2017

Member
Licensed User
I succeded in obtain the reader object using the getInstance method. It even exposed some info about the reader ( like Model, baudrate, etc ).
However, when I try RunMethod("init",null), or any other, which is supposed to be the first thing to do after the object is created, I get the following error message.

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 341)
java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
   at com.rscja.deviceapi.main._activity_create(main.java:341)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
   at com.rscja.deviceapi.main.afterFirstLayout(main.java:102)
   at com.rscja.deviceapi.main.access$000(main.java:17)
   at com.rscja.deviceapi.main$WaitForLayout.run(main.java:80)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:103)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5292)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.rscja.deviceapi-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.rscja.deviceapi-1, /vendor/lib, /system/lib]]]: findLibrary returned null
   at java.lang.Runtime.loadLibrary(Runtime.java:365)
   at java.lang.System.loadLibrary(System.java:526)
   at com.rscja.deviceapi.DeviceAPI.<clinit>(DeviceAPI.java:1459)
   at com.rscja.deviceapi.RFIDWithUHF.getDeviceAPI(RFIDWithUHF.java:41)
   at com.rscja.deviceapi.RFIDWithUHF.init(RFIDWithUHF.java:214)
   ... 19 more
 
Upvote 0

Glpm_2017

Member
Licensed User
Yes, i was suspecting that would be the case...
Along with the manufacturer's API Jar file I found a bunch of so files.
So, my next question would be how to create this library in Android Studio? Could someone guide me in this task or point me a "spoon-feeding" tutorial for this ? Or is someone from this forum willing to make this work for me for some bucks ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upload the example code, the library and all you have about documentation to google drive, dropbox, your webserver and give me an download-url. I´ll check it
 
Upvote 0

Glpm_2017

Member
Licensed User
Thanks, @DonManfred !
Excellent !
In the example you created I'm getting an error :

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 32 (Main)
java.lang.NullPointerException
   at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
   at android.app.AlertDialog.<init>(AlertDialog.java:98)
   at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.onPreExecute(RFIDWithUHFwrapper.java:183)
   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
   at android.os.AsyncTask.execute(AsyncTask.java:535)
   at de.donmanfred.RFIDWithUHFwrapper.Initialize(RFIDWithUHFwrapper.java:54)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
   at b4a.example.main.afterFirstLayout(main.java:102)
   at b4a.example.main.access$000(main.java:17)
   at b4a.example.main$WaitForLayout.run(main.java:80)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:103)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5292)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
   at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

The line is UHF.Initialize("UHF")

Best Regards
 
Upvote 0

Glpm_2017

Member
Licensed User
Hi, tried the new version. The new error is below :

B4X:
Logger connected to: ECH400323
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: An error occured while executing doInBackground()
   at android.os.AsyncTask$3.done(AsyncTask.java:300)
   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/b4a.example-1.apk"],nativeLibraryDirectories=[/data/app-lib/b4a.example-1, /vendor/lib, /system/lib]]]: findLibrary returned null
   at java.lang.Runtime.loadLibrary(Runtime.java:365)
   at java.lang.System.loadLibrary(System.java:526)
   at com.rscja.deviceapi.DeviceAPI.<clinit>(SourceFile:976)
   at com.rscja.deviceapi.RFIDWithUHF.getDeviceAPI(SourceFile:34)
   at com.rscja.deviceapi.RFIDWithUHF.init(SourceFile:183)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.doInBackground(RFIDWithUHFwrapper.java:164)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.doInBackground(RFIDWithUHFwrapper.java:158)
   at android.os.AsyncTask$2.call(AsyncTask.java:288)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   ... 4 more
** Activity (main) Pause, UserClosed = true **

When I load the program it warns me that it was written in a newer version of B4A. My version is 7.30.

REgards,
 
Upvote 0

Glpm_2017

Member
Licensed User
Unfortunately, the same error as before :
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
java.lang.RuntimeException: An error occured while executing doInBackground()
   at android.os.AsyncTask$3.done(AsyncTask.java:300)
   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/b4a.example-2.apk"],nativeLibraryDirectories=[/data/app-lib/b4a.example-2, /vendor/lib, /system/lib]]]: findLibrary returned null
   at java.lang.Runtime.loadLibrary(Runtime.java:365)
   at java.lang.System.loadLibrary(System.java:526)
   at com.rscja.deviceapi.DeviceAPI.<clinit>(SourceFile:976)
   at com.rscja.deviceapi.RFIDWithUHF.getDeviceAPI(SourceFile:34)
   at com.rscja.deviceapi.RFIDWithUHF.init(SourceFile:183)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.doInBackground(RFIDWithUHFwrapper.java:180)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.doInBackground(RFIDWithUHFwrapper.java:174)
   at android.os.AsyncTask$2.call(AsyncTask.java:288)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   ... 4 more

in the line uhf.Initialize("UHF")

Regards,
 
Upvote 0

Glpm_2017

Member
Licensed User
Just wondering if it would be possible to use B4A javaobject with the original jar api file and create a lib just for the so files, as Erel commented in the post #8, instead of wrapping the whole lib. Anyway, as Java is not in my league, please disregard this if it sounds too stupid..
 
Upvote 0

Glpm_2017

Member
Licensed User
Added the AdditionalJar line to the project, but the error still persists...

B4X:
Logger connected to: ECH400323
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
java.lang.RuntimeException: An error occured while executing doInBackground()
   at android.os.AsyncTask$3.done(AsyncTask.java:300)
   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/b4a.example-2.apk"],nativeLibraryDirectories=[/data/app-lib/b4a.example-2, /vendor/lib, /system/lib]]]: findLibrary returned null
   at java.lang.Runtime.loadLibrary(Runtime.java:365)
   at java.lang.System.loadLibrary(System.java:526)
   at com.rscja.deviceapi.DeviceAPI.<clinit>(SourceFile:976)
   at com.rscja.deviceapi.RFIDWithUHF.getDeviceAPI(SourceFile:34)
   at com.rscja.deviceapi.RFIDWithUHF.init(SourceFile:183)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.doInBackground(RFIDWithUHFwrapper.java:180)
   at de.donmanfred.RFIDWithUHFwrapper$InitTask.doInBackground(RFIDWithUHFwrapper.java:174)
   at android.os.AsyncTask$2.call(AsyncTask.java:288)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   ... 4 more

[\code]
 
Upvote 0
Top