Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi JNI Event handler - Berlin


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 0
Md. Shariful Al...

Posts: 70
Registered: 9/9/01
Delphi JNI Event handler - Berlin  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 5, 2016 7:35 AM
I did this post in https://forums.embarcadero.com/thread.jspa?threadID=228719&tstart=0 also in StackOverflow http://stackoverflow.com/questions/38730572/ but not getting proper answer so I am posting here it again. Below is my classes which are extracted by java2pas:

unit com.imagpay.SwipeHandler;

interface

uses
AndroidAPI.JNIBridge,
Androidapi.JNI.JavaTypes,
com.imagpay.bP,
Androidapi.JNI.GraphicsContentViewText,
com.imagpay.bN,
com.imagpay.bY,
com.imagpay.bQ,
com.imagpay.bS,
//com.imagpay.AppContext,
com.imagpay.SwipeListener;

type
{Class forward declarations}
JSwipeHandler = interface; {com/imagpay/SwipeHandler}

JSwipeHandlerClass = interface(JObjectClass)
['{03635EA0-78D0-42EC-9632-FF6C88EB945C}']
{Property methods}
function _GetMODE_IC_CARD: Integer;
function _GetMODE_MAG_CARD: Integer;
function _GetSTREAM_TYPE: Integer;
{Methods}
function init(Param0: JContext): JSwipeHandler; cdecl;
{Properties}
property MODE_IC_CARD: Integer read _GetMODE_IC_CARD;
property MODE_MAG_CARD: Integer read _GetMODE_MAG_CARD;
property STREAM_TYPE: Integer read _GetSTREAM_TYPE;
end;

[JavaSignature('com/imagpay/SwipeHandler')]
JSwipeHandler = interface(JObject)
['{D9C76589-D5D2-47D2-9072-C6CA43929E56}']
{Property methods}
function _GetisNewVersion: Boolean;
procedure _SetisNewVersion(Value: Boolean);
{Methods}
procedure addSwipeListener(Param0: JSwipeListener); cdecl;
procedure clearEnvironment; cdecl;
function detect: Boolean; cdecl;
function getDataWithCipherCode(Param0: JString): JString; cdecl;
function getDefaultCommandSignal: SmallInt; cdecl;
function getFilterSignal: SmallInt; cdecl;
function getMode: Integer; cdecl;
function getOtherCommandSignal: SmallInt; cdecl;
function getPowerChannel: Integer; cdecl;
function getPowerFrequency: Integer; cdecl;
function getReaderVersion: JString; cdecl;
function getRetryCount: Integer; cdecl;
function getSampleRate: Integer; cdecl;
function getTimeout: Integer; cdecl;
function isCheckConnection: Boolean; cdecl;
function isConnected: Boolean; cdecl;
function isDebug: Boolean; cdecl;
function isDownloadEnvironment: Boolean; cdecl;
function isHighSpeed: Boolean; cdecl;
function isNewVersion: Boolean; cdecl;
function isPowerOn: Boolean; cdecl;
function isReadable: Boolean; cdecl;
function isReadonly: Boolean; cdecl;
function isUpload: Boolean; cdecl;
function isUploadEnvironment: Boolean; cdecl;
function isWritable: Boolean; cdecl;
procedure onDestroy; cdecl;
procedure onParseData(Param0: JString); cdecl;
procedure onReadData(Param0: JString); cdecl;
procedure powerOff; cdecl;
procedure powerOn; cdecl; overload;
procedure powerOn(Param0: Integer; Param1: Integer; Param2: SmallInt; Param3: SmallInt; Param4: Integer); cdecl; overload;
procedure powerOnWithDefaultEnvironment; cdecl;
function quickTest: Boolean; cdecl;
procedure registerReceiver; cdecl;
procedure removeSwipeListener(Param0: JSwipeListener); cdecl;
procedure setCheckConnection(Param0: Boolean); cdecl;
procedure setDebug(Param0: Boolean); cdecl;
procedure setDownloadEnvironment(Param0: Boolean); cdecl;
procedure setHighSpeed(Param0: Boolean); cdecl;
procedure setMode(Param0: Integer); cdecl;
procedure setNewVersion(Param0: Boolean); cdecl;
procedure setReadable(Param0: Boolean); cdecl;
procedure setReaderVersion(Param0: JString); cdecl;
procedure setReadonly(Param0: Boolean); cdecl;
procedure setRetryCount(Param0: Integer); cdecl;
procedure setTimeout(Param0: Integer); cdecl;
procedure setUpload(Param0: Boolean); cdecl;
procedure setUploadEnvironment(Param0: Boolean); cdecl;
procedure setWritable(Param0: Boolean); cdecl;
function test: Boolean; cdecl;
function testDevice: Boolean; cdecl;
procedure unregisterReceiver; cdecl;
procedure writeCipherCode(Param0: JString); cdecl;
end;

TJSwipeHandler = class(TJavaGenericImport<JSwipeHandlerClass, JSwipeHandler>)
end;

const
TJSwipeHandlerTAG = 'SwipeHandler';
TJSwipeHandlerSTREAM_TYPE = 3;
TJSwipeHandlerMODE_MAG_CARD = 1;
TJSwipeHandlerMODE_IC_CARD = 2;

implementation

end.

unit com.imagpay.SwipeListener;

interface

uses
AndroidAPI.JNIBridge,
Androidapi.JNI.JavaTypes,
com.imagpay.SwipeEvent;

type
{Class forward declarations}
JSwipeListener = interface; {com/imagpay/SwipeListener}

JSwipeListenerClass = interface(IJavaClass)
['{E528660F-D406-406F-8655-FFB76A64B3EA}']
end;

[JavaSignature('com/imagpay/SwipeListener')]
JSwipeListener = interface(IJavaInstance)
['{51112FBC-8B83-46DC-9C9E-F97837120D06}']
{Methods}
procedure onConnected(Param0: JSwipeEvent); cdecl;
procedure onDisconnected(Param0: JSwipeEvent); cdecl;
procedure onICDetected(Param0: JSwipeEvent); cdecl;
procedure onParseData(Param0: JSwipeEvent); cdecl;
procedure onReadData(Param0: JSwipeEvent); cdecl;
procedure onStarted(Param0: JSwipeEvent); cdecl;
procedure onStopped(Param0: JSwipeEvent); cdecl;
end;

TJSwipeListener = class(TJavaGenericImport<JSwipeListenerClass, JSwipeListener>)
end;

implementation

end.

unit com.imagpay.SwipeEvent;

interface

uses
AndroidAPI.JNIBridge,
Androidapi.JNI.JavaTypes;

type
{Class forward declarations}
JSwipeEvent = interface; {com/imagpay/SwipeEvent}

JSwipeEventClass = interface(JObjectClass)
['{6C6C07D9-F191-47A5-AFF0-E0EEE9CD8903}']
{Property methods}
function _GetTYPE_CONNECTED: Integer;
procedure _SetTYPE_CONNECTED(Value: Integer);
function _GetTYPE_DISCONNECTED: Integer;
procedure _SetTYPE_DISCONNECTED(Value: Integer);
function _GetTYPE_IC_INSERTED: Integer;
procedure _SetTYPE_IC_INSERTED(Value: Integer);
function _GetTYPE_IC_REMOVED: Integer;
procedure _SetTYPE_IC_REMOVED(Value: Integer);
function _GetTYPE_PARSEDATA: Integer;
procedure _SetTYPE_PARSEDATA(Value: Integer);
function _GetTYPE_READDATA: Integer;
procedure _SetTYPE_READDATA(Value: Integer);
function _GetTYPE_STARTED: Integer;
procedure _SetTYPE_STARTED(Value: Integer);
function _GetTYPE_STOPPED: Integer;
procedure _SetTYPE_STOPPED(Value: Integer);
{Methods}
function init(Param0: JObject; Param1: Integer; Param2: JString): JSwipeEvent; cdecl;
{Properties}
property TYPE_CONNECTED: Integer read _GetTYPE_CONNECTED write _SetTYPE_CONNECTED;
property TYPE_DISCONNECTED: Integer read _GetTYPE_DISCONNECTED write _SetTYPE_DISCONNECTED;
property TYPE_IC_INSERTED: Integer read _GetTYPE_IC_INSERTED write _SetTYPE_IC_INSERTED;
property TYPE_IC_REMOVED: Integer read _GetTYPE_IC_REMOVED write _SetTYPE_IC_REMOVED;
property TYPE_PARSEDATA: Integer read _GetTYPE_PARSEDATA write _SetTYPE_PARSEDATA;
property TYPE_READDATA: Integer read _GetTYPE_READDATA write _SetTYPE_READDATA;
property TYPE_STARTED: Integer read _GetTYPE_STARTED write _SetTYPE_STARTED;
property TYPE_STOPPED: Integer read _GetTYPE_STOPPED write _SetTYPE_STOPPED;
end;

[JavaSignature('com/imagpay/SwipeEvent')]
JSwipeEvent = interface(JObject)
['{BD54986A-FE87-416E-9ED2-468DBE5B2827}']
{Methods}
function getSource: JObject; cdecl;
function getType: Integer; cdecl;
function getValue: JString; cdecl;
end;

TJSwipeEvent = class(TJavaGenericImport<JSwipeEventClass, JSwipeEvent>)
end;

const
TYPE_DISCONNECTED = 1;
TYPE_CONNECTED = 2;
TYPE_STARTED = 3;
TYPE_STOPPED = 4;
TYPE_READDATA = 5;
TYPE_PARSEDATA = 6;
TYPE_IC_INSERTED = 7;
TYPE_IC_REMOVED = 8;

implementation

end.

This is how it implement in java:
_handler = new SwipeHandler(this);
// only read magnetic card
_handler.setReadonly(true);
_msg = new MessageHandler((TextView) findViewById(R.id.status));
_ui = new Handler(Looper.myLooper());
_handler.addSwipeListener(new SwipeListener() {
@Override
public void onReadData(SwipeEvent event) {
}

@Override
public void onParseData(SwipeEvent event) {
if (_testFlag)
return;
String result = event.getValue();
Log.d(TAG, result);
// hex string message
sendMessage("Final(16)=>% " + result);

String[] tmps = event.getValue().split(" ");
StringBuffer sbf = new StringBuffer();
for (String str : tmps) {
sbf.append((char) Integer.parseInt(str, 16));
sbf.append(" ");
}
// char message: b{card number}^{card holder}^{exp date}{other track1 data}?;{track2 data}
// or b{card number}&{card holder}&{exp date}{other track1 data}?;{track2 data}
final String data = sbf.toString().replaceAll(" ", "");
int idx = data.indexOf("^");
// plain text of card data
if (data.toUpperCase().startsWith("B") && idx > 0 && data.indexOf("?") > 0) {
sendMessage("Final(10)=>% " + data);
_ui.post(new Runnable() {
@Override
public void run() {
int idx = data.indexOf("^");
String cardNo = data.substring(1, idx);
String cardHolder = "";
String expDate = "";
int idx1 = data.indexOf("^", idx + 1);
if (idx1 > 0 && idx1 < data.length() - 4) {
cardHolder = data.substring(idx + 1, idx1);
expDate = data.substring(idx1 + 1, idx1 + 1 + 4);
}

EditText et = (EditText) findViewById(R.id.cardno);
et.setText(cardNo);
et = (EditText) findViewById(R.id.holder);
et.setText(cardHolder);
et = (EditText) findViewById(R.id.expdate);
et.setText(expDate);
}
});
}
// encryption data of card data
else if (data.length() > 20 + 5 + 4) {
_ui.post(new Runnable() {
@Override
public void run() {
EditText et = (EditText) findViewById(R.id.cardno);
et.setText("****************");
et = (EditText) findViewById(R.id.holder);
et.setText("********");
et = (EditText) findViewById(R.id.expdate);
et.setText("****");
}
});
} else {
_ui.post(new Runnable() {
@Override
public void run() {
EditText et = (EditText) findViewById(R.id.cardno);
et.setText("");
et = (EditText) findViewById(R.id.holder);
et.setText("");
et = (EditText) findViewById(R.id.expdate);
et.setText("");
}
});
}
}

@Override
public void onDisconnected(SwipeEvent event) {
sendMessage("Device is disconnected!");
toggleConnectStatus();
}

@Override
public void onConnected(SwipeEvent event) {
sendMessage("Device is connected!");
checkDevice();
}

@Override
public void onStarted(SwipeEvent event) {
if (_testFlag)
return;
sendMessage("Device is started");
toggleConnectStatus();
}

@Override
public void onStopped(SwipeEvent event) {
if (_testFlag)
return;
sendMessage("Device is stopped");
toggleConnectStatus();
}

@Override
public void onICDetected(SwipeEvent arg0) {
// TODO Auto-generated method stub

}
});
JSwipeHandler has addSwipeListener(Param0: JSwipeListener) which initialize the SDK, before it JSwipeListener event must be set which is type of JSwipeEvent.

I did with Delphi as following:
TSwipeEvent = procedure(Event: JSwipeEvent) of object;

TListener = class(TJavaLocal, JSwipeListener)
private
FOwner: TSwipeListner;
public
constructor Create(AOwner: TSwipeListner);
procedure onConnected(Param0: JSwipeEvent); cdecl;
procedure onDisconnected(Param0: JSwipeEvent); cdecl;
procedure onICDetected(Param0: JSwipeEvent); cdecl;
procedure onParseData(Param0: JSwipeEvent); cdecl;
procedure onReadData(Param0: JSwipeEvent); cdecl;
procedure onStarted(Param0: JSwipeEvent); cdecl;
procedure onStopped(Param0: JSwipeEvent); cdecl;
end;

{ TListener }

constructor TListener.Create(AOwner: TSwipeListner);
begin
inherited Create;
FOwner := AOwner;
end;

procedure TListener.onConnected(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onConnected) then
FOwner.onConnected(Param0);
end;

procedure TListener.onDisconnected(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onDisconnected) then
FOwner.onDisconnected(Param0);
end;

procedure TListener.onICDetected(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onICDetected) then
FOwner.onICDetected(Param0);
end;

procedure TListener.onParseData(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onParseData) then
FOwner.onParseData(Param0);
end;

procedure TListener.onReadData(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onReadData) then
FOwner.onReadData(Param0);
end;

procedure TListener.onStarted(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onStarted) then
FOwner.onStarted(Param0);
end;

procedure TListener.onStopped(Param0: JSwipeEvent);
begin
if Assigned(FOwner.onStopped) then
FOwner.onStopped(Param0);
end;

and on TSwipeListner I did as following:
TSwipeListner = class
private
fSwipeListner: JSwipeListener;
fonReadData: TSwipeEvent;
fonParseData: TSwipeEvent;
fonConnected: TSwipeEvent;
fonStopped: TSwipeEvent;
fonDisconnected: TSwipeEvent;
fonStarted: TSwipeEvent;
fonICDetected: TSwipeEvent;

protected
public
constructor Create;
destructor Destroy; override;
procedure RegisterListner;
property onConnected: TSwipeEvent read fonConnected write fonConnected;
property onDisconnected: TSwipeEvent read fonDisconnected
write fonDisconnected;
property onICDetected: TSwipeEvent read fonICDetected write fonICDetected;
property onParseData: TSwipeEvent read fonParseData write fonParseData;
property onReadData: TSwipeEvent read fonReadData write fonReadData;
property onStarted: TSwipeEvent read fonStarted write fonStarted;
property onStopped: TSwipeEvent read fonStopped write fonStopped;

property SwipeListner: JSwipeListener read fSwipeListner
write fSwipeListner;
end;
procedure TSwipeListner.RegisterListner;
begin
fSwipeListner := TJSwipeListener.Wrap((TListener.Create(Self) as ILocalObject).GetObjectID);
end;
which doesn't initialize the event.

for JSwipeHandler I did as following:
I made a class named TSwipeHandler with all the methods and propery of JSwipeHandler
and did as following:
handler := TSwipeHandler.Create;
listner := TSwipeListner.Create;

listner.onReadData := SwipeListnerDoReadData; //this declare in a form with just adding event's output into TMemo
listner.onParseData := SwipeListnerDoParseData; //this declare in a form with just adding event's output into TMemo
listner.onDisconnected := SwipeListnerDoDisconnected; //this declare in a form with just adding event's output into TMemo
listner.onConnected := SwipeListnerDoConnected; //this declare in a form with just adding event's output into TMemo
listner.onStarted := SwipeListnerDoStarted; //this declare in a form with just adding event's output into TMemo
listner.onStopped := SwipeListnerDoStopped; //this declare in a form with just adding event's output into TMemo
listner.onICDetected := SwipeListnerDoICDetected; //this declare in a form with just adding event's output into TMemo
listner.RegisterListner;
handler.addSwipeListener(listner.SwipeListner);

JSwipeListener is a abstract interface which is as following:
package com.imagpay;

public abstract interface SwipeListener
{
public abstract void onDisconnected(SwipeEvent paramSwipeEvent);
public abstract void onConnected(SwipeEvent paramSwipeEvent);
public abstract void onStarted(SwipeEvent paramSwipeEvent);
public abstract void onStopped(SwipeEvent paramSwipeEvent);
public abstract void onReadData(SwipeEvent paramSwipeEvent);
public abstract void onParseData(SwipeEvent paramSwipeEvent);
public abstract void onICDetected(SwipeEvent paramSwipeEvent);
}
JSwipeHandler and JSwipeEvent java class.

How can I handle JSwipeListener events?

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02