Reputation: 37
I'm trying to create a .NET Maui application that reads from a device (barcode scanner) connected via usb (rs-232 emulation), but I still can't get the rights to open and the port open. Of course, I am also interested in reading from the port. Please do not have your successful solution available? Unfortunately, I couldn't find anything in the .NET Maui documentation.
This is my code based on the example from [https://stackoverflow.com/questions/73534644/how-to-write-raw-data-to-usb-connected-device-using-net-maui]:
Activity act = Platform.CurrentActivity;
UsbManager manager = (UsbManager)act.GetSystemService(Context.UsbService);
IDictionary<string, UsbDevice> devicesDictionary = manager.DeviceList;
UsbDevice dvc = _usbDevice; // I find _usbDevice successfully
string ACTION_USB_PERMISSION = "com.MauiAndroidSerialPort.USB_PERMISSION";
var interf = dvc.GetInterface(1);
var 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);
var deviceConnection = manager.OpenDevice(dvc);
if (deviceConnection != null)
deviceConnection.ClaimInterface(interf, true).ToString();
I get the device but the app crashes on the line "PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(act, 0, new Intent(ACTION_USB_PERMISSION), 0);"
Upvotes: 1
Views: 1903
Reputation: 2049
External barcode scanner acts like an external keyboard and types
recognized barcode text to the active control.
Have an active text entry control and make sure that it does not lose focus upon scanning with external barcode-reader. And when barcode is read, its value will appear in that control.
Upvotes: 0