Ranja AZ 2 Posted August 25, 2023 (edited) Hello everybody! I have a NEXGO android POS with an integrated biometric reader. I have already converted the SDKs to delphi for the biometrics reader. I also have a sample application but it's in Java (see attached). Can someone help me translate the following program lines? SM62K2UARTAPI.pas file is SDK library. app.java: package com.miaxis; import android.app.Application; import com.miaxis.alg.MxFingerAlgAPI; import com.miaxis.data.AppDataBase; import com.miaxis.finger.api.SM62K2DriverApi; import com.miaxis.utils.FileUtils; import java.io.File; import java.util.concurrent.Executor; import java.util.concurrent.Executors; /** * @author Tank * @date 2021/12/11 5:40 下午 * @des * @updateAuthor * @updateDes */ public class App extends Application { private final Executor executor = Executors.newSingleThreadExecutor(); private final SM62K2DriverApi mSM62K2DriverApi = new SM62K2DriverApi(); private final MxFingerAlgAPI mMxFingerAlgAPI = new MxFingerAlgAPI(); private static final String LicenseName = "license.txt"; public static final int RequestCode = 1001; private static App context; @Override public void onCreate() { super.onCreate(); context = this; AppDataBase.getInstance().init(this); } public String getLicenseFilePath(){ return new File(FileUtils.getPrivateDirectory(this), App.LicenseName).getAbsolutePath(); } public static App getInstance() { return context; } public SM62K2DriverApi getSM62K2DriverApi() { return this.mSM62K2DriverApi; } public MxFingerAlgAPI getMxFingerAlg() { return this.mMxFingerAlgAPI; } public Executor getExecutor() { return executor; } } in MainActivity.java: (What I want to convert in Delphi) private void open() { showLog(getString(R.string.text_open)); mMainViewModel.busy.postValue(true); mMainViewModel.opened.postValue(true); GeneralDdi.ddi_device_poweron(); SystemClock.sleep(500); try { String com = binding.etCom.getText().toString(); int baudRate = Integer.parseInt(binding.etBaudRate.getText().toString()); MxResult<?> open = App.getInstance().getSM62K2DriverApi().open(com, baudRate); showLog(open.getMsg(), true); MxResult<?> init = App.getInstance().getSM62K2DriverApi().AlgHandshakeInit(); showLog(getString(R.string.text_Alg_Handshake_Init) + "\n" + init.getMsg(), true); mMainViewModel.opened.postValue(open.isSuccess()); } catch (Exception e) { e.printStackTrace(); showLog(getString(R.string.button_open) + "\n" + e); mMainViewModel.opened.postValue(false); } mMainViewModel.busy.postValue(false); } Thank you! App.java MainActivity.java SM62K2UARTAPI.pas Edited August 25, 2023 by Ranja AZ Share this post Link to post
David Heffernan 2345 Posted August 25, 2023 What specifically do you mean by "help me translate" 1 Share this post Link to post
Ranja AZ 2 Posted August 26, 2023 The code is in Java. I don't know the Java language. So I want to do it in Delphi. Thanks! Share this post Link to post
Ranja AZ 2 Posted August 26, 2023 What I don't understand is the creation of the App class (public class App extends Application) for opening the serial port with the instructions below: MxResult<?> open = App.getInstance().getSM62K2DriverApi().open(com, baudRate); showLog(open.getMsg(), true); MxResult<?> init = App.getInstance().getSM62K2DriverApi().AlgHandshakeInit(); Is it not possible to directly create an mSM62K2DriverApi object? Regards. Share this post Link to post
David Heffernan 2345 Posted August 26, 2023 2 hours ago, Ranja AZ said: The code is in Java. I don't know the Java language. So I want to do it in Delphi. Thanks! What have you tried so far? 1 Share this post Link to post
Ranja AZ 2 Posted August 26, 2023 Here is what I tried: unit UBio306; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Controls.Presentation, classes,decoder, JustouchApi, MxAlgShankshake, SM62K2UARTAPI, nexgoSystemService_sdk_71, Androidapi.JNI.Media, FMX.Helpers.Android, Androidapi.JNI.JavaTypes, Androidapi.JNIBridge, Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo; Type TForm2 = class(TForm) ImageControl1: TImageControl; Button1: TButton; Button2: TButton; Memo1: TMemo; Button3: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Déclarations privées } DeviceEnginex : JDeviceEngine; FOnPrintListner : JOnPrintListener; public { Déclarations publiques } DeviceInfo : JDeviceInfo; POSModel : String; CanPrint : Boolean; Printerx : JPrinter1; mSM62K2DriverApi : JSM62K2DriverApi; end; TOnPrintListener=class(TJavaLocal, JOnPrintListener) public procedure onPrintResult(param0: Integer); cdecl; end; var Form2: TForm2; deviceEngine : JDeviceEngine; implementation {$R *.fmx} procedure TOnPrintListener.onPrintResult(param0: Integer); begin // end; procedure TForm2.Button1Click(Sender: TObject); begin if FOnPrintListner=Nil then FOnPrintListner := TOnPrintListener.Create; Printerx.initPrinter; Printerx.setTypeface(TJTypeface.JavaClass.DEFAULT); Printerx.appendPrnStr(StringToJString('Test this ' + FormatDateTime('dd/mm/yy-hh:nn:ss',now) + #13#10 + #13#10#13#10#13#10#13#10#13#10#13#10),18,TJAlignEnum.JavaClass.LEFT,true); Printerx.startPrint(false,FOnPrintListner); end; procedure TForm2.Button2Click(Sender: TObject); var OpenResult : JMxResult; begin OpenResult := TJMxResult.Create; OpenResult := mSM62K2DriverApi.open(StringToJString('/dev/ttyS0'),115200); Memo1.Lines.Add(JStringToString(OpenResult.getMsg)); end; procedure TForm2.Button3Click(Sender: TObject); var OpenResult : JMxResult; begin OpenResult := TJMxResult.Create; OpenResult := mSM62K2DriverApi.close; Memo1.Lines.Add(JStringToString(OpenResult.getMsg)); end; procedure TForm2.FormCreate(Sender: TObject); begin CanPrint := True; DeviceEnginex := deviceEngine; Printerx := deviceEngine.getPrinter(); DeviceInfo := deviceEngine.getDeviceInfo(); POSModel := JStringToString(DeviceInfo.GetModel()); TJGeneralDdi.JavaClass.ddi_device_poweron; sleep(1000); end; end. When I click on Button2 I get the access violation error. Share this post Link to post
darnocian 86 Posted August 26, 2023 Brian Long made a nice video on accessing Android APIs... Jim McKeeth also wrote an article which referenced the above: http://delphi.org/2014/07/custom-classes-dex/ On the various classes, you may note there are init() methods. these map onto the Java constructors (similar concept to Delphi constructors). from the app.java, I think you need to map over mSM62K2DriverApi and mMxFingerAlgAPI to Delphi fields on your form, something like: Quote FSM62K2DriverApi : JSM62K2DriverApi; FMxFingerAlgAPI : JMxFingerAlgAPI; and then initialize them : Quote FSM62K2DriverApi := TJSM62K2DriverApi.init(); FMxFingerAlgAPI :=TJMxFingerAlgAPI.init() ; then you should be able to reference the methods you are after. 1 Share this post Link to post
Ranja AZ 2 Posted August 26, 2023 Thank you! The issue is resolved now. following code: procedure TForm2.Button2Click(Sender: TObject); var OpenResult : JMxResult; begin OpenResult := TJMxResult.Create; OpenResult := mSM62K2DriverApi.open(StringToJString('/dev/ttyS0'),115200); Memo1.Lines.Add(JStringToString(OpenResult.getMsg)); end; procedure TForm2.Button3Click(Sender: TObject); var OpenResult : JMxResult; begin OpenResult := TJMxResult.Create; OpenResult := mSM62K2DriverApi.close; Memo1.Lines.Add(JStringToString(OpenResult.getMsg)); end; procedure TForm2.FormCreate(Sender: TObject); begin CanPrint := True; DeviceEnginex := deviceEngine; Printerx := deviceEngine.getPrinter(); DeviceInfo := deviceEngine.getDeviceInfo(); POSModel := JStringToString(DeviceInfo.GetModel()); TJGeneralDdi.JavaClass.ddi_device_poweron; sleep(1000); mSM62K2DriverApi := TJSM62K2DriverApi.JavaClass.init; end; Regards. 1 Share this post Link to post
darnocian 86 Posted August 26, 2023 sorry, I forgot the JavaClass before the .init 😉 1 Share this post Link to post