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