Java Question Hopeless? Need wrapper help with Events and returning Values

TheMightySwe

Active Member
Licensed User
Longtime User
Hi

I'm so lost, i have for several weeks now tried to make wrapper for a connection to a service that connects to a creditcard terminal.

So far it manage to get a connection, but i wan't to raise a event when that happens in the B4A application.

I have no idea what i'm doing wrong. I'm pretty shitty at programming i Java as you can see.

So I just throw out part of the code here hoping, and to see if anyone can point me in the right direction.

B4X:
@Author("IQP Design AB")
@Events(values={"onCompanionConnected",
        "onCompanionDisconnected",
        "onLog(Text As String)",
        "onError(Text As String)",
        "onWarning(Text As String)",
        "onStatusText(Text As String)",
        "onResultText(Text As String)",
        "onResultTerminalInfo(Info As String)",
        "onTransactionComplete(Amount as String, CurrencyCode as String, TerminalNumber as String, UserData as String, FFU as String)",
        "onTransactionCompleteEx(Amount as String, CurrencyCode as String, TerminalNumber as String, UserData as String, FFU as String, ExtDataOut as String)",
        "onTransactionError(ErrorCode As String)",
        "onCompanionBarcodeRead(Barcode As String)",
        "doSignatureCapture()",
        "onSignatureCaptureTimeout()",
        "doPrinterStartReceipt(Type As String)",
        "doPrinterEndReceipt()",
        "doPrinterFeedPaper(Lines As Int)",
        "doPrinterCutPaper()",
        "doPrinterPrintLinedoPrinterPrintLine(Text As String, Font As Byte, Justification As Byte, xFactor As Byte, yFactor As Byte, Underline As Byte, Bold As Byte)",
        "doPrinterLogotype()",
        "doPrinterAddSignature(Signature As Bitmap)"})
@ShortName("ISMPCompanion") // this is the name as it appear on the IDE
@Permissions(values = {"android.permission.ACCESS_NETWORK_STATE",
        "android.permission.BLUETOOTH",
        "android.permission.BLUETOOTH_ADMIN",
        "android.permission.ACCESS_WIFI_STATE",
        "android.permission.READ_PHONE_STATE"
})
@Version(0.24f)
@DependsOn(values = {"PclServiceLib","android-support-v4"})
public class ISMPWrapper
{
    private Application CheckOutPlus = null;
    protected PclService mPclService = null;
    protected PclServiceConnection mServiceConnection;
    protected int mReleaseService;
    protected boolean mBound = false;
   
    private static Boolean m_BarCodeActivated = false;
    private static Boolean m_PrinterActivated = false;
    private BarCodeReceiver m_BarCodeReceiver = null;
    private StateReceiver m_StateReceiver = null;

    private NetworkTask mNetworkTask;
   
    private static Bitmap mLastSignature = null;

    public BA Basic4Android;
    // Store B4A EventName
    private String EventName_;
   
    int SN, PN;
   
    private class _SYSTEMTIME
    {
        // WORD = UInt16
        public short wYear;
        public short wMonth;
        public short wDayOfWeek;
        public short wDay;
        public short wHour;
        public short wMinute;
        public short wSecond;
        public short wMilliseconds;
    }
    protected _SYSTEMTIME sysTime;
   
   
    /*
    *    Initialize Companion inside Basic4Android Wrapper
    */
    public void Initialize(final BA ba, String EventName)
    {
        Basic4Android = ba;
        EventName_ = EventName;
        initService(ba);
    }
   
    // You can call this method in onCreate for instance
   
    private void initService(final BA ba)
    {
        mServiceConnection = new PclServiceConnection();
        Intent i = new Intent();
        i.setClassName("com.ingenico.pclservice","com.ingenico.pclservice.PclService");
        ba.context.bindService(i, mServiceConnection,Context.BIND_AUTO_CREATE);
    }
    /*
    *    Check if Basic4Android is initialized
    */
    public Boolean IsInitialized()
    {
        if (Basic4Android == null)
        {
            BA.Log("ISMPWrapper: IsInitialized=false");
            return false;
        }
        else
        {
            BA.Log("ISMPWrapper: IsInitialized=true");
            return true;   
        }

    }
   
    // You can call this method in onDestroy for instance
    private void releaseService()
    {
        Basic4Android.context.unbindService(mServiceConnection);
        //getApplicationContext().unbindService( this.mServiceConnection);
    }
   
// Implement ServiceConnection
    public class PclServiceConnection implements ServiceConnection
    {
        @Override
        public void onServiceConnected(ComponentName className, IBinder boundService )
        {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) boundService;
            mPclService = (PclService) binder.getService();
           
            BA.Log("ISMPWrapper: binder.getService()");
            if( mPclService != null )
            {
                initStateReceiver();
                initBarCodeReceiver();
                mPclService.registerCallback(mCallback);
               
                BA.Log("ISMPWrapper: mPclService != null");
                try
                {
                    mPclService.addDynamicBridge(443, 1); // Host SSL
                    mPclService.addDynamicBridge(9034, 1); // Host
                    mPclService.addDynamicBridge(9046, 1); // TMS update service
                    mPclService.addDynamicBridge(9039, 1); // WyWallet/NFC Host connection
                    mPclService.addDynamicBridge(9599, 0);
                }
                catch(Exception ex )
                {
                    BA.Log(ex.getMessage());
                }
               
               
                BA.Log("ISMPWrapper: registerCallback()");
               
                if (Basic4Android.subExists(EventName_ + "_onCompanionConnected"))
                {
                    Basic4Android.raiseEvent(CheckOutPlus.getClass(), EventName_ + "_onCompanionConnected");
                }
            }
        }
       
        @Override
        public void onServiceDisconnected(ComponentName className)
        {
            BA.Log("ISMPWrapper: onServiceDisconnected()");

            mPclService.unregisterCallback(mCallback);
            releaseStateReceiver();
            releaseBarCodeReceiver();
            releaseService();

            mPclService = null;
            if (Basic4Android.subExists(EventName_ + "_onCompanionDisconnected"))
            {
                Basic4Android.raiseEvent(CheckOutPlus.getClass(), EventName_ + "_onCompanionDisconnected");
            }
        }
    };
   
    // Initialize broadcast receiver
    // This can be called in onResume for instance
    private void initStateReceiver()
    {
        BA.Log("ISMPWrapper: initStateReceiver()");
        if(m_StateReceiver == null)
        {
            m_StateReceiver = new StateReceiver();
            IntentFilter intentfilter = new IntentFilter("com.ingenico.pclservice.intent.action.STATE_CHANGED");
            CheckOutPlus.registerReceiver(m_StateReceiver, intentfilter);
        }
    }

    // Deinitialize broadcast receiver
    // This can be called in onPause for instance
    private void releaseStateReceiver()
    {
        BA.Log("ISMPWrapper: releaseStateReceiver()");
        if(m_StateReceiver != null)
        {
            CheckOutPlus.unregisterReceiver(m_StateReceiver);
            m_StateReceiver = null;
        }
    }
   
    private class StateReceiver extends BroadcastReceiver
    {
        @SuppressLint("UseValueOf")
        public void onReceive(Context context, Intent intent)
        {
            String state = intent.getStringExtra("state");
            BA.Log("ISMPWrapper: StateReceiver.onReceive(state=" + state + ")");
            if (state == "CONNECTED")
            {
                if (Basic4Android.subExists(EventName_ + "_onCompanionConnected"))
                {
                    Basic4Android.raiseEvent(CheckOutPlus, EventName_ + "_onCompanionConnected",(Object)null);
                }
            }
            else
            {
                if (Basic4Android.subExists(EventName_ + "_onCompanionDisconnected"))
                {
                    Basic4Android.raiseEvent(CheckOutPlus, EventName_ + "_onCompanionDisconnected",(Object)null);
                }
            }
            //"CONNECTED" or "DISCONNECTED"

        // Do appropriate action depending on state
        }
    }

    /** BarCode */
    private void initBarCodeReceiver()
    {
        BA.Log("ISMPWrapper: initBarCodeReceiver");
        if(m_BarCodeReceiver == null)
        {
            m_BarCodeReceiver = new BarCodeReceiver(this);
            IntentFilter intentfilter = new IntentFilter("com.ingenico.pclservice.action.BARCODE_EVENT");
            CheckOutPlus.registerReceiver(m_BarCodeReceiver, intentfilter);
        }
    }
   
    private void releaseBarCodeReceiver()
    {
        BA.Log("ISMPWrapper: releaseBarCodeReceiver");
        if(m_BarCodeReceiver != null)
        {
            CheckOutPlus.unregisterReceiver(m_BarCodeReceiver);
            m_BarCodeReceiver = null;
        }
    }
   
    public void onBarCodeReceived(String barCodeStr)
    {
        BA.Log("ISMPWrapper: onBarCodeReceived(" + barCodeStr + ")");
        if (Basic4Android.subExists(EventName_ + "_onCompanionBarcodeRead"))
        {
            Basic4Android.raiseEvent(this, EventName_ + "_onCompanionBarcodeRead",(Object)barCodeStr);
        }
    }
    private class BarCodeReceiver extends BroadcastReceiver
    {

        private ISMPWrapper ViewOwner = null;
        @SuppressLint("UseValueOf")
        public void onReceive(Context context, Intent intent)
        {
            BA.Log("ISMPWrapper: BarCodeReceiver.onReceive()");
            byte abyte0[] = intent.getByteArrayExtra("barcode");
            String BarCodeStr = new String(abyte0);
           
            ViewOwner.onBarCodeReceived(BarCodeStr);
        }

        BarCodeReceiver(ISMPWrapper receiver)
        {
            super();
            ViewOwner = receiver;
        }
    }
   
 

    public Boolean IsConnected()
    {
            BA.Log("ISMPWrapper: IsInitialized");
            return isCompanionConnected();
    }
}
 

stevel05

Expert
Licensed User
Longtime User
With just a quick look and not being able to try it, one problem is that the EventName and subroutine names need to be lower case.

You can enforce the EventName in the initialize method by doing:

B4X:
EventName_ = EventName.toLowerCase();

The test and call must be lower case to so:

B4X:
if (Basic4Android.subExists(EventName_ + "_onCompanionBarcodeRead"))
{
 Basic4Android.raiseEvent(CheckOutPlus, EventName_ + "_onCompanionBarcodeRead",(Object)null);
 }

becomes:

B4X:
if (Basic4Android.subExists(EventName_ + "_oncompanionbarcoderead"))
{
 Basic4Android.raiseEvent(CheckOutPlus, EventName_ + "_oncompanionbarcoderead",(Object)null);
 }

etc

regardless of the case in the B4a app.
 

TheMightySwe

Active Member
Licensed User
Longtime User
Still no luck with it.

On the B4A side the code looks like this an is in the Main Activity.

Do I need some referens to that?

B4X:
Sub Companion_oncompanionconnected

    If Debug = True Then Log("Companion_onCompanionConnected()")
    CompanionConnected = True
    ToastMessageShow("iSMP Companion Kortterminal ansluten",False)
  
End Sub

Sub Companion_oncompaniondisconnected

    If Debug = True Then Log("Companion_onCompanionDisconnected()")
    CompanionConnected = False
    ToastMessageShow("iSMP Companion Kortterminal bortkopplad",False)

End Sub
 
Top