File I/O on a mounted USB storage device in USB Host mode (Android 3.1 and up)

后端 未结 2 596
天涯浪人
天涯浪人 2020-12-09 12:48

Ok, so I have a Android 3.1 tablet (Acer Iconia Tab, which is great by the way) which I can use with Android USB API to connect with a USB Mass Storage Device (a simple USB

2条回答
  •  一向
    一向 (楼主)
    2020-12-09 13:13

    Your connection is set up, the end points are basically flags on the device with information on data transfer.

    For your stick you need to do something like VV to figure out how many endpoints you have,

    UsbInterface intf = device.getInterface(0);
    // checks for 2 endpoints
    if (intf.getEndpointCount() != 2) {
    Toast toast = Toast.makeText(context, "could not find endpoint", duration);
    toast.show();
    return;
        }
    UsbEndpoint ep = intf.getEndpoint(0);
    if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
    if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
    

    this will let you figure out if the endpoint you're interested in is a bulk (usb constants docs has other types) and if you can send data to or from the device at that endpoint (usb_dir_in to test for in). What endpoint you want is device specific, my example starts on 0, yours will be different

    To resave the file you need to do something like

    mConnection.bulkTransfer(mEndpointOut, bytes, 512, TIMEOUT);
    

    I have been saving the buffer each time it fills with a file output stream, this is probably inefficient (as I assume bulktransfer is already saving somewhere) but documentation is scarce.

提交回复
热议问题