Home > Enterprise >  Middleware To Access Java Callback Interface
Middleware To Access Java Callback Interface

Time:08-17

First of all i am newbie at Kotlin Language and android programming.

I am trying to develop a middleware SDK to access real SDK of a POS device. For example POS device SDK has a method like "printString()" and i am creating a method named "Print()".

People will only know the Print() method and i will access the real printString() method of device SDK.

The programming language of device SDK is Java, and my middleware SDK is Kotlin.

I actually wrote most of required methods (converted from java). But i have a one problem to create middleware of Java callback interface.

This the Java interface of device SDK

public interface CommonCB {
    int GetDateTime(byte[] var1);

    int ReadSN(byte[] var1);

    int GetUnknowTLV(int var1, byte[] var2, int var3);
}

I would like to create an interface with Kotlin named like "CommonCallback". And people can override the above methods with calling CommonCallback class or interface.

How can i do that ? i tried many time but couldnt find a solution yet.

CodePudding user response:

This is the java callback interface of device SDK and device library files are included to my SDK module.

public interface CommonCB {
    int GetDateTime(byte[] var1);

    int ReadSN(byte[] var1);

    int GetUnknowTLV(int var1, byte[] var2, int var3);
}

I want to create an interface which is inherited from CommonCB in my SDK like

interface CommonCallBack : CommonCB {
}

And third party applications will try to communicate via "CommonCallBack"

Third party app includes my module, and my SDK module includes device SDK as .jar file.

So when i try to access my SDK module like :

var ccb = object : CommonCallBack
        {
           override fun GetDateTime(bytes: ByteArray): Int {
                val DataTimeTemp = ByteArray(10)
                return 0
            }

            override fun ReadSN(bytes: ByteArray): Int {
                var sNumber = PedApi.readPinPadSerialNumber()
                if (sNumber!=null) {
                    val nLen = (sNumber[0] - 0x30) * 10   (sNumber[1] - 0x30)   2
                    if (nLen > 11) ByteUtils.memcpy(
                        bytes,
                        0,
                        sNumber,
                        2   nLen - 11,
                        11
                    ) else ByteUtils.memcpy(bytes, 0, sNumber, 2, nLen)
                }
                return CommonConstants.EmvResult.EmvOk
            }

            override fun GetUnknowTLV(i: Int, bytes: ByteArray, i1: Int): Int {
                return -1
            }
        }

Android Studio gives error like that : "Cannot access 'com.*.CommonCB' which is a supertype of 'com..sdktestapp.sdkservice.Definitions.Companion.ccb.'. Check your module classpath for missing or conflicting dependencies"

Actually i dont want to include CommonCB library to test application. Is it clear now ? thanks

Here is the basically drawing of my communication design to understand the problem better : https://i.hizliresim.com/f2jeqep.png

CodePudding user response:

It seems you should just create your own independent interface:

interface CommonCallback {
    fun getDateTime(var1 ByteArray): Int

    fun readSN(var1: ByteArray): Int

    fun getUnknowTLV(var1: Int, var2: ByteArray, var3: Int): Int
}

(of course with proper parameter names)

Then, in your own SDK code, you can pass an original callback implementation to the original device SDK methods that need it, and that implementation would simply be an adapter that delegates all the calls to the callback provided by your users:

internal class CommonCBAdapter(private val cb: CommonCallback): CommonCB {
    override fun GetDateTime(bytes: ByteArray): Int = cb.getDateTime(bytes)

    override fun ReadSN(bytes: ByteArray): Int = cb.readSN(bytes)

    override fun GetUnknowTLV(i: Int, bytes: ByteArray, i1: Int): Int = cb.getUnknownTLV(i, bytes, i1)
}

// then, somewhere else still in your own code
val userCallback: CommonCallback = TODO("the user provides you with an implementation of your callback interface")

val deviceCallback = CommonCBAdapter(userCallback)

someDeviceStuff.someDeviceMethodWithCB(deviceCallback)
  • Related