android读取usb设备数据

main.c

#include 
#include 
#include 
#include 
#include 

#include 

int main(int argc, char **argv)
{
        char buff[64];
        int i = 0,out =0;
        static uint8_t read_ep;

        struct usb_descriptor_header* desc;
        struct usb_descriptor_iter iter;
        struct usb_interface_descriptor *intf = NULL;
        struct usb_endpoint_descriptor *ep1 = NULL;
        struct usb_endpoint_descriptor *ep2 = NULL;

        struct usb_device *p = usb_device_open("/dev/bus/usb/001/002");

        int vendorId = usb_device_get_vendor_id(p);
        int productId = usb_device_get_product_id(p);

        printf("vid =  %x, pid = %x\n",vendorId,productId);

            usb_descriptor_iter_init(p, &iter);
            while ((desc = usb_descriptor_iter_next(&iter)) != NULL && (!intf || !ep1 || !ep2)) {
                if (desc->bDescriptorType == USB_DT_INTERFACE) {
                    intf = (struct usb_interface_descriptor *)desc;
                } else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
                    if (ep1)
                        ep2 = (struct usb_endpoint_descriptor *)desc;
                    else
                        ep1 = (struct usb_endpoint_descriptor *)desc;
                }
            }

            if (!intf) {
                printf("interface not found\n");
                exit(1);
            }
            if (!ep1 || !ep2) {
                printf("endpoints not found\n");
                exit(1);
            }

            printf("%d,%d,%d\n",ep1->bEndpointAddress,ep2->bEndpointAddress,intf->bInterfaceNumber);

            if (usb_device_claim_interface(p, intf->bInterfaceNumber)) {
                printf("usb_device_claim_interface failed errno2:\n");
                exit(1);
            }


            if ((ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
               read_ep = ep1->bEndpointAddress;
            } else {
                read_ep = ep2->bEndpointAddress;
            }


        for(i = 0;i<10;i++){
                out = usb_device_bulk_transfer(p,read_ep,buff,64,0);
                printf("out = %d\n",out);
        }

    return 0;
}

Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c
LOCAL_MODULE := main
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_LIBRARIES := \
    libusbhost \
include $(BUILD_EXECUTABLE)



你可能感兴趣的:(Android)