I want to send large image data over USB from a PC to my Android Phone. I learned about the Android accessory protocol https://developer.android.com/develop/connectivity/usb/accessory and can now send data from my Mac to my App. The code is almost identical to Android USB Accessory mode can't read/write with host PC using libusb
However, I only get data rates of about 3.7 MB/s which is much to slow. I wanted to use USB to be fast.
The relevant receiving part in kotlin is this one:
val fd: FileDescriptor = mFileDescriptor.getFileDescriptor()
mInputStream = FileInputStream(fd)
mOutputStream = FileOutputStream(fd)
val bytes_length: ByteArray = ByteArray(4)
mInputStream.read(bytes_length, 0, 4)
val length = byteArrayToUnsignedInt(bytes_length.toUByteArray()).toInt()
val bytes = ByteArray(length)
val package_size = 16384
val timeTaken = measureTime {
val num_chunks = length / package_size
var offset = 0
for (i: Int in 1..num_chunks) {
val bytes_read = mInputStream.read(bytes, offset, package_size)
offset += bytes_read
}
val remaining = length - offset
mInputStream.read(bytes, offset, remaining)
}
val mb_length = length/1024.0/1024
val bps = mb_length/(timeTaken.inWholeMilliseconds*1e-3)
text_view.append("\nReading: ${mb_length} MB in ${timeTaken.inWholeMilliseconds} ms = ${bps} MB/s")
Does anybody have a clue why it is so slow? With usb c I expected at least 100 MB/s or more.
I think I found another workaround. During my research I came across https://immersed.zendesk.com/hc/en-us/articles/14823473330957-USB-C-Mode-w-Meta-Quest-BETA-
I wondered, how they could achieve it. And I noticed, that they required that the android device has the debugging mode enabled. Therefore, I searched for a solution via the adb bridge. And I found the
adb forwardcommand. Using this command and just communicating over a TCP sockets I get now transfer rates around 209 MB/s. That's much more usable.ADB command:
Python code to send an image:
Kotlin code to receive the image:
Maybe this helps somebody if you are looking for some fast USB transfer between a PC/Mac and an android device.