Android Host API与设备通信

时间:2012-10-08 13:48:09

标签: android usb

我需要使用Android主机API与USB通信设备进行通信

这是我正在使用的设备的信息。

lsusb -v -d PID:VID

总线003设备007:ID PIDVID VOTI

设备描述符:

bLength                18
bDescriptorType         1
bcdUSB               1.01
bDeviceClass            2 Communications
bDeviceSubClass         0 
bDeviceProtocol         0 
bMaxPacketSize0         8
idVendor              PID VOTI
idProduct             VID 
bcdDevice            1.00
iManufacturer           1 
iProduct                2 
iSerial                 0 
bNumConfigurations      1
Configuration Descriptor:
bLength                 9
bDescriptorType         2
wTotalLength           67
bNumInterfaces          2
bConfigurationValue     1
iConfiguration          0 
bmAttributes         0x80
(Bus Powered)
MaxPower              100mA

接口描述符:

  bLength                 9
  bDescriptorType         4
  bInterfaceNumber        0
  bAlternateSetting       0
  bNumEndpoints           1
  bInterfaceClass         2 Communications
  bInterfaceSubClass      2 Abstract (modem)
  bInterfaceProtocol      1 AT-commands (v.25ter)
  iInterface              0 
  CDC Header:
    bcdCDC               1.10
  CDC ACM:
    bmCapabilities       0x02
      line coding and serial state
  CDC Union:
    bMasterInterface        0
    bSlaveInterface         1 
  CDC Call Management:
    bmCapabilities       0x03
      call management
      use DataInterface
    bDataInterface          1

端点描述符:

    bLength                 7
    bDescriptorType         5
    bEndpointAddress     0x83  EP 3 IN
    bmAttributes            3
      Transfer Type            Interrupt
      Synch Type               None
      Usage Type               Data
    wMaxPacketSize     0x0008  1x 8 bytes
    bInterval             100

接口描述符:

  bLength                 9
  bDescriptorType         4
  bInterfaceNumber        1
  bAlternateSetting       0
  bNumEndpoints           2
  bInterfaceClass        10 CDC Data
  bInterfaceSubClass      0 Unused
  bInterfaceProtocol      0 
  iInterface              0 

端点描述符:

    bLength                 7
    bDescriptorType         5
    bEndpointAddress     0x01  EP 1 OUT
    bmAttributes            2
      Transfer Type            Bulk
      Synch Type               None
      Usage Type               Data
    wMaxPacketSize     0x0006  1x 6 bytes
    bInterval               0

端点描述符:

    bLength                 7
    bDescriptorType         5
    bEndpointAddress     0x81  EP 1 IN
    bmAttributes            2
      Transfer Type            Bulk
      Synch Type               None
      Usage Type               Data
    wMaxPacketSize     0x0008  1x 8 bytes
    bInterval               0

现在我可以发现设备并获得与设备通信的权限。 但是当我尝试使用bulkTransfer()函数与设备通信时,它总是返回-1。

这是代码

UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);

conn.controlTransfer(33, 34, 0, 0, null, 0, 0);

byte[] buffer = new byte[]{ (byte) 0x80,0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };

conn.controlTransfer(33, 32, 0, 0, buffer , 7, 0);  //8N1, 9600 baud

conn.bulkTransfer(epOUT, new byte[]{msData}, 1, 0);

UsbEndpoint epIN = null;

UsbEndpoint epOUT = null;

UsbInterface usbIf = mDevice.getInterface(1);

for (int i = 0; i < usbIf.getEndpointCount(); i++) {

if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {

    if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)

        epIN = usbIf.getEndpoint(i);
    else
        epOUT = usbIf.getEndpoint(i);
}
}

for (;;) {// this is the main loop for transferring

synchronized (sSendLock) {//ok there should be a OUT queue
    try {
        sSendLock.wait();
    } catch (InterruptedException e) {
        if (mStop) {
            mConnectionHandler.onUsbStopped();
            return;
        }
        e.printStackTrace();
    }
}
conn.bulkTransfer(epOUT, new byte[] { mData }, 1, 0);

    if (mStop) {
        mConnectionHandler.onUsbStopped();
        return;
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,你不需要先调用claimInterface()。 另请注意,如果由于某种原因无法创建设备,则声明接口将失败。我有问题让一个irda接口在手机上正确注册,这是由于irda的功率需求看到我的问题bacck然后] 1