我在使用 jni 的 android 应用程序中使用 libusb。我可以使用 libusb 找到设备。但无法打开它。
TestExP.c
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <libusb.h>
#include <jni.h>
#define MY_VENDOR_ID 0xXXXX
#define MY_PRODUCT_ID 0xXXXX
int is_usbdevblock( libusb_device *dev )
{
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor( dev, &desc );
if( desc.idVendor == MY_VENDOR_ID && desc.idProduct == MY_PRODUCT_ID ){
return 1;
}
return 0;
}
char* mymethod()
{
libusb_device **list;
libusb_device *found = NULL;
libusb_context *ctx = NULL;
int attached = 0;
if(libusb_init(&ctx)!=0)
return "libusb init failed";
libusb_set_debug(ctx,3);
ssize_t cnt = libusb_get_device_list(ctx, &list);
ssize_t i = 0;
int err = 0;
if (cnt < 0)
return "No usb device on system";
for(i = 0; i < cnt; i++){
libusb_device *device = list[i];
if( is_usbdevblock(device) ){
found = device;
break;
}
}
if(found){
libusb_device_handle *handle;
err = libusb_open(found, &handle);
if (err)
return "Open error";
return "Device Opened";
}
return "Device not found";
}
JNIEXPORT jstring JNICALL Java_com_example_test_Test_TestOpen(JNIEnv* env, jobject obj)
{
return mymethod();
}
安卓.mk
include $(CLEAR_VARS)
LOCAL_MODULE := libusb-1.0
LOCAL_SRC_FILES := libusb-1.0.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := TestExP
LOCAL_SRC_FILES := TestExP.c
LOCAL_SHARED_LIBRARIES += usb-1.0
include $(BUILD_SHARED_LIBRARY)
在我的活动课上,我遵循...
测试.java
static{
System.loadLibrary("usb-1.0");
System.loadLibrary("TestExP");
}
public native String TestOpen();
一切运行良好。当我运行这个程序时,它返回 -3 libusb 错误代码,上面写着“ LIBUSB_ERROR_ACCESS ”我做错了什么。有人说授予 USB 设备的权限。我可以使用 Android USB-API 打开设备并连接到设备。但我想用 libusb-1.0 做到这一点。