简体   繁体   English

如何使用 .NET MAUI 将原始数据写入 USB 连接设备

[英]How to write raw data to usb connected device using .NET MAUI

I would like to write an application that will send raw data to a device that is connected to my mobile phone with Android OS through USB cable.我想编写一个应用程序,将原始数据发送到通过 USB 电缆连接到我的 Android 操作系统手机的设备。 For this purpose, I would like to use .NET MAUI.为此,我想使用 .NET MAUI。 I know that this is a quite new framework, yet I would like to give it a shot.我知道这是一个相当新的框架,但我想试一试。 I'm wondering if there is a way that I can use SerialPort from System.IO namespace or some other library that will allow me to accomplish this task.我想知道是否有一种方法可以使用 System.IO 命名空间中的 SerialPort 或其他一些允许我完成此任务的库。 So far I have found a library Device.Net but it didn't support .NET MAUI.到目前为止,我已经找到了一个Device.Net库,但它不支持 .NET MAUI。 I would be very grateful for any hint if/how this is possible.如果/如何可能,我将非常感谢任何提示。

Activity act = Platform.CurrentActivity;活动行为 = Platform.CurrentActivity;

    UsbManager manager = (UsbManager)act.GetSystemService(Context.UsbService);

    IDictionary<string, UsbDevice> devicesDictionary = manager.DeviceList;

    UsbDevice dvc = devicesDictionary.ElementAt(0).Value;

    string ACTION_USB_PERMISSION = "rzepak";

    var interf = dvc.GetInterface(1);

    outEndpoint = interf.GetEndpoint(1);

    PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(act, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

    if (manager.HasPermission(dvc) == false) manager.RequestPermission(dvc, mPermissionIntent);

    deviceConnection = manager.OpenDevice(dvc);

    if (deviceConnection != null) deviceConnection.ClaimInterface(interf, true).ToString();
    else return false;

device connection is an Android.Hardware.Usb.UsbDeviceConnection type, opened in connect method):设备连接是 Android.Hardware.Usb.UsbDeviceConnection 类型,在连接方法中打开):

byte[] test = new byte[] { input }; byte[] test = new byte[] { 输入 };

    if (deviceConnection.BulkTransfer(outEndpoint, test, test.Length, 500) < 0) Connected = false;

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

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