Mudassar
Mudassar

Reputation: 1576

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.

Upvotes: 1

Views: 1921

Answers (2)

Mudassar
Mudassar

Reputation: 1576

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);

Upvotes: 3

mrwcs
mrwcs

Reputation: 41

Phone B is UsbDevice, it should use the USB Accessory function to connect with Accessory host(Phone A). Phone B selects to accessory usb gadget function, which means Phone B's sys.usb.config = accessory. [getprop sys.usb.config => accessory]

App level design refer to AOA:

https://source.android.com/devices/accessories/protocol https://developer.android.com/guide/topics/connectivity/usb/accessory.html#java

UsbAccessory accessory;
ParcelFileDescriptor fileDescriptor;
FileInputStream inputStream;
FileOutputStream outputStream;
...

private void openAccessory() {
    Log.d(TAG, "openAccessory: " + accessory);
    fileDescriptor = usbManager.openAccessory(accessory);
    if (fileDescriptor != null) {
        FileDescriptor fd = fileDescriptor.getFileDescriptor();
        inputStream = new FileInputStream(fd);
        outputStream = new FileOutputStream(fd);
        Thread thread = new Thread(null, this, "AccessoryThread");
        thread.start();
    }
}

Upvotes: 0

Related Questions