使用NDK / JNI从Android调用带有双指针参数的C方法

时间:2013-06-06 10:41:05

标签: java android c android-ndk java-native-interface

我正在使用API​​来控制连接到Android设备的指纹扫描仪。我需要调用的本机方法需要混合使用指针和双指针才能传递给方法,但我不确定如何执行此操作。

原始C方法有以下定义:

BioErrType BioGetTemplates(BioHandle handle, char **badgeNumberList,uint8_t *templateIndexList, BioTemplate **bioTemplateList)

为调用它而提供的Java方法是:

public native int BioGetTemplates(int handle, int [] badgeNumberList, byte [] templateIndexList, byte [][]bioTemplateList);

原始C方法的文档说明:

***    Gets from the biometric device a template for each of the listed
***  badge numbers.
***  
***    The badgeNumberList pointer is a pointer to a null terminated array
***  of pointers to badge number strings.  The bioTemplateList should be an
***  array of pointers to memory where templates can be stored, one pointer
***  for each possible template returned.
***    The templateIndexList is a pointer to an array of template indices.
***  If templateIndexList is not null, than its size must be the same as the
***  number of badge number strings provided via badgeNumberList, which must
***  be the same size as the array of template pointers provided via
***  bioTemplateList. If templateIndexList is null, than the bioTemplateList
***  array size must be a device specific multiple of the number of badge
***  numbers.  For all current devices, this multiple is 2, corresponding to
***  a primary and secondary template for each badge number.
***    Each template is copied from the device to the memory at the
***  corresponding pointer in the bioTemplateList. If no template for a
***  given badge number and template index is stored in the device, the
***  corresponding pointer in the bioTemplateList is set to NULL.  If a
***  template pointer in bioTemplateList is NULL and yet a template exists
***  for the corresponding badge number and index, BIOAPI_INVALID is returned.
***    The applications program shall provide a separate buffer, of a size
***  specific to the device type, for every template it expects.
***
***  returns:
***    BIOAPI_OK if all templates found were copied to the provided template
***  buffers.
***    BIOAPI_INVALID if the handle is invalid or the badgeNumberList pointer
***  is null or if the bioTemplateList pointer is null, or if any badge
***  number in the list is invalid, or if any template index is out of range
***  for the device, or if a null template pointer is provided for a
***  template that is requested and is in the device, or if the device could
***  not successfully be talked to.  This error code can be returned even if
***  some of the templates were copied from the device, if the function was
***  not able to complete.
***    BIOAPI_BUSY if another thread is currently using the library.

我目前试图调用代码:

            int[] badgeNumberList = { 100, 0 };
            byte[] templateIndexList = new byte[2 * (badgeNumberList.length - 1)];
            byte[][] bioTemplateList = new byte[1][2];

            try {
                mBio.bioGetTemplates(badgeNumberList, templateIndexList,
                        bioTemplateList);

                byte[] byteArr = bioTemplateList[0];
                BigInteger bigTemplate = new BigInteger(byteArr);

                Log.d(TAG, CLASS + "." + METHOD + " BigInt is: "
                        + bigTemplate.toString());

            } catch (BioError e) {
                Log.w(TAG, CLASS + "." + METHOD + ": " + e.getMessage());

            }

请注意,mBio对象是提供的Java API的外观,它处理传入句柄并根据方法的整数返回引发错误。

我意识到我没有对输出做正确的事情,但到目前为止我只是想看一些输出然后我可以稍后处理它。

目前运行此代码会引发BIOAPI_INVALID错误。

我目前无法在设备上进行调试,只能写入日志。

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:0)

这只是猜测。

BIOAPI_INVALID: ...if any badge number in the list is invalid, ...

可能0无效?

如果我正在设计Java API,我会遗漏诸如" null终止数组"等概念。该服务只有弱C数据结构。因此,int [] badgeNumberList只是一个徽章编号列表。

尝试:

int[] badgeNumberList = { 100 };

您确实需要Java API的文档。

答案 1 :(得分:0)

我与那些编写API的人交谈,正确的呼吁是:

int[] badgeNumberList = new int[] { 100, 200 };
byte[] templateIndexList = new byte[] { 0, 0 };
byte[][] bioTemplateList = new byte[2][TEMPLATE_SIZE];

mBio.getTemplates(badgeNumberList, templateIndexList, bioTemplateList);