Android Question Access data from in-Line Java

Harris

Expert
Licensed User
Longtime User
I have a small test app that integrates a BlueTooth device through an external jar provided by the supplier. Right now I populate a map that gets passed to Java method (and returned) using a timer.

It is working fine but is there a better way to accomplish this - like access the java variables directly - if possible?

B4X:
#AdditionalJar: bluefire-api-v5

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
     Private NativeMe As JavaObject
     Private tim1 As Timer
     Public mymp As Map
    
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.

    Private Label1 As Label
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("lomain")
    mymp.Initialize
   
    If FirstTime Then
        NativeMe.InitializeContext
        tim1.Initialize("tim1",1000)
        tim1.Enabled = True
   End If

   Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
   Log(s) 'will print Hello World!   

End Sub

Sub tim1_tick

   mymp =  NativeMe.RunMethod("showrpm", Array(mymp))
   Dim i As Int

   Label1.Text = ""
   For i = 0 To mymp.Size - 1
      If mymp.GetKeyAt(i) = "VIN" Then
         Label1.Text = Label1.Text&CRLF&mymp.GetKeyAt(i)&"  XYZ1234567890"
      Else
         Label1.Text = Label1.Text&CRLF&mymp.GetKeyAt(i)&"  "&mymp.GetValueAt(i)
      End If
   Next
[INDENT][/INDENT]
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


The important java code:

B4X:
#If JAVA

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Map;


   private boolean isConnecting;
    private boolean isConnected;
   
    private String adapterName = "";
    private int ledBrightness;
    private boolean ignoreJ1939;
    private boolean ignoreJ1708;

    private int faultIndex;
   
    private int groupNo;
    private static final int maxGroupNo = 5;
   
    private Timer connectTimer;
   
    private ConnectionStates ConnectionState = ConnectionStates.NotConnected;

    // BlueFire adapter
    private Adapter blueFire;

    private Settings appSettings;
   
public String FirstMethod() {

  sFirstMethod();

return "Started";
}



public void sFirstMethod() {

   blueFire = new Adapter(this, adapterHandler);
       
    this.setTitle("BlueFire Demo v-" + blueFire.Version);
   
    if (android.os.Build.VERSION.RELEASE.startsWith("4.0."))
            blueFire.Comm.UseInsecureConnection = true;
    else
            blueFire.Comm.UseInsecureConnection = false;          
   
    appSettings = new Settings();
   
    initializeAdapter();

    connectTimer = new Timer();
    connectTimer.schedule(new ConnectAdapter(), 1, Long.MAX_VALUE);
    BA.Log("from frist method call");

}


// the adaptor is inited and running
// the following returns the map stuffed with new values (if any)


public Map showrpm(  Map<String, String> mymp )
{
  mymp.put("RPM",String.valueOf(Truck.RPM));
  mymp.put("SPEED",String.valueOf(Truck.Speed));
  mymp.put("VIN",Truck.VIN);
  mymp.put("PEDAL",String.valueOf(Truck.AccelPedal));
  mymp.put("THROTTLEPOS",String.valueOf(Truck.ThrottlePos));
  mymp.put("DISTANCE",String.valueOf(Truck.Distance ));

  mymp.put("BATTERY",roundString(Truck.BatteryPotential,1));

   mymp.put("OIL PSI",roundString(Truck.OilPressure * Const.kPaToPSI,1));
  mymp.put("Adaptor Name",appSettings.adapterName);
  mymp.put("AVG FUEL",roundString(Truck.AvgFuelEcon * Const.KplToMpg,1));
  mymp.put("HOURMETER",String.valueOf(Truck.TotalHours));
  mymp.put("PRCTLOAD",roundString(Truck.PctLoad,1 ));
  mymp.put("FUEL",roundString(Truck.FuelUsed  * Const.LitersToGal,1 ));
  mymp.put("HIRESDISTANCE",roundString(Truck.Odometer * Const.MetersToMiles, 1) );
 

return mymp;
 
}

#End If

Screenshot_2015-11-15-13-46-12.png
 

Harris

Expert
Licensed User
Longtime User
Thanks Erel,

I got it to work from an activity, but not thru a class.
I will need it to work as a service since it needs to run always.

I had to create a Map from B4A, pass it to the java and return the populated Map.
Seems Maps created in java are not the same.

The raised event method works much faster than a timer - with much less overhead...


B4X:
Sub Process_Globals
     Private NativeMe As JavaObject
     Public mympp As Map
End Sub

Sub Globals
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("lomain")
  mympp.Initialize

  If FirstTime Then
     NativeMe.InitializeContext
  End If
  Dim s As String = NativeMe.RunMethod("FirstMethod", Array(mympp))
  Log(s)

End Sub

Private Sub MyEvent_Fire( Value As Object )
   showlab(Value)
End Sub


Public Sub showlab(mymp As Map)
Dim i As Int

Label1.Text = ""
For i = 0 To mymp.Size - 1
   If mymp.GetKeyAt(i) = "VIN" Then
      Label1.Text = Label1.Text&CRLF&mymp.GetKeyAt(i)&"  XYZ1234567890"
   Else
      Label1.Text = Label1.Text&CRLF&mymp.GetKeyAt(i)&"  "&mymp.GetValueAt(i)
   End If
Next
End Sub

B4X:
public void gatherpgns()
{

  mymp.put("RPM",String.valueOf(Truck.RPM));
  mymp.put("SPEED",String.valueOf(Truck.Speed));
  mymp.put("VIN",Truck.VIN);
  mymp.put("PEDAL",roundString(Truck.AccelPedal,1));
  mymp.put("THROTTLEPOS",String.valueOf(Truck.ThrottlePos));
  mymp.put("DISTANCE",String.valueOf(Truck.Distance ));
  mymp.put("BATTERY",roundString(Truck.BatteryPotential,1));
  mymp.put("OIL PSI",roundString(Truck.OilPressure * Const.kPaToPSI,1));
  mymp.put("Adaptor Name",appSettings.adapterName);
  mymp.put("AVG FUEL",roundString(Truck.AvgFuelEcon * Const.KplToMpg,1));
  mymp.put("HOURMETER",String.valueOf(Truck.TotalHours));
  mymp.put("PRCTLOAD",roundString(Truck.PctLoad,1 ));
  mymp.put("FUEL",roundString(Truck.FuelUsed  * Const.LitersToGal,1 ));
  mymp.put("HIRESDISTANCE",roundString(Truck.Odometer * Const.MetersToMiles, 1) );

}

private void ShowData()
    {  
        // Show truck data
      
        faultIndex++;
      
        if (blueFire.IsTruckDataChanged)
        {
            getpgns("myevent");
        }
  
   }

Firstmethod to init my map ...


B4X:
private Map<String,String> mymp ;

public String FirstMethod(Map<String,String> mpp) {

    mymp = mpp;

  sFirstMethod();  // another call to a sub which starts jar processes...

return "Started";
}


public void getpgns(String EventName ) {
   gatherpgns();
//  processBA.raiseEventFromUI(this, EventName+ "_fire", mymp);  // this call works as well....
   processBA.raiseEvent(this, EventName+ "_fire", mymp);

}
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I now have this working from a service.

Thanks
 
Upvote 0
Top