Android Question Lost in JavaObject land

techknight

Well-Known Member
Licensed User
Longtime User
I am trying to control a peice of hardware that the company gave me an API/Java file to. the board runs android, and I was able to compile and run my own APK files on it no problem.

But to control the hardware, I have to use the JAR file and call some native stuff to get brightness, etc.

Trouble is I dunno how to get it to work in JavaObject. I tried to add the jar file with AdditionalJar and it crashes with function not found errors.

I attached the JAR file. Here is the code in the Java example that was given to me. How do I convert to java object?

B4X:
package com.xixun.joey.aidldemo;

import com.xixun.contract.model.TaskToSetBrightness;
import com.xixun.joey.aidlset.CardService;
import com.xixun.joey.aidlset.CommunicationJoey;
import com.xixun.joey.aidlset.FpgaInfomation;
public class MainActivity extends Activity {
    private static String TAG = "aidldemo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
             
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                CardService card = null;
             
                try {
//                    Log.e(TAG, "Upgrading ...");
                    card = CommunicationJoey.getCardService(MainActivity.this);
                    if (null != card) {
                        Log.i(TAG, "Brightness is: " + card.getBrightness());
                        FpgaInfomation info = (FpgaInfomation) card.getFpgaInfomation().get(0);
                        Log.i(TAG, "Temperature is: " + info.temperature);
                        Log.i(TAG, "Humidity is: " + info.humidity);
                        Log.i(TAG, "CardVoltage is: " + info.cardVoltage);
                        Log.i(TAG, "ExternalVoltage1 is: " + info.externalVoltage1);
                        Log.i(TAG, "ExternalVoltage2 is: " + info.externalVoltage2);
                        Log.i(TAG, "DoorOpened is: " + info.doorOpened);
                        Log.i(TAG, "Smoke is: " + info.smoke);
                        TaskToSetBrightness tts = new TaskToSetBrightness();
                        card.setTimingTaskBrightness(tts);
                    } else {
                        Log.i(TAG, "Is NULL.");
                    }
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }         
        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

This is the code I tried:

B4X:
Label1.Text = JO.InitializeStatic("com.xixun.joey.aidlset.CardService").RunMethod("GetBrightness", Null)

That didnt work so I must be missing something as I see they are doing something else with: card = CommunicationJoey.getCardService(MainActivity.this);

At this point I am completely lost as I have almost zero java knowledge. Just a VB guy.

Any help here would be awesome!
 

Attachments

  • xixun_card_settings_1.2.1.jar
    23.8 KB · Views: 219

stevel05

Expert
Licensed User
Longtime User
Try this, it fails to initialize the card for me, but then I don't have one.

B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
#AdditionalJar: xixun_card_settings_1.2.1

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

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.

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("Layout1")
   
End Sub

Sub Activity_Resume
    Setup_Card
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub Setup_Card
    Dim CommunicationJoey As JavaObject
    CommunicationJoey.InitializeStatic("com.xixun.joey.aidlset.CommunicationJoey")
    Log(CommunicationJoey)
    Dim Context As JavaObject
    Context.InitializeContext
    Dim Card As JavaObject = CommunicationJoey.RunMethod("getCardService",Array(Context))
    If Card <> Null And Card.IsInitialized Then
        Log(Card.RunMethod("getBrightness",Null))
    Else
        Log("Card not found")
    End If
End Sub

Note that the methods are case sensitive, use the same case as in the example.
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I will give it a shot. Need to figure out another way to get log data back to me because this board doesnt support ADB or logging.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
got this: (Class) class com.xixun.joey.aidlset.CommunicationJoey

And then: Got card not found.

Edit: Put in a log after Dim Card as JavaObject and got this:

(JavaObject) Not initialized
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Narrowed it down to this:

B4X:
package com.xixun.joey.aidlset;

import android.content.Context;
import android.content.Intent;

public class CommunicationJoey {
   private static CardService iSet;

   public static CardService getCardService(Context context) {
       Intent service = new Intent("com.xixun.joey.aidlset.SettingsService");
       iSet = null;
       for (int i = 0; i < 5; i++) {
           context.bindService(service, new 1(), 1);
           if (iSet != null) {
               break;
           }
           System.out.println("Get communication interface NULL, try again ...");
           try {
               Thread.sleep(100);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
       return iSet;
   }
}

is returning Null.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yes, probably yhe wrong context, try this:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
#AdditionalJar: xixun_card_settings_1.2.1

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

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.

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("Layout1")
    
End Sub

Sub Activity_Resume
    Setup_Card
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub Setup_Card
    Dim CommunicationJoey As JavaObject
    CommunicationJoey.InitializeStatic("com.xixun.joey.aidlset.CommunicationJoey")
    Log(CommunicationJoey)
    Dim Card As JavaObject = CommunicationJoey.RunMethod("getCardService",Array(GetContext))
    If Card <> Null And Card.IsInitialized Then
        Log(Card.RunMethod("getBrightness",Null))
    Else
        Log("Card not found")
    End If
End Sub

Sub GetContext As JavaObject
    Return GetBA.GetField("context")
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
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
By studying the jar, it looks like all this thing is doing is trying to bind to an already running service which is started by other software on the OS.

Maybe if B4A could do this without going through javaobject it might work fine.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Without knowledge of the device or one to test, I can't think of anything else to suggest. If it's a binding problem then using java code vs access via JavaObject would make no difference.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
its a Xixun LED sign control card that runs on android.

And this is the ONLY thing the company gave me: They give you 3 different methods of development, I chose method 1. the info they provide is horribly cryptic and poorly documented as youll see. But thats the chinese for ya.

https://mdnet.majordisplay.com/xixundev.zip
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Sadly I wish I could, but without ADB I have no way to do so. the Android platform the card runs is headless. no keyb/mouse/touchscreen as it runs an LED display.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Yea but it doesnt tell me much. I am doing custom development for the card, not using the stock stuff. Anyways.

Turns out that I can connect and control the card via a websocket. I played with the javascript code that sets up a socket server and relays POST data as JSON back to the card via the websocket.

This may work fine if I can setup a websocket server on B4A when running my app, but I need access to the raw JSON data, and not "Wrapped"(object/map/list) data that b4A may impose on the websocket. Need to figure out how to do that, it would make life much easier than trying to bind a service.
 
Upvote 0
Top