我想在以下代码中使用 USB 设备。它成功列出了 USB 设备并对其进行了迭代。在以下代码中,对象“设备”是我需要打开的 USB 设备。除了总是返回空值的 OpenDevice() 方法外,一切似乎都很好!
[Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] {UsbManager.ActionUsbDeviceAttached})]
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]
public class MainActivity : Activity
{
int count = 1;
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
UsbManager manager = (UsbManager)GetSystemService(Context.UsbService);
UsbDevice device = null;
foreach (var dev in manager.DeviceList)
{
if (dev.Value.VendorId == 5401)
{
device = dev.Value;
}
}
var connection = manager.OpenDevice(device);
// Read some data! Most have just one port (port 0).
}
device_filter.xml 包含以下几行:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<usb-device product-id="8704" vendor-id="5401" />
</resources>
当我尝试 bool hasPermision = manager.HasPermission(device); 我看到 hasPermission 是假的。谁能告诉我如何授予在 xamarin 中打开 USB 设备的权限?谢谢你的帮助。