Android 附件协议速度很慢(3.7 MB/s)

问题描述 投票:0回答:1

我想通过 USB 将大图像数据从 PC 发送到我的 Android 手机。我了解了 Android 配件协议 https://developer.android.com/develop/connectivity/usb/accessory,现在可以将数据从我的 Mac 发送到我的应用程序。 该代码几乎与Android USB Accessory mode can't read/write with host PC using libusb

相同

但是,我只能获得大约 3.7 MB/s 的数据速率,这太慢了。我想使用 USB 来加快速度。

kotlin中相关的接收部分是这个:

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

有人知道为什么这么慢吗?使用 USB C,我预计至少 100 MB/s 或更高。

android kotlin usb libusb
1个回答
0
投票

我想我找到了另一种解决方法。在我的研究过程中,我遇到了https://immersed.zendesk.com/hc/en-us/articles/14823473330957-USB-C-Mode-w-Meta-Quest-BETA-

我想知道他们如何实现这一目标。我注意到,他们要求 Android 设备启用调试模式。因此,我通过adb桥寻找解决方案。 我找到了

adb forward
命令。 使用此命令并仅通过 TCP 套接字进行通信,我现在获得的传输速率约为 209 MB/s。这更有用。

亚行命令:

./adb forward tcp:3001 tcp:3001

发送图像的Python代码:

import socket
import struct

def send_bitmap(filename, server_address=('127.0.0.1', 3001)):
    try:
        # Open the bitmap file in binary mode
        with open(filename, 'rb') as file:
            # Read the binary data
            bitmap_data = file.read()

        # Get the size of the bitmap data
        file_size = len(bitmap_data)
        print(file_size)

        # Create a TCP socket
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
            # Connect to the server
            client_socket.connect(server_address)

            # Send the file size as a 4-byte integer
            client_socket.sendall(struct.pack("!I", file_size))

            # Send the bitmap data
            client_socket.sendall(bitmap_data)

        print(f"Bitmap file '{filename}' sent successfully to {server_address}")

    except Exception as e:
        print(f"Error: {e}")

# Replace 'your_bitmap_file.bmp' with the actual filename of your bitmap file
send_bitmap('../output.bmp')

接收图像的 Kotlin 代码:

private inner class ServerThread : Thread() {
    override fun run() {
        var running = true
        while (running) {
            val server = ServerSocket(3001)
            println("Server running on port ${server.localPort}")
            val client = server.accept()
            println("Client connected : ${client.inetAddress.hostAddress}")

            val inputStream = BufferedInputStream(client.getInputStream())
            try {
                // Step 1: Read the integer (4 bytes) for file length
                val lengthBytes = ByteArray(4)
                inputStream.read(lengthBytes)
                var fileLength = byteArrayToInt(lengthBytes)

                // Step 2: Read the full bitmap file using the obtained length
                val bitmapBytes = ByteArray(fileLength)

                var package_size: Int = 16384

                val timeTaken = measureTime {
                    val num_chunks = fileLength / package_size
                    var offset = 0

                    for (i: Int in 1..num_chunks) {

                        val bytes_read = inputStream.read(bitmapBytes, offset, package_size)
                        offset += bytes_read
                    }
                    val remaining = fileLength - offset
                    inputStream.read(bitmapBytes, offset, remaining)
                }

                val mb_length = fileLength/1024.0/1024
                val bps = mb_length/(timeTaken.inWholeMilliseconds*1e-3)
                val bitmap: Bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.size)
                runOnUiThread {
                    text_view.append("\nReading: ${mb_length} MB in ${timeTaken.inWholeMilliseconds} ms = ${bps} MB/s")
                    preview_image.setImageBitmap(bitmap)
                }
                
            } catch (e: Exception) {
                e.printStackTrace()
            }

            // Close resources when done
            inputStream.close()
            client.close()
            server.close()
        }
    }
}

如果您正在寻找 PC/Mac 和 Android 设备之间的快速 USB 传输,这也许会对某人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.