Search code examples
javaandroidusbusb-hostcontroller

Android to android data transfer using usb


I have created an application which transfer data from android to android over wifi. I am exploring usb host apis to add support for data transfer over usb. I am following android docs for host apis

https://developer.android.com/guide/topics/connectivity/usb/host

and followed all the steps. So far I am able to grant permission to the selected device lets say phone "A" and opened usb device successfully to write data , but i don't know how to create/access usbdevice on other phone "B"? I have seen other phone isn't notified about the usb attached/unattached events. When I added "android.hardware.usb.action.USB_STATE" other side is notified by event change but I don't know how to get/access usbdevice object on phone "B" to initialize read/write data? Does the current host protocol is one sided protocol? Any help in this regard will be highly appreciated.


Solution

  • After reading further I was able to identify the problem. I had to force accessory mode in device from host, by sending special controlTransfer messages.

    I found it here

    https://github.com/peyo-hd/TcpDisplay/blob/master/sink/src/com/android/accessorydisplay/sink/SinkActivity.java

    sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MANUFACTURER, MANUFACTURER);
    sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MODEL, MODEL);
    sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_DESCRIPTION, DESCRIPTION);
    sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_VERSION, VERSION);
    sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_URI, URI);
    sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_SERIAL, SERIAL);
    
    // The device should re-enumerate as an accessory.
    conn.controlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR,
        UsbAccessoryConstants.ACCESSORY_START, 0, 0, null, 0, 10000);