简体   繁体   English

libusb_open返回LIBUSB_ERROR_ACCESS?

[英]libusb_open returns LIBUSB_ERROR_ACCESS?

I am using libusb in my android app using jni. 我在使用jni的android应用程序中使用libusb。 I am able to find the device using libusb. 我可以使用libusb找到该设备。 but not able to open it. 但无法打开它。

TestExP.c 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();
}

Android.mk Android.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)

in my activity class I do following... 在我的活动课程中,我确实遵循...

test.java test.java

static{
    System.loadLibrary("usb-1.0");
    System.loadLibrary("TestExP");
}
 public native String TestOpen();

Everything runs fine. 一切运行正常。 when i run this program it returns -3 libusb error code which says " LIBUSB_ERROR_ACCESS " What I am doing wrong. 当我运行该程序时,它返回-3 libusb错误代码,提示“ LIBUSB_ERROR_ACCESS ”,这是我做错了。 Some say give permisions to the usb device. 有人说给USB设备带来麻烦。 I can open the device using Android USB-APIs and make a connection to device. 我可以使用Android USB-API打开设备并建立与设备的连接。 But I want to do that with libusb-1.0. 但是我想用libusb-1.0做到这一点。

Find your device and remember its VID and PID: 找到您的设备并记住其VID和PID:

lsusb

Create rules file: 创建规则文件:

sudo gedit /etc/udev/rules.d/99-my-android-device.rules

Enter next strings to file, fill 03eb and 204f with your VID and PID: 输入下一个要归档的字符串,用您的VID和PID填充03eb204f

ACTION!="add|change", GOTO="my_android_device_rules_end"
SUBSYSTEM!="usb|tty|hidraw", GOTO="my_android_device_rules_end"

ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204f", MODE="664", GROUP="plugdev"

LABEL="my_android_device_rules_end"

Add yourself to group plugdev : 将自己添加到plugdev组:

usermod -aG plugdev $USER

Relogin or restart. 重新登录或重新启动。

Also you could be interested in similar issue . 您也可能对类似问题感兴趣。

将您的用户添加到“ root”组,这将使您可以访问所有USB设备和输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM