我有一个 USB 设备,我正在尝试使用 MonoDroid API 与我的 Android 4.1 设备进行通信,但在设置正确连接时遇到了一些问题。首先,为达到我的“想法”而采取的步骤可能是一个问题:
- 在我的 AndroidManifest 文件中使用意图过滤器按供应商和产品 ID 过滤我的设备。这很好用,因为当我插入设备时,我的应用程序默认请求启动,因此权限应该正确。
- 从我的意图过滤器在发现所述设备后发送程序的 Activity 中获取我的 USB 设备:
UsbDevice device = (UsbDevice)this.Intent.GetParcelableExtra(UsbManager.ExtraDevice);
- 在检查只存在一个接口后,我通过发出以下命令来获取关联的接口:
UsbInterface intf = device.GetInterface(0);
- 检查端点的数量并抓取它们。有 2 个,因为这是一个输入和输出设备:
UsbEndpoint endpoint_IN = intf.GetEndpoint(0);
UsbEndpoint endpoint_OUT = intf.GetEndpoint(1);
- 使用 UsbManager 获取与设备的连接:
UsbDeviceConnection connection = device_manager.OpenDevice(device);
但是,我注意到接口索引 0 处的端点(上面的 endpoint_IN)具有 UsbAddressing 枚举类型“DirMask”,其中 endpoint_OUT 具有类型“Out”;我希望 endpoint_IN 为“In”,但事实并非如此。什么是“DirMask”?内联文档状态“尚未输入此部分的文档”,在线文档反映相同:http ://api.xamarin.com/?link=T%3aAndroid.Hardware.Usb.UsbAddressing
这可能是我的问题吗?我只是不太确定。我尝试执行其余的通信程序,但未能产生任何结果。例如,下面的代码应该输入一个命令来接收一个读数:
Byte[] sys_command = Encoding.ASCII.GetBytes("!001:SYS?\r");
Java.Nio.ByteBuffer sys_command_buffer = Java.Nio.ByteBuffer.Wrap(sys_command);
Java.Nio.ByteBuffer output_buffer = Java.Nio.ByteBuffer.Allocate(4);
UsbRequest request_out = new UsbRequest();
request_out.Initialize(connection, endpoint_OUT);
connection.ClaimInterface(intf, forceClaim);
request_out.Queue(output_buffer, 4);
connection.BulkTransfer(endpoint_IN, sys_command, sys_command.Length, TIMEOUT);
if (connection.RequestWait() == request_out)
readings.Text = output_buffer.GetFloat(0).ToString();
有什么见解吗?