简体   繁体   English

Android USB主机异步批量传输示例

[英]Example of Android USB Host Asynchronous Bulk Transfer

I am working with Android USB Host mode and would like to perform an asynchronous bulk transfer. 我正在使用Android USB主机模式,并希望执行异步批量传输。 I have so far been successfully using synchronous bulk transfers, but am having a little trouble grasping how the pieces come together for an asynchronous transfer. 到目前为止,我已经成功地使用了同步批量传输,但是在把握各个部分如何进行异步传输方面遇到了一些麻烦。 From the UsbRequest documentation (bold mine): UsbRequest文档中(粗体显示):

Requests on bulk endpoints can be sent synchronously via bulkTransfer(UsbEndpoint, byte[], int, int) or asynchronously via queue(ByteBuffer, int) and requestWait() [a UsbDeviceConnection method] . 批量端点上的请求可以通过bulkTransfer(UsbEndpoint,byte [],int,int)同步发送,也可以通过queue(ByteBuffer,int)requestWait() [UsbDeviceConnection方法]异步发送。

Ok, so does this mean I call queue() from the existing thread of execution and then requestWait() somewhere else in another thread? 好的,这是否意味着我从现有的执行线程中调用queue(),然后在另一个线程中的其他位置调用requestWait()? Where does requestWait() get my logic from to execute when the request completes? 请求完成后,requestWait()从何处执行我的逻辑? Most of the async work I have done has been in languages like Javascript and Python, generally by passing a callback function as an argument. 我所做的大多数异步工作都是使用Java语言和Python这样的语言完成的,通常是通过将回调函数作为参数来进行的。 In Java I was expected to perhaps pass an object that implements a specific method as a callback, but I can't see that happening anywhere. 在Java中,我可能希望传递一个实现特定方法的对象作为回调,但是我看不到任何地方发生这种情况。 Perhaps my mental model of the whole thing is wrong. 也许我对整个事情的思维模式是错误的。

Can someone provide an isolated example of sending an asynchronous bulk transfer? 有人可以提供发送异步批量传输的孤立示例吗?

Basically the requestWait() method is going to return once the queued UsbRequest has completed. 基本上,排队的UsbRequest完成后, requestWait()方法将返回。 You can do this on the same thread or on another. 您可以在同一线程或另一线程上执行此操作。 Use the setClientData() AND getClientData() methods to determine which request has just completed, assuming that you had more than one outstanding! 假设您有多个未完成的请求,请使用setClientData()getClientData()方法确定刚刚完成的请求!

You can queue multiple UsbRequests across multiple EndPoints and the consume their completion status by repeatedly calling requestWait() until you have no more outstanding requests. 您可以将多个UsbRequests排队在多个Endpoints上,并通过重复调用requestWait()消耗其完成状态,直到没有其他未完成的请求为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM