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?
The important java code:
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