通过USB从Windows PC与android设备通信

通过USB从Windows PC与android设备通信,android,windows,usb,drivers,Android,Windows,Usb,Drivers,我有android设备和windows驱动程序。 我必须在附件模式下使用自己的二进制协议进行通信。 是否可以在不编写自定义USB驱动程序的情况下将数据发送到此设备?USB是一个复杂的协议,因为它是“通用”的,可以处理多种设备(大容量存储、摄像头等)。因此,在android设备上,必须实现一个基本的USB结构,才能使用libusb之类的标准库。 您可以假设情况就是这样,因为在大多数情况下,android是在带有USB端口的处理器上使用的 看 通过使用libusb-v 然后,如果您知道android

我有android设备和windows驱动程序。 我必须在附件模式下使用自己的二进制协议进行通信。
是否可以在不编写自定义USB驱动程序的情况下将数据发送到此设备?

USB是一个复杂的协议,因为它是“通用”的,可以处理多种设备(大容量存储、摄像头等)。因此,在android设备上,必须实现一个基本的USB结构,才能使用libusb之类的标准库。 您可以假设情况就是这样,因为在大多数情况下,android是在带有USB端口的处理器上使用的

通过使用
libusb-v

然后,如果您知道android设备上的USB结构是什么样子的,您可以使用libusb或其他库将数据写入USB结构中的特定端点,例如,
libusb\u bulk\u transfer()
libusb\u control\u transfer()

如果你想用java编程,你可以使用usb4java,但是这有点古怪

通用USB结构是设备->(配置)->接口->端点

因此,将数据写入端点的方法是:

libusb_init(NULL); 
libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);
libusb_claim_interface(devh, 0);
libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT), data, 
        4, &p, 0); //example
... release interface...
...
libusb_close(devh);
libusb_exit(NULL);

这里是从

复制的C++示例
#包括
#包括
使用名称空间std;
int main(){
libusb_设备**devs;//指向设备指针的指针,用于
检索设备列表
libusb\u device\u handle*dev\u handle;//设备句柄
libusb_context*ctx=NULL;//一个libusb会话
int r;//用于返回值
ssize_t cnt;//保存列表中的设备数
r=libusb_init(&ctx);//为我们所执行的会话初始化库
刚刚宣布
if(r<0){
库特
#include <iostream>
#include <libusb.h>

using namespace std;

int main() {
    libusb_device **devs; //pointer to pointer of device, used to      
                            retrieve a list of devices
    libusb_device_handle *dev_handle; //a device handle
    libusb_context *ctx = NULL; //a libusb session

    int r; //for return values
    ssize_t cnt; //holding number of devices in list
    r = libusb_init(&ctx); //initialize the library for the session we 
                             just declared
    if(r < 0) {
        cout<<"Init Error "<<r<<endl; //there was an error
        return 1;
    }
    libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in 
                                the documentation

    cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
    if(cnt < 0) {
        cout<<"Get Device Error"<<endl; //there was an error
        return 1;
    }
    cout<<cnt<<" Devices in list."<<endl;

    dev_handle = libusb_open_device_with_vid_pid(ctx, 5118, 7424);  
              //these are vendorID and productID I found for my usb device

    if(dev_handle == NULL)
        cout<<"Cannot open device"<<endl;
    else
        cout<<"Device Opened"<<endl;
    libusb_free_device_list(devs, 1); //free the list, unref the devices 
                                         in it

    unsigned char *data = new unsigned char[4]; //data to write

    data[0]='a';data[1]='b';data[2]='c';data[3]='d'; //some dummy values

    int actual; //used to find out how many bytes were written
    if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if 
                                               kernel driver is attached
        cout<<"Kernel Driver Active"<<endl;
        if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
            cout<<"Kernel Driver Detached!"<<endl;
    }
    r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the   
    first) of device (mine had jsut 1)
    if(r < 0) {
        cout<<"Cannot Claim Interface"<<endl;
        return 1;
    }
    cout<<"Claimed Interface"<<endl;

    cout<<"Data->"<<data<<"<-"<<endl; //just to see the data we want to 
    write : abcd
    cout<<"Writing Data..."<<endl;
    r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 
    4, &actual, 0); //my device's out endpoint was 2, found with trial- 
    the device had 2 endpoints: 2 and 129
    if(r == 0 && actual == 4) //we wrote the 4 bytes successfully
        cout<<"Writing Successful!"<<endl;
    else
        cout<<"Write Error"<<endl;

    r = libusb_release_interface(dev_handle, 0); //release the claimed 
                                                    interface
    if(r!=0) {
        cout<<"Cannot Release Interface"<<endl;
        return 1;
    }
    cout<<"Released Interface"<<endl;    

    libusb_close(dev_handle); //close the device we opened
    libusb_exit(ctx); //needs to be called to end the
    delete[] data; //delete the allocated memory for data
    return 0;
}