访问Java回调接口的中间件



首先,我是Kotlin语言和android编程的新手。

我正在尝试开发一个中间件SDK来访问POS设备的真实SDK。例如,POS设备SDK具有一种方法;printString(("并且我正在创建一个名为";打印((;。

人们将只知道Print((方法,而我将访问设备SDK的真正printString((方法。

设备SDK的编程语言是Java,我的中间件SDK是Kotlin。

实际上,我编写了大多数必需的方法(从java转换而来(。但我有一个问题,创建Java回调接口的中间件。

这是设备SDK 的Java接口

public interface CommonCB {
int GetDateTime(byte[] var1);
int ReadSN(byte[] var1);
int GetUnknowTLV(int var1, byte[] var2, int var3);
}

我想用Kotlin创建一个名为";CommonCallback";。人们可以通过调用CommonCallback类或接口来覆盖上述方法。

我该怎么做?我试了很多次,但都没找到解决办法。

您似乎应该创建自己的独立接口:

interface CommonCallback {
fun getDateTime(var1 ByteArray): Int
fun readSN(var1: ByteArray): Int
fun getUnknowTLV(var1: Int, var2: ByteArray, var3: Int): Int
}

(当然有适当的参数名称(

然后,在您自己的SDK代码中,您可以将原始回调实现传递给需要它的原始设备SDK方法,该实现将只是一个适配器,它将所有调用委托给用户提供的回调:

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)

这是设备SDK的Java回调接口,设备库文件包含在我的SDK模块中。

public interface CommonCB {
int GetDateTime(byte[] var1);
int ReadSN(byte[] var1);
int GetUnknowTLV(int var1, byte[] var2, int var3);
}

我想在我的SDK中创建一个继承自CommonCB的接口,如下所示:

interface CommonCallBack : CommonCB {
}

第三方应用程序将尝试通过CommonCallBack进行通信

第三方应用程序包括我的模块,我的SDK模块包括设备SDK作为.jar文件。

因此,当我尝试访问我的SDK模块时,如下所示:

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给出如下错误:"无法访问"com.*.CommonCB",它是"com"的超类型.sdkestapp.sdkservice.Definitions.CCompanion.ccb.'。检查模块类路径是否存在丢失或冲突的依赖项">

实际上,我不想包含CommonCB库来测试应用程序。现在清楚了吗?感谢

以下是我的通信设计的基本图,以更好地理解问题:https://i.hizliresim.com/f2jeqep.png

最新更新