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

问题描述 投票:0回答:2

我想编写一个应用程序,将原始数据发送到通过 USB 电缆连接到我的 Android 操作系统手机的设备。为此,我想使用.NET MAUI。我知道这是一个相当新的框架,但我想尝试一下。我想知道是否有一种方法可以使用 System.IO 命名空间中的 SerialPort 或其他一些库来完成此任务。到目前为止,我已经找到了一个库Device.Net,但它不支持.NET MAUI。如果/如何这是可能的,我将非常感激任何提示。

c# cross-platform maui .net-maui maui-windows
2个回答
0
投票
Activity act = 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;

deviceConnection
Android.Hardware.Usb.UsbDeviceConnection
类型,在connect方法中打开:

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

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

0
投票

您可以分享此代码吗?

© www.soinside.com 2019 - 2024. All rights reserved.