Android Question [RESOLVED] Convert Java code to B4A

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
I have this code in java:
B4X:
......
     case 5:
                    name = String.format("%d%02d%02d%02d%02d%02d.txt", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
                    f = new File(folder, name);
                    ret = irh.doGetFile(f);
                    break;
            }
            notifyMessage(ret);

        }

        private void notifyMessage(int ret) {
            Message msg = handler.obtainMessage();
            Bundle b = new Bundle();
            b.putInt("EndThread", ret);
            msg.setData(b);
            handler.sendMessage(msg);
        }
    }

  private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
          
            if(bundle.containsKey(IrHandler.MSG_VERSION_STRING)) {
                String value = bundle.getString(IrHandler.MSG_VERSION_STRING);
            }
            if(bundle.containsKey(IrHandler.MSG_SN_STRING)) {
                String value = bundle.getString(IrHandler.MSG_SN_STRING);
            }
            if(bundle.containsKey(IrHandler.MSG_BATTERY_INT)) {
                int value = bundle.getInt(IrHandler.MSG_BATTERY_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_NFILE_INT)) {
                int value = bundle.getInt(IrHandler.MSG_NFILE_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_MEMUSED_INT)) {
                memUsed = bundle.getInt(IrHandler.MSG_MEMUSED_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_MEMTOT_INT)) {
                memTot = bundle.getInt(IrHandler.MSG_MEMTOT_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_AUDIT_INT)) {
                int value = bundle.getInt(IrHandler.MSG_AUDIT_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_FILE_SIZE_INT)) {
                fSize = bundle.getInt(IrHandler.MSG_FILE_SIZE_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_FILE_READ_INT)) {
                int value = bundle.getInt(IrHandler.MSG_FILE_READ_INT);
            }
          
            if (bundle.containsKey("EndThread")) {
                int value = bundle.getInt("EndThread");
                TextView tv;
                tv = (TextView) findViewById(R.id.textStatus);
...............

Now if you call this method:

B4X:
ret = irh.doGetFile(f);

this variable "ret" i see in

notifyMessage(ret);

and this notifyMessage go in "public void handleMessage(Message msg)" and here i have:

B4X:
....
       if(bundle.containsKey(IrHandler.MSG_FILE_SIZE_INT)) {
                fSize = bundle.getInt(IrHandler.MSG_FILE_SIZE_INT);
            }
.....

this value returns the size of the file found, as well as other calls that I return data type value of readings, etc.
How do i translate this code into B4A ?
Thank you
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
Hi @Erel, All
Well...i have this code translate in B4A:

JAVA
B4X:
   .....
                   ret = irh.doDdcmp(0, 0, IrHandler.COM_RS, f);
                    break;
}
            notifyMessage(ret);
        }


private void notifyMessage(int ret) {
            Message msg = handler.obtainMessage();
            Bundle b = new Bundle();
            b.putInt("EndThread", ret);
            msg.setData(b);
            handler.sendMessage(msg);
        }



private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
           
            if(bundle.containsKey(IrHandler.MSG_VERSION_STRING)) {
                String value = bundle.getString(IrHandler.MSG_VERSION_STRING);
            }
            if(bundle.containsKey(IrHandler.MSG_SN_STRING)) {
                String value = bundle.getString(IrHandler.MSG_SN_STRING);
            }
            if(bundle.containsKey(IrHandler.MSG_BATTERY_INT)) {
                int value = bundle.getInt(IrHandler.MSG_BATTERY_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_NFILE_INT)) {
                int value = bundle.getInt(IrHandler.MSG_NFILE_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_MEMUSED_INT)) {
                memUsed = bundle.getInt(IrHandler.MSG_MEMUSED_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_MEMTOT_INT)) {
                memTot = bundle.getInt(IrHandler.MSG_MEMTOT_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_AUDIT_INT)) {
                int value = bundle.getInt(IrHandler.MSG_AUDIT_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_FILE_SIZE_INT)) {
                fSize = bundle.getInt(IrHandler.MSG_FILE_SIZE_INT);
            }
            if(bundle.containsKey(IrHandler.MSG_FILE_READ_INT)) {
                int value = bundle.getInt(IrHandler.MSG_FILE_READ_INT);
            }
           
            if (bundle.containsKey("EndThread")) {
                int value = bundle.getInt("EndThread");
....

B4A
B4X:
.....
Ritorno = Cooffee.RunMethod("doDdcmp", Array As Object(0, 0, 1, FormatoFile))   
notifyMessage(Ritorno)


Sub notifyMessage( Ritornomessaggio As Int )
    msg = message
    msg = handler.RunMethod("obtainMessage", Null)
    bundle.RunMethod("putInt", Array("EndThread", Ritornomessaggio))
    msg.RunMethod("setData", Array(bundle))
    handleMessage(handler.RunMethod("sendMessage", Array(msg)))
End Sub


Sub handleMessage( msg1 As JavaObject)
    msg1 = msg.RunMethod("getData", Null)
    If msg1.RunMethod("containsKey", Array("IrHandler.MSG_VERSION_STRING")) Then
        Dim valore As String = msg1.RunMethod("getString", Array("IrHandler.MSG_VERSION_STRING"))
        Log("Valore: " & valore)
    End If
    If msg1.RunMethod("containsKey", Array("MSG_AUDIT_INT")) Then
        Dim valoreintero As Int = msg1.RunMethod("getInt", Array("IrHandler.MSG_AUDIT_INT"))
        Log("Valore: " & valoreintero)
    End If
    If msg1.RunMethod("containsKey", Array("MSG_FILE_READ_INT")) Then
        Dim valoreintero As Int = msg1.RunMethod("getInt", Array("IrHandler.MSG_FILE_READ_INT"))
        Log("Valore: " & valoreintero)
    End If
    If msg1.RunMethod("containsKey", Array("EndThread")) Then
        Dim valoretornato As Int = msg1.RunMethod("getInt", Array("EndThread"))
        Log("Valore: " & valoretornato)
    End If
.....
End Sub

and WORK.
I have in B4A:

upload_2017-3-27_15-45-47.png


and if you see msg1 ( Debug Windows ) have
(Message) { when=-45m26s101ms barrier=0 }
(Message) { when=-45m26s102ms barrier=0 }
(Bundle) Bundle[{EndThread=0}]
In Log Windows i have:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Entro in run()
File: /storage/emulated/0/Download/20170327153719.txt
2.02
Valore: 419
Ritorno: 419
Entro in run()
File: /storage/emulated/0/Download/20170327153739.txt
2.02
Valore: 0
Ritorno: 0
Batteria: 77
MemTot: 128000
MemUsed: 0
NFile: 0
SN: 2429FD00534919E3
Version: 0.2.3 - 0
Temperature: 16.8700008392334

Anyway the question is the code WAIT the final value ( if Success comeback 0 and if ERROR come back another value ) while it should display different values while reading the same.
For example, when reading data should bring up from time to time the read values (type 10%, 30% etc.) and then at the end to give the result.
Instead the system freezes giving me only the result at the end without getting to see the data that they return while maintaining operation.
I think the key is in these instructions:
B4X:
....
private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
.....

How can i translate this in B4A ??
Any idea ??
Thank you
Marco
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi @Erel, all

I also tried with javaonline code, in this case the Worker Thread ( notifyMessage(ret)) working properly.

upload_2017-3-28_12-41-46.png


In this mode i have in Log window correct result.
Now the question is:
There is a way to have the same result in JavaObject ( see #5 ) ??
Thank you
Marco

Code B4A:
B4X:
jo.InitializeContext
jo.RunMethod("Coffe", Array(1))


Code Inline JavaCode:
B4X:
#If JAVA
// RFT -->
import com.rft.irlib.IrHandler;
// <-- RFT
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import java.io.File;
import java.util.Calendar;
private int flgchiudi;
// RFT -->
private boolean taskRunning = false;
private Handler handler;
private boolean saveImmediately = true;
// <-- RFT
// VARIABLI DA RIPORTARE
String name;
String versione = "";
int GetMemoriaTotale = 0;
int GetBatteria = 0;
int GetMemUtente = 0;
String GetSerialNumber = "";
String Ritorno = "";
int RitornoDo = 100;
public void _onCreate() {
// RFT -->
BA.Log("Entro in Create");
handler = new MyHandler();
// <-- RFT
}
public void Coffe(int c) {
if (!taskRunning) {
BA.Log("Entro TaskRunning");
IrLibStart();
irLibThread irThr = new irLibThread(this, handler);
irThr.cmd(c);
irThr.start();
}
}
class irLibThread extends Thread {
private Handler handler;
private Context context;
private int irCmdReq = 0;
public irLibThread(Context context, Handler handler) {
this.handler = handler;
this.context = context;
}
public void cmd(int c) {
irCmdReq = c;
BA.Log("irThr.cmd(" + String.format("%X", irCmdReq) + ")");
}
public void run() {
BA.Log("Entro in run()");
Calendar c = Calendar.getInstance();
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (irCmdReq == 0)
return;
IrHandler irh = new IrHandler(context, handler);
irh.setAudioFilter(IrHandler.FILTER_TYPE_1);//per s4 mini
//String name;
File f = null;
int ret = 0;
boolean saveImmediately = true;
switch (irCmdReq) {
// Trasmissione IR
case 1:
BA.Log("Entro in saveimmediately");
if (saveImmediately) {
name = String.format("%d%02d%02d%02d%02d%02d.txt", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
f = new File(folder, name);
}
ret = irh.doDdcmp(0, 0, IrHandler.COM_IR | IrHandler.COM_BAUD_2400, f);
BA.Log("Valore RET doDdcmp: " + String.format("%X", ret));
RitornoDo = ret;
versione = irh.GetVersion();
BA.Log("Valore GetVersion: " + versione);
GetMemoriaTotale = irh.GetMemTot();
BA.Log("Valore GetMemTot: " + String.format("%X", GetMemoriaTotale));
GetBatteria = irh.GetBattery();
BA.Log("Valore GetBattery: " + String.format("%X", GetBatteria));
GetMemUtente = irh.GetMemUsed();
BA.Log("Valore GetMemUsed: " + String.format("%X", GetMemUtente));
GetSerialNumber = irh.GetSN();
BA.Log("Valore GetSN: " + GetSerialNumber);
break;
// Trasmissione RS232
case 2:
if (saveImmediately) {
name = String.format("%d%02d%02d%02d%02d%02d.txt", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
f = new File(folder, name);
}
ret = irh.doDdcmp(0, 0, IrHandler.COM_RS, f);
BA.Log("Valore RET doDdcmp: " + String.format("%X", ret));
RitornoDo = ret;
versione = irh.GetVersion();
BA.Log("Valore GetVersion: " + versione);
GetMemoriaTotale = irh.GetMemTot();
BA.Log("Valore GetMemTot: " + String.format("%X", GetMemoriaTotale));
GetBatteria = irh.GetBattery();
BA.Log("Valore GetBattery: " + String.format("%X", GetBatteria));
GetMemUtente = irh.GetMemUsed();
BA.Log("Valore GetMemUsed: " + String.format("%X", GetMemUtente));
GetSerialNumber = irh.GetSN();
BA.Log("Valore GetSN: " + GetSerialNumber);
break;
}
notifyMessage(ret);
}
private void notifyMessage(int ret) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("EndThread", ret);
msg.setData(b);
handler.sendMessage(msg);
}
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
     if(bundle.containsKey(IrHandler.MSG_VERSION_STRING)) {
                String value = bundle.getString(IrHandler.MSG_VERSION_STRING);
            }
            if(bundle.containsKey(IrHandler.MSG_SN_STRING)) {
                String value = bundle.getString(IrHandler.MSG_SN_STRING);
                BA.Log(value);
            }
            if(bundle.containsKey(IrHandler.MSG_BATTERY_INT)) {
                int value = bundle.getInt(IrHandler.MSG_BATTERY_INT);
                BA.Log("** Battery: " + String.format("%X", value));
            }
            if(bundle.containsKey(IrHandler.MSG_NFILE_INT)) {
                int value = bundle.getInt(IrHandler.MSG_NFILE_INT);
                BA.Log("** NFile: " + String.format("%X", value));
            }
            if(bundle.containsKey(IrHandler.MSG_MEMUSED_INT)) {
                int memUsed = bundle.getInt(IrHandler.MSG_MEMUSED_INT);
                BA.Log("** Memuser: " + String.format("%X", memUsed));
            }
            if(bundle.containsKey(IrHandler.MSG_MEMTOT_INT)) {
                int memTot = bundle.getInt(IrHandler.MSG_MEMTOT_INT);
                BA.Log("** MemTot: " + String.format("%X", memTot));
            }
            if(bundle.containsKey(IrHandler.MSG_AUDIT_INT)) {
                int value = bundle.getInt(IrHandler.MSG_AUDIT_INT);
                BA.Log("** Audit: " + String.format("%X", value));
            }
            if(bundle.containsKey(IrHandler.MSG_FILE_SIZE_INT)) {
                int fSize = bundle.getInt(IrHandler.MSG_FILE_SIZE_INT);
                BA.Log("** fSize: " + String.format("%X", fSize));
            }
            if(bundle.containsKey(IrHandler.MSG_FILE_READ_INT)) {
                int value = bundle.getInt(IrHandler.MSG_FILE_READ_INT);
                BA.Log("** File Read Int: " + String.format("%X", value));
            }
if (bundle.containsKey("EndThread")) {
int value = bundle.getInt("EndThread");
switch (value) {
case 0:
BA.Log("SUCCESS");
Ritorno = "SUCCESS";
break;
case IrHandler.ERR_SAMPLE_FREQ:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Audio Frequency");
Ritorno = "ERROR: " + String.format("%X", value) + " Audio Frequency";
break;
case IrHandler.ERR_NOHEADSET:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " No HeadSet");
Ritorno = "N.° " + String.format("%X", value) + " No HeadSet";
break;
case IrHandler.ERR_OUTPUT:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Audio output");
Ritorno = "N.° " + String.format("%X", value) + " Audio output";
break;
case IrHandler.ERR_NOIRMODULE:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " No Ir Module");
Ritorno = "N.° " + String.format("%X", value) + " No Ir Module";
break;
case IrHandler.ERR_RX_TIMEOUT:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Timeout");
Ritorno = "N.° " + String.format("%X", value) + " Rx Timeout";
break;
case IrHandler.ERR_RX_DATA:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Data");
Ritorno = "N.° " + String.format("%X", value) + " Rx Data";
break;
case IrHandler.ERR_RX_DATALEN:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Data Length");
Ritorno = "N.° " + String.format("%X", value) + " Rx Data Length";
break;
case IrHandler.ERR_RX_FRAME:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Frame");
Ritorno = "N.° " + String.format("%X", value) + " Rx Frame";
break;
case IrHandler.ERR_RX_SYSTEM:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx system");
Ritorno = "N.° " + String.format("%X", value) + " Rx system";
break;
case IrHandler.ERR_FILE_NONE:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " No File");
Ritorno = "N.° " + String.format("%X", value) + " No File";
break;
case IrHandler.ERR_FILE_WRITE:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Write File");
Ritorno = "N.° " + String.format("%X", value) + " Write File";
break;
default:
int mask = value & 0xFF00;
switch (mask) {
case IrHandler.ERR_AUDIT_MASK:
if (value == IrHandler.ERR_AUDIT_MEMFULL)
// *** Aggiunto per visualizzare i Log
//BA.Log("ERROR: " + String.format("%X", value) + " Memory Full");
Ritorno = "N.° " + String.format("%X", value) + " Memory Full";
else
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " AUDIT FAILED");
//Ritorno = "N.° " + String.format("%X", value) + " AUDIT FAILED";
break;
case IrHandler.ERR_FILE_READ_MASK:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Read File");
Ritorno = "N.° " + String.format("%X", value) + " Read File";
break;
case IrHandler.ERR_FILE_DEL_MASK:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " File SAVED, still in module?");
Ritorno = "N.° " + String.format("%X", value) + " File SAVED, still in module ?";
break;
case IrHandler.ERR_FW_MASK:
if (value == IrHandler.ERR_FW_UPTODATE)
// *** Aggiunto per visualizzare i Log
//BA.Log("FW UPTODATE: " + String.format("%X", value) + " ");
Ritorno = "N.° " + String.format("%X", value) + " ";
else
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR FW: " + String.format("%X", value) + " ");
Ritorno = "N.° " + String.format("%X", value) + " ";
break;
default:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " ");
Ritorno = "N.° " + String.format("%X", value) + " ";
break;
}
break;
}
IrLibStop();
}
}
}
private void IrLibStart() {
taskRunning = true;
}
private void IrLibStop() {
taskRunning = false;
}
public int RitornoDo(){
return RitornoDo;
}
public String GetVersion(){
return versione;
}
public int GetBattery() {
return GetBatteria;
}
public int GetMemTot() {
return GetMemoriaTotale;
}
public int GetMemUsed() {
return GetMemUtente;
}
public String GetSN() {
return GetSerialNumber;
}
public String Ritorno(){
return Ritorno;
}
public String NomeFile(){
return name;
}
#End If
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Well.
RESOLVED with mix inline JavaCode and JavaObject

Code Java ( inline Javacode ):
B4X:
#If JAVA
...................
f = new File(folder, name);
ret = irh.doGetFile(f);
break;
// Aggiornamento Firmware
case 6:
name = "IR.FW";
f = new File(folder, name);
ret = irh.doFwUpdate(f);
break;
}
notifyMessage(ret);
}
private void notifyMessage(int ret) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("EndThread", ret);
msg.setData(b);
handler.sendMessage(msg);
}
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
     if(bundle.containsKey(IrHandler.MSG_VERSION_STRING)) {
                String value = bundle.getString(IrHandler.MSG_VERSION_STRING);
            }
            if(bundle.containsKey(IrHandler.MSG_SN_STRING)) {
                String value = bundle.getString(IrHandler.MSG_SN_STRING);
                BA.Log(value);
                if(processBA.subExists(EventName.toLowerCase(BA.cul) + "_seriale")){
                    //BA.Log("Found");
                    //BA.Log("Result: " + String.format("%1$d", result));
                    processBA.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_seriale", value);
                } else {
                    //BA.Log("Not Found");
                }
            }
...............

To get event Raise is possible write this
B4X:
 if(processBA.subExists(EventName.toLowerCase(BA.cul) + "_seriale"))
                  processBA.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_seriale", value);
                } else {
                    BA.Log("Not Found");
                }

Code B4A:

B4X:
...........
Sub btn_test_Click
    lbl_dati.TextColor = Colors.White
    Try
        Dim comando As Int = 2 'DEX SER
        Dim nomevento As String = "Leggi"
        jo2.RunMethod("Cooffee",Array(comando, nomevento))
    Catch
        Log(LastException)
    End Try
..........
End Sub

All work without problem. Fantastic B4X :D:D:D




Sub Leggi_Seriale(Result As String)
    Log($"Seriale:  ${Result}"$)
    lbl_messaggio.Text = $"Seriale:  ${Result} ${CRLF}"$
End Sub
 
Upvote 0
Top