[英]USB host bulkTransfer returns valid amount of data but I don't get anything back
I have an external camera that's connected via a USB C dongle to my Android tablet.我有一个外部摄像头,它通过 USB C 加密狗连接到我的 Android 平板电脑。 My goal is to have a constant stream of data from the camera into my phone, showing it to the user and allowing him to record it and save it to the local storage.
我的目标是将摄像头中的 stream 数据持续发送到我的手机中,将其显示给用户并允许他记录并保存到本地存储。
I am following the official docs from the following link - https://developer.android.com/guide/topics/connectivity/usb/host#working-d我正在关注以下链接中的官方文档 - https://developer.android.com/guide/topics/connectivity/usb/host#working-d
And I have spent the last couple of hours trying to figure out how things work, mapping the interfaces and endpoints, eventually finding an interface that has an endpoint that when I call bulkTransfer()
on, does not return a failed value (-1).我花了最后几个小时试图弄清楚事情是如何工作的,映射接口和端点,最终找到一个接口,它有一个端点,当我调用
bulkTransfer()
时,不会返回失败值 (-1) .
I currently am facing 2 issues:我目前面临两个问题:
I have indeed got a valid response from the bulkTransfer()
function, but my ByteArray does not fill with relevant information - when trying to print out the values they are all 0's.我确实从
bulkTransfer()
function 得到了有效的响应,但是我的 ByteArray 没有填充相关信息——当试图打印出它们都是 0 的值时。 I though it may be a wrong endpoint as suggested in the official docs, but I have tried all combinations of interfaces and endpoints until I get an indexOutOfBoundException.我虽然这可能是官方文档中建议的错误端点,但我已经尝试了接口和端点的所有组合,直到我得到 indexOutOfBoundException。 That combination of interface + endpoint that I used is the only one that produced a valid bulk response.
我使用的接口 + 端点的组合是唯一产生有效批量响应的组合。 What am I missing?
我错过了什么?
I am looking for a stream of data that doesn't stop, but it seems like when calling bulkTransfer()
it's one a one time oppression, unlike CameraX library for example that I get a constant callback each time a new chunck of data is available.我正在寻找一个不会停止的 stream 数据,但在调用
bulkTransfer()
时它似乎是一次性的,不像 CameraX 库,例如每次有新的数据块可用时我都会得到一个持续的回调.
Here is the code on my main screen -这是我主屏幕上的代码 -
LaunchedEffect(key1 = true) {
val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
val filter = IntentFilter(ACTION_USB_PERMISSION)
registerReceiver(context, UsbBroadcastReceiver(), filter, RECEIVER_NOT_EXPORTED)
val hdCamera = usbManager.deviceList.values.find { device ->
val name = device.productName ?: return@LaunchedEffect
name.contains("HD camera")
} ?: return@LaunchedEffect
val permissionIntent = PendingIntent.getBroadcast(
context,
0, Intent(ACTION_USB_PERMISSION),
0
)
usbManager.requestPermission(hdCamera, permissionIntent)
}
And here is my BroadcastReceiver -这是我的 BroadcastReceiver -
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action != ACTION_USB_PERMISSION) return
synchronized(this) {
val usbManager = context?.getSystemService(Context.USB_SERVICE) as UsbManager
val device: UsbDevice? = intent.getParcelable(UsbManager.EXTRA_DEVICE)
val usbInterface = device?.getInterface(0)
val endpoint = usbInterface?.getEndpoint(1) ?: return@synchronized
usbManager.openDevice(device)?.apply {
val array = ByteArray(endpoint.maxPacketSize)
claimInterface(usbInterface, true)
val bulkTransfer = bulkTransfer(endpoint, array, array.size, 0)
Log.d("defaultAppDebuger", "bulk array: $bulkTransfer") //prints a valid number - 512
array.forEach {
Log.d("defaultAppDebuger", "bulk array: $it") //the array values are empty
}
}
}
}
edit: I have tried to move the BroadcastReceiver code to an async coroutine thinking that the loading of the information is related to the fact that I am in the wrong thread.编辑:我试图将 BroadcastReceiver 代码移动到异步协程,认为信息的加载与我在错误的线程中有关。 Still didn't work, I get a valid result from the bulkTransfer and the byteArray is not filled -
仍然没有用,我从 bulkTransfer 得到了一个有效结果并且 byteArray 没有被填充 -
fun BroadcastReceiver.goAsync(
context: CoroutineContext = Dispatchers.IO,
block: suspend CoroutineScope.() -> Unit
) {
val pendingResult = goAsync()
CoroutineScope(SupervisorJob()).launch(context) {
try {
block()
} finally {
pendingResult.finish()
}
}
}
override fun onReceive(context: Context?, intent: Intent?) = goAsync { .... }
Thanks!谢谢!
After carefully researching I was not able to get an answer and ditched that mini project that I worked on.在仔细研究后,我无法得到答案并放弃了我从事的那个迷你项目。 I followed this comment on the following thread -
我在以下线程中关注了此评论 -
https://stackoverflow.com/a/68120774/8943516 https://stackoverflow.com/a/68120774/8943516
That, combined with a 2.5 days of deep researched of both USB Host protocol which was not able to connect to my camera and Camera2API which couldn't recognize my external camera brought me to a dead end.结合 2.5 天对无法连接到我的相机的 USB 主机协议和无法识别我的外部相机的 Camera2API 的深入研究,我陷入了死胡同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.